```This is how floating point math works. If you want exact values use
decimals.
http://en.wikipedia.org/wiki/Floating_point

On Mon, Jul 18, 2011 at 10:15 AM, ZipiZap zap <zipizap123 / gmail.com> wrote:

> Hello all,
>
> This is my first mail into the mailing list, so please be kind with any
> errors, and please point them out so we can learn from them :)
>
> Last night I was messing with some simple arithmetic operations in irb, and
> notice a funny thing: when I made subtractions (did not tested any other
> operation) I did not got the expected results...
>
> It seems that the result of floating-point subtraction is not correct... as
> if it had some residual value left in some deep decimal position...
> This happens in both ruby 1.8.7 (2011-06-30 patchlevel 352) [i686-linux]
> and
> ruby 1.9.2p180 (2011-02-18 revision 30909) [i686-linux]
>
> I'm I missing something?
> Or is it that the results are in fact incorrect and a bug should be
> reported?
> If so, should I report the bug into "http://redmine.ruby-lang.org/
> ", or where?
>
>
> \$ ruby -v
> ruby 1.8.7 (2011-06-30 patchlevel 352) [i686-linux]*
> *
> \$ irb
> ruby-1.8.7-p352 :001 > 2.45 - 1.45
>  => 1.0
> ##
> ## The last result is ok
> ##
>
> ruby-1.8.7-p352 :002 > (0...10).map {|x| (x+0.9) - (x+0.7)}
>  => [0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.200000000000001,
> 0.200000000000001]
> ##
> ## The last results should all be 0.20 and they not only are different from
> 0.20 but they are also ?!?different between themselves?!?
> ## See the following "uniq" filter
> ## This also happens in Ruby 1.9.2
>
> ruby-1.8.7-p352 :003 > (0...10).map {|x| (x+0.9) - (x+0.7)}.uniq
>  => [0.2, 0.2, 0.2, 0.2, 0.200000000000001]
>
>
>
>
> \$ ruby -v
> ruby 1.9.2p180 (2011-02-18 revision 30909) [i686-linux]
>
> \$ irb
> ruby-1.9.2-p180 :002 >   2.45 - 1.45
>  => 1.0000000000000002
>
> ruby-1.9.2-p180 :005 >   (0...10).map {|x| (x+0.9) - (x+0.7)}
>  => [0.20000000000000007, 0.19999999999999996, 0.19999999999999973,
> 0.19999999999999973, 0.20000000000000018, 0.20000000000000018,
> 0.20000000000000018, 0.20000000000000018, 0.20000000000000107,
> 0.20000000000000107]
>
> ruby-1.9.2-p180 :006 > (0...10).map {|x| (x+0.9) - (x+0.7)}.uniq
>  => [0.20000000000000007, 0.19999999999999996, 0.19999999999999973,
> 0.20000000000000018, 0.20000000000000107]
>

--