lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]



I am in the position to help a customer transition to Lua, and I'm sure they'll ask question s.a. where's "round" when we start going specific.

And since I didn't have the reason for abandoning 'round' I wanted to ask. That's all.

The specific variant I'd like to have is

function round( v, step )
    return math.floor( v*step +0.5 ) / step
end

This is the kind of things that just should "be there" (as most is, already). Maybe I'll just inject a 'math.round' to them myself, to avoid sticking to this issue and focusing on the language itself, instead.

Mark Hamburg nailed the reasoning right:
In adding a rounding function, there's a disincentive to add it to math itself because the Lua distribution conceptually controls that namespace. So one adds mymath.round. (Lightroom has AgMathUtils.round.) But then everyone goes off and re-invents this and when one wants to share code we run into problems.

The round above already causes a clash compared to the one on the Wiki (2nd param works differently).

-asko


On Wed, 23 Apr 2008 10:43:02 -0400
 "Mike Crowe" <drmikecrowe@gmail.com> wrote:
What's the specific request that we are trying to define? math.round(value,
precision) for non-integer rounding?

I'm confused about what is being requested. I agree with the earlier
observation that floor/ceil give us everything we need:

a = 12.345678
print(math.floor(a+0.5))
12
print(math.floor(a*10+0.5)/10)
12.3
print(math.floor(a*100+0.5)/100)
12.35

Am I missing something very obscure?