Doing this:
raise “false” if ((625.91 + 900.00 + 22.00) != 1547.91)
…raises the exception.
Seems like a pretty major addition bug to me. Anyone know of a good
workaround?
Sean O'Dell
Doing this:
raise “false” if ((625.91 + 900.00 + 22.00) != 1547.91)
…raises the exception.
Seems like a pretty major addition bug to me. Anyone know of a good
workaround?
Sean O'Dell
raise "false" if ((625.91 + 900.00 + 22.00) != 1547.91)
Well, this is well known
svg% ruby -e 'p "%.24f" % (625.91 + 900.00 + 22.00)'
"1547.909999999999854480847716"
svg%
svg% ruby -e 'p "%.24f" % 1547.91'
"1547.910000000000081854523160"
svg%
Guy Decoux
Well known? I had no idea this bug existed until I wrote a line of code that
double-checked the arithmetic from a different app. I have code all over
doing simple arithmetic like this. Ruby can’t add 3 simple numbers? I don’t
mean to be disproportionately negative, but that shakes my faith more than a
little. It’s just simple arithmetic. How old is this bug? When is it
getting fixed?
Sean O'Dell
On Wednesday 12 May 2004 09:53, ts wrote:
raise “false” if ((625.91 + 900.00 + 22.00) != 1547.91)
Well, this is well known
In article 200405121653.i4CGrEm04797@moulon.inra.fr,
ts decoux@moulon.inra.fr wrote:
raise “false” if ((625.91 + 900.00 + 22.00) != 1547.91)
Well, this is well known
I’m not sure how well known this is. Is it well known as being a problem
or well known as in “you should never do this sort of thing”.
Seems to present a major problem if you want to deal with financial
figures, for example. Are there any workarounds?
Phil
Sorry, to shake your faith, but consider the following:
jfn@juno 124 /home/jfn/tmp > cat n.c
int main()
{
printf(“%.24f\n”, 625.91+900.00+22.00);
}
jfn@juno 125 /home/jfn/tmp > ./a.out
1547.909999999999854480847716
You might want to try the rational library.
Jim
On Thursday, 13 May 2004 at 2:07:52 +0900, Sean O’Dell wrote:
On Wednesday 12 May 2004 09:53, ts wrote:
raise “false” if ((625.91 + 900.00 + 22.00) != 1547.91)
Well, this is well known
Well known? I had no idea this bug existed until I wrote a line of code that
double-checked the arithmetic from a different app. I have code all over
doing simple arithmetic like this. Ruby can’t add 3 simple numbers? I don’t
mean to be disproportionately negative, but that shakes my faith more than a
little. It’s just simple arithmetic. How old is this bug? When is it
getting fixed?Sean O’Dell
–
Jim Freeze
Police: Good evening, are you the host?
Host: No.
Police: We’ve been getting complaints about this party.
Host: About the drugs?
Police: No.
Host: About the guns, then? Is somebody complaining about the guns?
Police: No, the noise.
Host: Oh, the noise. Well that makes sense because there are no guns
or drugs here. (An enormous explosion is heard in the
background.) Or fireworks. Who’s complaining about the noise?
The neighbors?
Police: No, the neighbors fled inland hours ago. Most of the recent
complaints have come from Pittsburgh. Do you think you could
ask the host to quiet things down?
Host: No Problem. (At this point, a Volkswagon bug with primitive
religious symbols drawn on the doors emerges from the living
room and roars down the hall, past the police and onto the
lawn, where it smashes into a tree. Eight guests tumble out
onto the grass, moaning.) See? Things are starting to wind
down.
Well known? I had no idea this bug existed until I wrote a line of code that
double-checked the arithmetic from a different app. I have code all over
doing simple arithmetic like this. Ruby can't add 3 simple numbers? I don't
mean to be disproportionately negative, but that shakes my faith more than a
little. It's just simple arithmetic. How old is this bug? When is it
getting fixed?
http://docs.sun.com/source/806-3568/ncg_goldberg.html
Guy Decoux
It’s about 40 years old, and unlikely to be fixed. Floating point
numbers are not represented exactly inside computers, and so floating
point comparisons are routinely deprecated in books on programming.
Certain values cannot ever be expressed in floating point
representation.
If you want exact, fractional, math, you should probably use the
‘rational’ library and investigate ‘mathn’.
Cheers
Dave
On May 12, 2004, at 12:07, Sean O’Dell wrote:
On Wednesday 12 May 2004 09:53, ts wrote:
raise “false” if ((625.91 + 900.00 + 22.00) != 1547.91)
How old is this bug? When is it
getting fixed?
Try the same in Python. It's the same.
In Ruby you can use BigDecimal:
require 'bigdecimal'
BigDecimal.new("625.91") + 900 + 22 == BigDecimal.new("1547.91") # => true
Regards,
Michael
On Thu, May 13, 2004 at 02:07:52AM +0900, Sean O'Dell wrote:
On Wednesday 12 May 2004 09:53, ts wrote:
>
> > raise "false" if ((625.91 + 900.00 + 22.00) != 1547.91)
>
> Well, this is well knownWell known? I had no idea this bug existed until I wrote a line of code that
double-checked the arithmetic from a different app. I have code all over
doing simple arithmetic like this. Ruby can't add 3 simple numbers? I don't
mean to be disproportionately negative, but that shakes my faith more than a
little. It's just simple arithmetic. How old is this bug? When is it
getting fixed?
Sean O’Dell wrote:
On Wednesday 12 May 2004 09:53, ts wrote:
“S” == Sean O’Dell sean@celsoft.com writes:
raise “false” if ((625.91 + 900.00 + 22.00) != 1547.91)
Well, this is well known
Well known? I had no idea this bug existed until I wrote a line of code that
double-checked the arithmetic from a different app. I have code all over
doing simple arithmetic like this. Ruby can’t add 3 simple numbers? I don’t
mean to be disproportionately negative, but that shakes my faith more than a
little. It’s just simple arithmetic. How old is this bug? When is it
getting fixed?Sean O’Dell
.
For what its worth, it appears (on my box, at least) that perl and
python both give the same result. It is not a Ruby bug, but rather a
problem with floating point round off.
–
Jamis Buck
jgb3@email.byu.edu
http://www.jamisbuck.org/jamis
ruby -h | ruby -e
‘a=;readlines.join.scan(/-(.)[e|Kk(\S*)|le.l(…)e|#!(\S*)/) {|r| a <<
r.compact.first };puts “\n>#{a.join(%q/ /)}<\n\n”’
“Sean O’Dell” sean@celsoft.com writes:
raise “false” if ((625.91 + 900.00 + 22.00) != 1547.91)
Well, this is well known
Well known? I had no idea this bug existed until I wrote a line of
code that double-checked the arithmetic from a different app. I have
code all over doing simple arithmetic like this. Ruby can’t add 3
simple numbers? I don’t mean to be disproportionately negative, but
that shakes my faith more than a little. It’s just simple arithmetic.
How old is this bug? When is it getting fixed?
In 1969 I took a course called Introduction to Computer Programming. In
one of the early chapters of the textbook, we learned about a concept
called “floating point arithmetic”. One aspect of this topic that was
drilled into us is the way that floating point arithmetic does not yield
exact results.
I want to add that this concept, which is so basic to the understanding
of programming that it was taught in an introductory course before we
even wrote a line of code, was old information even in 1969.
This basic concept of floating point arithmetic is every bit as
pertinent today as it was in 1969 and the years prior to that. Did you
actually not learn this concept before starting to program in Ruby?
In the off chance that you were absent that day when they talked about
this in school, or that the 5 pages that discussed this in your
Introduction to Computer Programming book accidentally got eaten by your
dog, I want you to know that the case that you are describing here is
not a bug, but rather, an intrisic feature of how computers do certain
kinds of arithmetic.
Do a search in Google on “floating point arithmetic” (best to put
that entire phrase in double quotes) for more information.
On Wednesday 12 May 2004 09:53, ts wrote:
Sean O’Dell
–
Lloyd Zusman
ljz@asfast.com
God bless you.
i’d say no time soon unless ruby decides to implement it’s own floating point
hardware emulator:
~ > cat a.c
#include <stdlib.h>
#include <stdio.h>
int
main (argc, argv, env)
int argc;
char **argv;
char **env;
{
float total = 1547.91;
float a = 625.91;
float b = 900.00;
float c = 22.00;
float sum;
sum = a + b + c;
fprintf (stdout, "total <%f>\n", total);
fprintf (stdout, "sum <%f>\n", sum);
return 0;
}
~ > gcc a.c -o a
~ > ./a
total <1547.910034>
sum <1547.909912>
-a
On Thu, 13 May 2004, Sean O’Dell wrote:
On Wednesday 12 May 2004 09:53, ts wrote:
raise “false” if ((625.91 + 900.00 + 22.00) != 1547.91)
Well, this is well known
Well known? I had no idea this bug existed until I wrote a line of code that
double-checked the arithmetic from a different app. I have code all over
doing simple arithmetic like this. Ruby can’t add 3 simple numbers? I don’t
mean to be disproportionately negative, but that shakes my faith more than a
little. It’s just simple arithmetic. How old is this bug? When is it
getting fixed?
EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
PHONE :: 303.497.6469
ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
URL :: Solar-Terrestrial Physics Data | NCEI
TRY :: for l in ruby perl;do $l -e “print "\x3a\x2d\x29\x0a"”;done
===============================================================================
Seems to present a major problem if you want to deal with financial
figures, for example. Are there any workarounds?
Don’t use floats for financial figures. Use a Money object, such as the
one[1] Fowler describes in PoEAA. That way you’ll be setup for handling
currency issues as well.
[1] P of EAA: Money
–
David Heinemeier Hansson,
http://www.instiki.org/ – A No-Step-Three Wiki in Ruby
http://www.basecamphq.com/ – Web-based Project Management
http://www.loudthinking.com/ – Broadcasting Brain
http://www.nextangle.com/ – Development & Consulting Services
In article c7tljo01576@enews4.newsguy.com,
Phil Tomson ptkwt@aracnet.com wrote:
In article 200405121653.i4CGrEm04797@moulon.inra.fr,
ts decoux@moulon.inra.fr wrote:
raise “false” if ((625.91 + 900.00 + 22.00) != 1547.91)
Well, this is well known
I’m not sure how well known this is. Is it well known as being a problem
or well known as in “you should never do this sort of thing”.Seems to present a major problem if you want to deal with financial
figures, for example. Are there any workarounds?
Sorry to respond to my own post:
Probably the easiest workaround is not to use ‘==’ or ‘!=’ to compare
floating point numbers. Use ‘>’,‘<’ instead.
Or round off. Maybe mathn does what you need it to do.
Phil
Phil Tomson wrote:
[Floating point imprecision seems] to present a major
problem if you want to deal with financial figures, for
example. Are there any workarounds?
Financial calculations aren’t done in floating point. There are various
number formats that are used, but they all boil down to some form of integer
arithmetic. For example, fixed point decimal may be used, where the value
$625.91 is represented as the integer 62591 with a scaling factor of two
decimal places. In other words, the arithmetic is actually done in pennies,
not dollars.
-Mike
ptkwt@aracnet.com (Phil Tomson) writes:
In article 200405121653.i4CGrEm04797@moulon.inra.fr,
raise “false” if ((625.91 + 900.00 + 22.00) != 1547.91)
Well, this is well known
I’m not sure how well known this is. Is it well known as being a problem
or well known as in “you should never do this sort of thing”.Seems to present a major problem if you want to deal with financial
figures, for example. Are there any workarounds?
I have been programming for 35 years, and during this time, I keep
seeing members of the financial community writing programs that do money
math with floating point numbers. Several times in my career we had to
rewrite code that mis-used floating point in this way, when people
finally realized that they can’t get exact results.
One way around this in Ruby is to use the BigDecimal class. It allows
arbitrary precision math to be performed. There are other solutions, as
well. For example, the Rational class can also be used for financial
calculations.
Sean O’Dell wrote:
“S” == Sean O’Dell sean@celsoft.com writes:
raise “false” if ((625.91 + 900.00 + 22.00) != 1547.91)
Well, this is well known
Well known? I had no idea this bug existed until I wrote a line of code that
double-checked the arithmetic from a different app. I have code all over
doing simple arithmetic like this. Ruby can’t add 3 simple numbers? I don’t
mean to be disproportionately negative, but that shakes my faith more than a
little. It’s just simple arithmetic. How old is this bug? When is it
getting fixed?
Offhand I’d say this bug dates back to the 1940s, and isn’t
getting fixed any time soon.
Seriously, I think it’s a matter of trying to store an infinite
number of digits in a finite number of bits.
For a daily life example: How many digits does it take to store
“1/3” exactly? 0.333333…
Hal
On Wednesday 12 May 2004 09:53, ts wrote:
I’m not sure how well known this is. Is it well known as being a problem
or well known as in “you should never do this sort of thing”.Seems to present a major problem if you want to deal with financial
figures, for example. Are there any workarounds?
Short answer: use rational numbers – slower, but works.
Long answer: For finance, specifically, use integer numbers of pennies,
at least for US currency, or some other fixed-point format. It has
error, but it’s an accepted, standard error. According to most
accountants, 0.005 + 0.044 is 0.05.
Ari
I don’t expect a script language to have those same rounding problems, so I
hope this gets fixed quickly. Thanks for the tip.
Sean O'Dell
On Wednesday 12 May 2004 10:12, Jim Freeze wrote:
On Thursday, 13 May 2004 at 2:07:52 +0900, Sean O’Dell wrote:
On Wednesday 12 May 2004 09:53, ts wrote:
raise “false” if ((625.91 + 900.00 + 22.00) != 1547.91)
Well, this is well known
Well known? I had no idea this bug existed until I wrote a line of code
that double-checked the arithmetic from a different app. I have code all
over doing simple arithmetic like this. Ruby can’t add 3 simple numbers?
I don’t mean to be disproportionately negative, but that shakes my faith
more than a little. It’s just simple arithmetic. How old is this bug?
When is it getting fixed?Sean O'Dell
Sorry, to shake your faith, but consider the following:
jfn@juno 124 /home/jfn/tmp > cat n.c
int main()
{
printf(“%.24f\n”, 625.91+900.00+22.00);
}
jfn@juno 125 /home/jfn/tmp > ./a.out
1547.909999999999854480847716You might want to try the rational library.
Funny though, if I set a variable to 1547.91 exactly, it is represented just
fine. A value during the arithmetic must be a value which can’t be
represented.
I had to massage my floating point math in C/C++ a lot many years ago, but I
always assumed it was a flaw in the MS compiler, and once I got in the habit
of doing it, I forgot about it.
Thanks for the info. I’m far less bitter now about having to round the
numbers for comparison. =)
Sean O'Dell
On Wednesday 12 May 2004 10:18, Dave Thomas wrote:
On May 12, 2004, at 12:07, Sean O’Dell wrote:
On Wednesday 12 May 2004 09:53, ts wrote:
raise “false” if ((625.91 + 900.00 + 22.00) != 1547.91)
How old is this bug? When is it
getting fixed?It’s about 40 years old, and unlikely to be fixed. Floating point
numbers are not represented exactly inside computers, and so floating
point comparisons are routinely deprecated in books on programming.
Certain values cannot ever be expressed in floating point
representation.If you want exact, fractional, math, you should probably use the
‘rational’ library and investigate ‘mathn’.
I guess, you guys are missing the point,
in real life you can’t have exact answer of 1/3 + 2.3 + 1.3
but you can have exact answer of 625.91 + 900.00 + 22.00
Mohammad
----- Original Message -----
From: “Hal Fulton” hal9000@hypermetrics.com
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Wednesday, May 12, 2004 3:15 PM
Subject: Re: Major Addition Bug?
Sean O’Dell wrote:
On Wednesday 12 May 2004 09:53, ts wrote:
“S” == Sean O’Dell sean@celsoft.com writes:
raise “false” if ((625.91 + 900.00 + 22.00) != 1547.91)
Well, this is well known
Well known? I had no idea this bug existed until I wrote a line of code
that
double-checked the arithmetic from a different app. I have code all
over
doing simple arithmetic like this. Ruby can’t add 3 simple numbers? I
don’t
mean to be disproportionately negative, but that shakes my faith more
than a
little. It’s just simple arithmetic. How old is this bug? When is it
getting fixed?Offhand I’d say this bug dates back to the 1940s, and isn’t
getting fixed any time soon.Seriously, I think it’s a matter of trying to store an infinite
number of digits in a finite number of bits.For a daily life example: How many digits does it take to store
“1/3” exactly? 0.333333…Hal