Quick Float question

I'm doing some math with Ruby and I'd really like to know why:

irb(main):002:0> 10.0 / 3.0
=> 3.33333333333333

but

irb(main):003:0> (10.0 / 3.0) == 3.33333333333333

is

=> false

Thanks :slight_smile:

Cliff

This is because the 3.33333333333333 is not the exact value you got
from 10.0/3.0. Ruby store more information you can't not see.
So if you want to compare the float, you can write it as
(10.0/3.0 - 3.3333333333).abs <= 0.0001

路路路

On Aug 11, 10:10 am, Cliff Rowley <cliffrow...@gmail.com> wrote:

I'm doing some math with Ruby and I'd really like to know why:

irb(main):002:0> 10.0 / 3.0
=> 3.33333333333333

but

irb(main):003:0> (10.0 / 3.0) == 3.33333333333333

is

=> false

Thanks :slight_smile:

Cliff

Hank Gong wrote:

This is because the 3.33333333333333 is not the exact value you got
from 10.0/3.0. Ruby store more information you can't not see.
So if you want to compare the float, you can write it as
(10.0/3.0 - 3.3333333333).abs <= 0.0001
  

This is what I thought. Thanks for clarifying that. I need it primarily for unit testing, I'll try this technique and see how I go.

Thanks again

Cliff

assert_in_delta 3.3333333333, 10.0/3.0, 0.0001

-greg

路路路

On 8/10/07, Cliff Rowley <cliffrowley@gmail.com> wrote:

Hank Gong wrote:
> This is because the 3.33333333333333 is not the exact value you got
> from 10.0/3.0. Ruby store more information you can't not see.
> So if you want to compare the float, you can write it as
> (10.0/3.0 - 3.3333333333).abs <= 0.0001
>
This is what I thought. Thanks for clarifying that. I need it
primarily for unit testing, I'll try this technique and see how I go.

Gregory Brown wrote:

assert_in_delta 3.3333333333, 10.0/3.0, 0.0001

-greg
  

Perfect, thankyou :slight_smile:

Cliff