[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: 5.3.0-work3 and lossless integer conversion
- From: Josh Haberman <jhaberman@...>
- Date: Thu, 26 Jun 2014 11:41:39 -0700
Let me try to alleviate some possible "floating-point anxiety."
Doubles can represent integers [-2^53, 2^53] exactly. If you have
integers in this range, addition, subtraction, and multiplication
yield exact integers.
1.0 + 2.0 = 3.0 (exactly)
5.0 - 1.0 = 4.0 (exactly)
2.0 * 3.0 = 6.0 (exactly)
This is unlike decimal numbers, where some numbers are exact and some
are approximate.
0.25 + 0.25 = 0.5 (exactly)
0.1 + 0.2 = 0.3000000000000000444089209850062616169452667236328125
Because floating point numbers are inexact for decimals, people tend
to assume that floating point numbers are approximate and unreliable
in general. But as long as you are dealing with integer values, the
values will stay integral.
Division is another story, of course:
2.0 / 3.0 = 0.66666666666666662965923251249478198587894439697265625
But luckily Lua 5.3 is providing an integer division:
2.0 // 3.0 = 0 (result takes the floor)
So if anyone is afraid that inexactness will creep into your
integral-valued doubles, hopefully this info will help. In a loop like
this, "i" is guaranteed to stay perfectly integral:
local i = 1.0
while i < 100 do
i = i + 1.0
-- do some other stuff...
end
If you *do* end up with a value like 10.0000000000000073 in a place
you were expecting an integer, it means that somewhere along the line
one of your inputs was non-integral (or you used non-integer division
without rounding the result). These are errors you'd want to have
raised IMHO.
On Thu, Jun 26, 2014 at 10:28 AM, Rena <hyperhacker@gmail.com> wrote:
> On Jun 26, 2014 8:36 AM, "Roberto Ierusalimschy" <roberto@inf.puc-rio.br>
> wrote:
>>
>> > > The other place where truncation (already) occurs is "outside" Lua,
>> > > in the API, when the C code uses things like lua_checkinteger. This
>> > > is the only place where the change would affect compatibility:
>> > > string.sub("abc", 2.3) currently behaves in a non-specified way
>> > > (although most certainly it is equivalent to string.sub("abc", 2)
>> > > and we are afraid some programmers assume this behavior); current
>> > > 5.3w specifies that 2.3 should be truncated to 2, while with this new
>> > > proposal Lua would raise an error.
>> > >
>> >
>> > Are you saying that a float value would return an error, or a
>> > non-integral
>> > value would return an error?
>>
>> A non-integral value would raise an error, as an out-of-bound float
>> already raises an error in work3. Something like 10.0 still behaves
>> like 10 in most situations.
>>
>> -- Roberto
>>
>
> What about 10.0000000000000073?