Quick Float question

C

Cliff Rowley

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 :)

Cliff
 
H

Hank Gong

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
 
C

Cliff Rowley

Hank said:
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
 
G

Gregory Brown

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.

assert_in_delta 3.3333333333, 10.0/3.0, 0.0001

-greg
 

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
473,774
Messages
2,569,598
Members
45,152
Latest member
LorettaGur
Top