From: Robert Klemme Date: 2007-08-20T03:04:58+09:00 Subject: Re: Float addition problem / Processor bug? On 19.08.2007 19:49, Christian Luginbuehl wrote: > Hi, > > Check out this weird IRB output: > > === START === > irb(main):001:0> a = 2.95 + 2.95 + 2.95 > => 8.85 > irb(main):002:0> puts 8.85 - a > -1.77635683940025e-15 > => nil > === END === > > I'm running "ruby 1.8.6 (2007-06-07 patchlevel 36) [i486-linux]" on > Xubuntu beta with this processor: > > === START cat /proc/cpuinfo === > processor : 0 > vendor_id : GenuineIntel > cpu family : 15 > model : 2 > model name : Intel(R) Pentium(R) 4 CPU 2.26GHz > stepping : 4 > cpu MHz : 2271.951 > cache size : 512 KB > fdiv_bug : no > hlt_bug : no > f00f_bug : no > coma_bug : no > fpu : yes > fpu_exception : yes > cpuid level : 2 > wp : yes > flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge > mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm up > bogomips : 4547.85 > clflush size : 64 > === END cat /proc/cpuinfo === > > Is it a processor bug, a simple rounding problem or a ruby error? The second one. This is a standard rounding issue. The short explanation is that you see the numbers in decimal system while the computer uses binary representation for floats. > How do I proceed? Live with it or use other data types like BigDecimal or Rational. irb(main):001:0> require 'bigdecimal' => true irb(main):002:0> x = BigDecimal.new "2.95" => # irb(main):003:0> a = x + x + x => # irb(main):004:0> 8.85 - a => 0.0 irb(main):005:0> (8.85 - a).class => Float irb(main):007:0> BigDecimal.new("8.85") - a => # irb(main):008:0> Kind regards robert