"Josef 'Jupp' Schugt" <jupp / gmx.de> wrote: > > [...] Even the most trivial computations are > nontrivial when one has a closer look at them: > > 1.upto(16) { |i| > puts 1.0 + (0.1 ** i) - 1.0 - (0.1 ** i) > } > > on my machine results in: > > 8.32667268468867e-17 > [snipped some] > -1 e-16 > > The mathematical results of course all are 0. > > Josef 'Jupp' Schugt > Just trollin', but is it right that Float#to_f should be a no-op when it could have saved doing 'to_s.to_f' (or other) below ?: expect = [ nil, 8.32667268468867e-17, 6.93889390390723e-18, -1.10371781159024e-16, -1.10317571050400e-17, 6.55095281406476e-17, ] 1.upto(5) do |i| ans = 1.0 + (0.1 ** i) - 1.0 - (0.1 ** i) unless ans == expect[i] p [ans, expect[i]] puts 'Oops' unless Float === ans and Float === expect[i] if ans.to_s.to_f == expect[i] # <<----##### # ans.to_f short-circuits (just returns self) puts "Not equal but equal"; puts end end end #-> [8.32667268468867e-17, 8.32667268468867e-17] #-> Not equal but equal #-> #-> [6.93889390390723e-18, 6.93889390390723e-18] #-> Not equal but equal #-> #-> [-1.10371781159024e-16, -1.10371781159024e-16] #-> Not equal but equal #-> #-> [-1.103175710504e-17, -1.103175710504e-17] #-> Not equal but equal #-> #-> [6.55095281406476e-17, 6.55095281406476e-17] #-> Not equal but equal daz