[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

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

Greg Hauptmann

11/8/2008 6:26:00 AM

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
=> #<BigDecimal:2395e98,'0.65E1',8(8)>
>> x
=> 6.5
>> a == x
=> false #<<== DIDN'T WORK
>>

tks

7 Answers

Michael Morin

11/8/2008 7:03:00 AM

0

Greg Hauptmann wrote:
> 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
> => #<BigDecimal:2395e98,'0.65E1',8(8)>
>>> x
> => 6.5
>>> a == x
> => 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.

Greg Hauptmann

11/8/2008 7:45:00 AM

0

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

>> a = BigDecimal.new('6.5')
=> #<BigDecimal:238875c,'0.65E1',8(8)>
>> b = 6.5
=> 6.5
>> a == b
=> false
>> a.class
=> BigDecimal
>> b.class
=> Float
>>

On Sat, Nov 8, 2008 at 5:03 PM, Michael Morin <uzimonkey@gmail.com> wrote:
> Greg Hauptmann wrote:
>>
>> 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
>> => #<BigDecimal:2395e98,'0.65E1',8(8)>
>>>>
>>>> x
>>
>> => 6.5
>>>>
>>>> a == x
>>
>> => 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.
>
>

Robert Parker

11/8/2008 7:56:00 AM

0

On Sat, Nov 8, 2008 at 2:44 PM, Greg Hauptmann
<greg.hauptmann.ruby@gmail.com> wrote:
> that works - but my 6.5 number doesn't work - does the following also
> not work for you?
>
>>> a = BigDecimal.new('6.5')
> => #<BigDecimal:238875c,'0.65E1',8(8)>
>>> b = 6.5
> => 6.5
>>> a == b
> => false
>>> a.class
> => BigDecimal
>>> b.class
> => Float
>>>
>

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.
--
In a world without walls who needs Windows (or Gates)? Try Linux instead!

pen

11/8/2008 8:12:00 PM

0

On Nov 8, 8:26 am, Greg Hauptmann <greg.hauptmann.r...@gmail.com>
wrote:
> 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

Greg Hauptmann

11/8/2008 8:23:00 PM

0

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

On Sun, Nov 9, 2008 at 6:12 AM, pen <pyeniemi@gmail.com> wrote:
> On Nov 8, 8:26 am, Greg Hauptmann <greg.hauptmann.r...@gmail.com>
> wrote:
>> 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
>
>

Michael Morin

11/10/2008 7:28:00 AM

0

Greg Hauptmann wrote:
> oh so convert the BigDecimal to a float by multiplying by 1.0 you mean first?
>
> On Sun, Nov 9, 2008 at 6:12 AM, pen <pyeniemi@gmail.com> wrote:
>> On Nov 8, 8:26 am, Greg Hauptmann <greg.hauptmann.r...@gmail.com>
>> wrote:
>>> 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
>>
>>
>

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

Mark Thomas

11/10/2008 4:47:00 PM

0

On Nov 8, 1:26 am, Greg Hauptmann <greg.hauptmann.r...@gmail.com>
wrote:
> 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
> => #<BigDecimal:2395e98,'0.65E1',8(8)>>> x
> => 6.5
> >> a == x
>
> => 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/bc1e54...

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.