what's easiest way to compare a Float & BigDecimal (i.e. like aequals mechanism)

G

Greg Hauptmann

Hi,

what's easiest way to compare a Float & BigDecimal (i.e. like a equals
mechanism)? It seems that "==" does NOT work as they are different
types. Is there a way to do a comparison without having to convert
types?

?> a

tks
 
M

Michael Morin

Greg said:
Hi,

what's easiest way to compare a Float & BigDecimal (i.e. like a equals
mechanism)? It seems that "==" does NOT work as they are different
types. Is there a way to do a comparison without having to convert
types?

?> a

=> false #<<== DIDN'T WORK

tks


It works fine for me. If I do something like this:

f = 0.1
b = BigDecimal.new('0.1')
puts 'yes' if f == b

It will return true. I've always stayed clear of the equality operator
with floating point numbers. Floating point cannot represent all
numbers exactly, so comparing it with something like a BigDecimal (which
can represent all numbers exactly) might be problematic.
 
G

Greg Hauptmann

that works - but my 6.5 number doesn't work - does the following also
not work for you?
 
R

Robert Parker

that works - but my 6.5 number doesn't work - does the following also
not work for you?

You can rely on comparing floating point numbers for less than or
greater than only.
You can not reliably compare floating point numbers for equality period.
 
P

pen

Hi,

what's easiest way to compare a Float & BigDecimal (i.e. like a equals
mechanism)?  It seems that "==" does NOT work as they are different
types.  Is there a way to do a comparison without having to convert
types?

Actually, there is none. Why don't you just let Ruby do the job and
multiply the bigdecimal with 1.0 when comparing? Also, as previously
noted, == operator with floats is not ... reliable.

br,

pen
 
G

Greg Hauptmann

oh so convert the BigDecimal to a float by multiplying by 1.0 you mean first?
 
M

Michael Morin

Greg said:
oh so convert the BigDecimal to a float by multiplying by 1.0 you mean first?

BigDecimal objects should have a to_f method. Using that would probably
be more clear than multiplying by 1.0 (a seamingly meaningless thing to do).
 
M

Mark Thomas

Hi,

what's easiest way to compare a Float & BigDecimal (i.e. like a equals
mechanism)?  It seems that "==" does NOT work as they are different
types.  Is there a way to do a comparison without having to convert
types?

?> a


=> false      #<<== DIDN'T WORK

I thought I saw a similar discussion not too long ago... aha:

http://groups.google.com/group/comp.lang.ruby/msg/bc1e54150d1d45d5

With the approx_equal? method in that thread, this works:

a = BigDecimal.new('6.5')
f = 6.5
f == a
#=> false
f.approx_equal?(a, 0.00000000001)
#=> true


-- Mark.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top