Simple Math Problem

T

Thom Loring

Can anyone shed some light on a simple math problem I have encountered?
I am doing a very simple calculation, and the comparison of that result
compared to the number itself is returning false in some cases...for
instance:

$irb=> 80.04
=> 80.04
=> false
=> true
=> false
=> "80.04"
=> "80.04"

Any insight?

Thanks,
Thom
 
T

Tim Pease

Can anyone shed some light on a simple math problem I have encountered?
I am doing a very simple calculation, and the comparison of that result
compared to the number itself is returning false in some cases...for
instance:

$irb
=> 80.04

=> 80.04

=> false

=> true

=> false

=> "80.04"

=> "80.04"

Any insight?

Machine roundoff error. Floating point values are never precise on any computer.

The best way to compare two floating point values is to take there
difference and make sure it lies withing some acceptable error limit.

v1 = 80.04
v2 = 0.12 * 667
diff = v1-v2
diff.abs < Float::EPSILON

That last line returns false, so you need to choose a different epsilon ...

diff.abs < 1.0e-12

That will return true.

Blessings,
TwP
 
H

Hugh Sasse

Machine roundoff error. Floating point values are never precise on any
computer.
brains hgs 226 %> irb
irb(main):001:0> v1 = 80.04
=> 80.04
irb(main):002:0> v2 = 0.12 * 667
=> 80.04
irb(main):003:0> puts "%.20f" % v1
80.04000000000000625278
=> nil
irb(main):004:0> puts "%.20f" % v2
80.03999999999999204192
=> nil
irb(main):005:0> exit
brains hgs 227 %>

Hugh
 
T

Thom Loring

Hmm...yeah, I figured that must be it, but I just didn't expect it with
numbers of this scale. Thanks for the insight!

Thom
 
A

ara.t.howard

Machine roundoff error. Floating point values are never precise on any
computer.

The best way to compare two floating point values is to take there
difference and make sure it lies withing some acceptable error limit.

v1 = 80.04
v2 = 0.12 * 667
diff = v1-v2
diff.abs < Float::EPSILON

That last line returns false, so you need to choose a different epsilon ...

diff.abs < 1.0e-12

That will return true.

boy, how bout an rcr for that behaviour if one write this

2.0 =~ 2.0

??

-a
 
T

Tim Pease

brains hgs 226 %> irb
irb(main):001:0> v1 = 80.04
=> 80.04
irb(main):002:0> v2 = 0.12 * 667
=> 80.04
irb(main):003:0> puts "%.20f" % v1
80.04000000000000625278
=> nil
irb(main):004:0> puts "%.20f" % v2
80.03999999999999204192
=> nil
irb(main):005:0> exit
brains hgs 227 %>

With IEEE 754 floating point values you only have 15 digits of
precision (8 byte doubles). Ara's little example above demonstrates
that very nicely. Things go pear shaped in the 17th digit with the
"exact" value 80.04 (v1).

Take a look at the "assert_in_delta" method of the Test::Unit library.
That performs floating point comparison by comparing a diff against
some epsilon (delta).

<shameless plug>
There is an RCR open to implement a =~ operator for floating point
values that would perform a diff comparison against an epsilon value.

http://rcrchive.net/rcr/show/340
</shameless plug>

Blessings,
TwP
 
T

Tim Pease

With IEEE 754 floating point values you only have 15 digits of
precision (8 byte doubles). Ara's little example above demonstrates
that very nicely. Things go pear shaped in the 17th digit with the
"exact" value 80.04 (v1).

<errata>
Sorry, that should be "Hugh's little example above".
</errata>

TwP
 
H

Hugh Sasse

irb(main):002:0> v2 = 0.12 * 667
=> 80.04 [...]
irb(main):004:0> puts "%.20f" % v2
80.03999999999999204192
[...]

With IEEE 754 floating point values you only have 15 digits of

I just picked a large value. This seemed the clearest way to show
what was going on, from my viewpoint.
precision (8 byte doubles). Ara's little example above demonstrates
that very nicely. Things go pear shaped in the 17th digit with the
"exact" value 80.04 (v1).
[other good points trimmed]
Hugh
 
J

Josef 'Jupp' Schugt

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

* Thom Loring, 11/08/2006 06:35 PM:
Any insight?

Visit an introduction to Numerical Mathematics at your favorite
university. Numerical imprecision and the stability of algorithms
against it is one of the most prominent questions addressed there.
Also they address the question how precise a result of a computation
can be given a certain imprecision of input that either results from
the computer's approximation to a given value (decimal 0.1 cannot be
represented as a binary fraction with a finite number of digits) or
from imprecisely known values - which includes any measured quantity
occuring in natural science (actually all quantities that are not
defined by law as it is the case with vacuum light speed that by law
is defined to be precisely 299792458 m/s).

Jupp
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (GNU/Linux)

iD8DBQFFU5+6rhv7B2zGV08RAquRAJ9iylLMInU/DyFSLmbgtHYAf1CPTACfZz8I
NvxFp0ctdnvL8LmK43KbvK4=
=jb/q
-----END PGP SIGNATURE-----
 
B

benjohn

Jupp:
Visit an introduction to Numerical Mathematics at your favorite
university. Numerical imprecision and the stability of algorithms
against it is one of the most prominent questions addressed there.
Also they address the question how precise a result of a computation
can be given a certain imprecision of input that either results from
the computer's approximation to a given value (decimal 0.1 cannot be
represented as a binary fraction with a finite number of digits) or
from imprecisely known values - which includes any measured quantity
occuring in natural science (actually all quantities that are not
defined by law as it is the case with vacuum light speed that by law
is defined to be precisely 299792458 m/s).

It's jolly decent of light to be so obliging about the speed it travels
at. I count myself very lucky to live in a universe where it takes on an
integer speed.

;)
 

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

Forum statistics

Threads
474,183
Messages
2,570,969
Members
47,524
Latest member
ecomwebdesign

Latest Threads

Top