[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Strange add results
- From: Peter Cawley <lua@...>
- Date: Sat, 20 Nov 2010 13:39:12 +0000
2010/11/20 Alexey Malyshev <malma@kaluga.net>:
> What's wrong with this code?
>
> local i = 1
> i = i + 0.2
> i = i + 0.2
> i = i + 0.2
> i = i + 0.2
> i = i + 0.2
> print(i)
> print(tostring( i ~= 2))
>
> output:
>
> 2
> true
Welcome to the world of floating point arithmetic. Note the following:
> i=1 for k=1,5 do i=i+0.2 end
> print(("%.17f"):format(i)) --> 1.99999999999999980
By default, print on floating point numbers rounds to less than 17
places, at which point the value of i is rounded to 2.