[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Floating point inaccuracies
- From: bb <bblochl@...>
- Date: Sat, 27 Nov 2010 21:52:40 +0100
Am 27.11.2010 21:29, schrieb bb:
> Am 27.11.2010 21:16, schrieb Andreas Falkenhahn:
>> Hi,
>>
>> why does this code not print "yo"?
>>
>> ---snip---
>> local x = 1.1
>> x = x + 0.1
>> if x == 1.2 then
>> print("yo")
>> end
>> ---snap---
>>
>> x is set to 1.2 but the if clause isn't entered. Can anybody explain why?
>>
>> Tks,
>>
>> Andreas
>>
>>
> The answer is very simple:
>
>> x= 1.1+0.1
>> print(string.format("x = %.20f", x))
> x = 1.20000000000000017764
>
> You may find a solution at http://lua-users.org/wiki/SimpleRound
>
> Regads BB
>
Some extension to avoid misunderstandings and to straighten out the
concept of cheating the compiler:
> x=1.1+0.1
> y=1.2
> print(string.format("x = %.20f", x))
x = 1.20000000000000017764
> print(string.format("y = %.20f", y))
y = 1.19999999999999995559
> x=round(x,2)
> print(string.format("x = %.20f", x))
x = 1.19999999999999995559
> y=round(y,2)
> print(string.format("y = %.20f", y))
y = 1.19999999999999995559
> print(x==y)
true
>