[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Ruby doesn't know how to multiply

rjprado@gmail.com

6/21/2007 7:21:00 PM

Hello,

Dear friends, today I have stumbled into a really weird problem. Try
typing this on irb:

14.95 * 0.6 == 8.97

Ruby says it's false!

I don't know if this is a bug. Please let me know. By the way i´m on
ruby 1.8.5. I'll give it a try on 1.8.6 and let you know.

Thanks,
Roberto Prado.

16 Answers

Gregory Seidman

6/21/2007 7:32:00 PM

0

On Fri, Jun 22, 2007 at 04:25:04AM +0900, rjprado@gmail.com wrote:
> Hello,
>
> Dear friends, today I have stumbled into a really weird problem. Try
> typing this on irb:
>
> 14.95 * 0.6 == 8.97
>
> Ruby says it's false!
>
> I don't know if this is a bug. Please let me know. By the way i?m on
> ruby 1.8.5. I'll give it a try on 1.8.6 and let you know.

% irb
>> "%2.40f" % (14.95 * 0.6)
=> "8.9699999999999988631316227838397026062012"
>> "%2.40f" % 8.97
=> "8.9700000000000006394884621840901672840118"

http://en.wikipedia.org/wiki/Roun...

> Thanks,
> Roberto Prado.
--Greg


Gregory Brown

6/21/2007 7:33:00 PM

0

On 6/21/07, rjprado@gmail.com <rjprado@gmail.com> wrote:
> Hello,
>
> Dear friends, today I have stumbled into a really weird problem. Try
> typing this on irb:
>
> 14.95 * 0.6 == 8.97
>
> Ruby says it's false!
>
> I don't know if this is a bug. Please let me know. By the way i´m on
> ruby 1.8.5. I'll give it a try on 1.8.6 and let you know.

It's not a bug. It's the way floating point arithmetic works, and is
true for *most* programming languages.

http://docs.sun.com/source/806-3568/ncg_gol...

James Gray

6/21/2007 7:33:00 PM

0

On Jun 21, 2007, at 2:30 PM, juan pedro meriño wrote:

> I thingh that it is a bug

It's not. It's a typical floating point error:

http://en.wikipedia.org/wiki/Floa...

James Edward Gray II

MenTaLguY

6/21/2007 7:41:00 PM

0

On Fri, 22 Jun 2007 04:25:04 +0900, "rjprado@gmail.com" <rjprado@gmail.com> wrote:
> Dear friends, today I have stumbled into a really weird problem. Try
> typing this on irb:
>
> 14.95 * 0.6 == 8.97
>
> Ruby says it's false!

This happens in all languages that use floating point to represent decimal numbers (you will get precisely the same result in C or Javascript or Perl, for instance). Floating-point arithmetic is only approximate, so the result does not _exactly_ equal 8.97, even though it is very close.

Because of this, when using floating-point arithmetic, testing for exact equality is impractical. The best you can do is test whether the result is within some interval (epsilon) of the expected result:

( ( 14.95 * 0.6 ) - 8.97 ).abs < 1e-6

(Here, we've chosen 1e-6 as our epsilon, which is arbitrary but probably "small enough" in this case. For mathematically intensive code, you may need to be more careful.)

Although floating-point is the default for Ruby, you do have the option of using a different representation of numbers for which arithmetic is exact, although it will not be as fast. One such option is the Rational class in Ruby's standard library, which represents numbers as fractions rather than floating-point numbers.

-mental


Adriano Ferreira

6/21/2007 7:50:00 PM

0

On 6/21/07, juan pedro meriño <juapdiaz@gmail.com> wrote:
> You are rigth James, but you can better reponse why is a floating point
> error?
>

Some numbers (like 0.1) cannot be represented exactly in binary with a
finite number of bits (because 0.1[base 10] =
0.0001100110011001100110011...[base 2]) and when they are represented
(as it happens with floating point representations), they lose
precision. So you must not test floating point for equality but always
rely on some tolerance and use an expression like MenTaLguY showed:

( 14.95 * 0.6 - 8.97 ).abs < tol

where tol depends on your application.

rjprado@gmail.com

6/21/2007 7:50:00 PM

0

On Jun 21, 3:21 pm, "rjpr...@gmail.com" <rjpr...@gmail.com> wrote:
> Hello,
>
> Dear friends, today I have stumbled into a really weird problem. Try
> typing this on irb:
>
> 14.95 * 0.6 == 8.97
>
> Ruby says it's false!
>
> I don't know if this is a bug. Please let me know. By the way i´m on
> ruby 1.8.5. I'll give it a try on 1.8.6 and let you know.
>
> Thanks,
> Roberto Prado.

Yes, you are all right. It's a common problem. I have confirmed this
by doing the same thing on java. Thank you all for your fast replies.

Greetings,
Roberto Prado.

rjprado@gmail.com

6/21/2007 8:00:00 PM

0

On Jun 21, 3:49 pm, "Adriano Ferreira" <a.r.ferre...@gmail.com> wrote:
> On 6/21/07, juan pedro meriño <juapd...@gmail.com> wrote:
>
> > You are rigth James, but you can better reponse why is a floating point
> > error?
>
> Some numbers (like 0.1) cannot be represented exactly in binary with a
> finite number of bits (because 0.1[base 10] =
> 0.0001100110011001100110011...[base 2]) and when they are represented
> (as it happens with floating point representations), they lose
> precision. So you must not test floating point for equality but always
> rely on some tolerance and use an expression like MenTaLguY showed:
>
> ( 14.95 * 0.6 - 8.97 ).abs < tol
>
> where tol depends on your application.

I'm starting to remember something about that on my computer
architecture course. But it's something so technical and I'm so used
to high level programming, that I couldn't remember...


MenTaLguY

6/21/2007 8:27:00 PM

0

On Fri, 22 Jun 2007 05:00:03 +0900, "rjprado@gmail.com" <rjprado@gmail.com> wrote:
> I'm starting to remember something about that on my computer
> architecture course. But it's something so technical and I'm so used
> to high level programming, that I couldn't remember...

I guess it goes to show that one never really escapes this stuff,
even in "high level" programming.

-mental


Lloyd Linklater

6/21/2007 8:58:00 PM

0

> Yes, you are all right. It's a common problem. I have confirmed this
> by doing the same thing on java. Thank you all for your fast replies.

I tried this in Delphi, smalltalk and oracle SQL and got the correct
result.

C# and visual C++ seem to it to be false.

This is most perplexing.

--
Posted via http://www.ruby-....

Rick DeNatale

6/22/2007 7:12:00 PM

0

On 6/21/07, Adriano Ferreira <a.r.ferreira@gmail.com> wrote:
> On 6/21/07, juan pedro meriño <juapdiaz@gmail.com> wrote:
> > You are rigth James, but you can better reponse why is a floating point
> > error?
> >
>
> Some numbers (like 0.1) cannot be represented exactly in binary with a
> finite number of bits (because 0.1[base 10] =
> 0.0001100110011001100110011...[base 2]) and when they are represented
> (as it happens with floating point representations), they lose
> precision

Actually in any base there are fractions which cannot be represented with a
FINITE number of digits. For instance in base 10:

1/3 = 0.33333333333.......


--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...