what's wrong with these calculations?

I

Ivo Dancet

Hi

I thought, why search for two hours being totally frustrated if I can
just ask...

why does this happen:
=> 9.06999999999999 # why oh why?

What should I do about it?

Thanks
-c
 
H

hemant

Hi

I thought, why search for two hours being totally frustrated if I can
just ask...

why does this happen:

=> 9.06999999999999 # why oh why?

Thats folly of floats, you can't do much apart from :

sprintf("%.2f",(73.07-64.00)).to_f



--
Let them talk of their oriental summer climes of everlasting
conservatories; give me the privilege of making my own summer with my
own coals.

http://gnufied.org
 
H

hemant

Thats folly of floats, you can't do much apart from :

sprintf("%.2f",(73.07-64.00)).to_f

generally, I add a method to Float class like this:

class Float
def r2p places
sprintf("%.#{places}f",self).to_f
end
end
 
J

Jason Roelofs

Note: parts of this message were removed by the gateway to make it a legal Usenet post.

generally, I add a method to Float class like this:

class Float
def r2p places
sprintf("%.#{places}f",self).to_f
end
end
Also look into using the BigDecimal class. It does special processing to
keep floating point precision.

Jason
 
P

Phrogz

generally, I add a method to Float class like this:

class Float
def r2p places
sprintf("%.#{places}f",self).to_f
end
end

Note that, due to the nature of floats, calling to_f on that string
brings you back into the problem again:

x = ( 73.07 - 64.00 )
y = x.r2p( 2 )
p x, y, "%.20f" % y

#=> 9.06999999999999
#=> 9.07
#=> "9.07000000000000028422"
 
J

Jason Roelofs

Note: parts of this message were removed by the gateway to make it a legal Usenet post.

Note that, due to the nature of floats, calling to_f on that string
brings you back into the problem again:

x = ( 73.07 - 64.00 )
y = x.r2p( 2 )
p x, y, "%.20f" % y

#=> 9.06999999999999
#=> 9.07
#=> "9.07000000000000028422"
require 'bigdecimal'
require 'bigdecimal/math'
include BigMath

(BigDecimal("73.07") - BigDecimal("64.00")).to_f #=> 9.07

http://www.ruby-doc.org/stdlib/libdoc/bigdecimal/rdoc/index.html

Jason
 
P

Phrogz

Note: parts of this message were removed by the gateway to make it a legal Usenet post.

require 'bigdecimal'
require 'bigdecimal/math'
include BigMath

(BigDecimal("73.07") - BigDecimal("64.00")).to_f #=> 9.07

http://www.ruby-doc.org/stdlib/libdoc/bigdecimal/rdoc/index.html

irb(main):002:0> require 'bigdecimal'
irb(main):003:0> require 'bigdecimal/math'
irb(main):004:0> include BigMath
irb(main):005:0> y = (BigDecimal("73.07") - BigDecimal("64.00")).to_f
=> 9.07
irb(main):006:0> "%.20f" % y
=> "9.07000000000000028422"

You might not see the problem on that particular output, but the
potential inaccuracy still exists as long as you're in the Float
domain.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top