[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Bizarre Floating point errors in Ruby? Serious bug?

MenTaLguY

11/28/2007 9:21:00 PM

On Thu, 29 Nov 2007 06:03:21 +0900, "Shot (Piotr Szotkowski)" <shot@hot.pl> wrote:
> space.ship.traveller@gmail.com:
>
>> >>> print (-140.14 * 100)
>> -14014.0
>
>> Well, actually this is the correct answer.... i.e. the result we are
>> looking for if we were using real math. But, it is not the correct
>> way to round 14013.999999, which is the actual value that we get with
>> floating point math...
>
> Why isnâ??t it? I might have learned basic math years ago, but to
> me -14013.999999 rounded to one decimal place is exactly -14014.0
> (itâ??s also -14014.00 when rounded to two decimal places, as well as
> -14014 when rounded to integers).

This comes up a lot, and I guess the thing that most people have a
hard time wrapping their heads around here is the fact that floating
point numbers don't obey the laws of basic math -- not exactly.

Floating point numbers are an approximation of real numbers. That's
all. We're stuck with them because it isn't physically possible to
represent real numbers in hardware, and floating point is one of
the few ways to approximate them efficiently.

And it is a hardware limitation -- you'll see the same behavior in
languages like C or Java which also rely on hardware support for
computations with "decimal" numbers.

-mental


3 Answers

Gary Wright

11/29/2007 3:49:00 AM

0


On Nov 28, 2007, at 4:20 PM, MenTaLguY wrote:
> This comes up a lot, and I guess the thing that most people have a
> hard time wrapping their heads around here is the fact that floating
> point numbers don't obey the laws of basic math -- not exactly.

That and the fact that internally numbers are represented in base 2
and externally they are represented in base 10. Unfortunately there
is not a one-to-one mapping between the two representations leading
to inexact conversions and confusion. If evolution had given us four
fingers on each hand and we counted in base 8 then maybe we could
have avoided these problems. :-)

Gary Wright

MonkeeSage

11/29/2007 4:35:00 AM

0

On Nov 28, 9:49 pm, Gary Wright <gwtm...@mac.com> wrote:
> On Nov 28, 2007, at 4:20 PM, MenTaLguY wrote:
>
> > This comes up a lot, and I guess the thing that most people have a
> > hard time wrapping their heads around here is the fact that floating
> > point numbers don't obey the laws of basic math -- not exactly.
>
> That and the fact that internally numbers are represented in base 2
> and externally they are represented in base 10. Unfortunately there
> is not a one-to-one mapping between the two representations leading
> to inexact conversions and confusion. If evolution had given us four
> fingers on each hand and we counted in base 8 then maybe we could
> have avoided these problems. :-)
>
> Gary Wright

I think the OP understands about bases (if I may presume a bit here!).
What I think was a suprise to him was that the string representation
not only rounds, but presents a trailing ".0", as if it were accurate
to within one magnitude of 10. That is pretty much an arbitrary call,
as has been mentioned; and as the to_s method can be overridden in
ruby, it's not a problem at all. It's just a suprise if you're
expecting some (arbitrary) percision for the string representation
(e.g., the same percision used in GDB).

Regards,
Jordan

Michal Suchanek

11/30/2007 5:00:00 PM

0

On 30/11/2007, Shot (Piotr Szotkowski) <shot@hot.pl> wrote:

> --
> <Black_Dog> "^\\([^ ()]+\\)\\(([0-9]+\\),\\([0-9]+\\))"
> <Black_Dog> gotta love regexps
> <Bl1tz|work> it looks like some elaborate Japanese smiley
> <Bl1tz|work> like "your parents just found out you've been
> slacking in class and you also have the flu"

ROTFL

where do you get the sigs ? :D