Forum: Ruby on Rails BigDecimal + Float => Float?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ashley M. (Guest)
on 2006-03-31 18:01
(Received via mailing list)
I was just playing around in irb and noticed that while BigDecimal +
Integer
=> BigDecimal, BigDecimal + Float => Float.  Is there any reason for
this?
It seems inconsistent and means you have to check the class of any
number you
add to a BigDecimal, which looks very un-Ruby-like to me.

Ashley
Rob B. (Guest)
on 2006-03-31 18:17
(Received via mailing list)
I'm guessing that the result didn't *need* a BigDecimal so a Float
was good enough.  Try the same thing with values that are too large
for Float and the result will likely be typed as a BigDecimal.

-Rob


Rob B.		http://agileconsultingllc.com
removed_email_address@domain.invalid
+1 513-295-4739
Ashley M. (Guest)
on 2006-03-31 18:26
(Received via mailing list)
On Friday 31 March 2006 15:15, Rob B. wrote:
> I'm guessing that the result didn't *need* a BigDecimal so a Float  
> was good enough.  Try the same thing with values that are too large  
> for Float and the result will likely be typed as a BigDecimal.
>
> -Rob

I thought of that- I figured maybe it was an optimisation.  But:

irb(main):004:0> (BigDecimal("1.0000000000000000000000000001") +
1.0).class
=> Float

So it looks like it always reduces the available precision.

Ashley
Ashley M. (Guest)
on 2006-03-31 18:26
(Received via mailing list)
I've also just realised I meant to post this to ruby-talk :)
Justin F. (Guest)
on 2006-03-31 18:32
(Received via mailing list)
Ashley M. wrote:
> I was just playing around in irb and noticed that while BigDecimal + Integer
> => BigDecimal, BigDecimal + Float => Float.  Is there any reason for this?
> It seems inconsistent and means you have to check the class of any number you
> add to a BigDecimal, which looks very un-Ruby-like to me.

It seems right to me that when you add something imprecise (Float) to
something precise (BigDecimal) the result must be imprecise.

regards

   Justin
Ashley M. (Guest)
on 2006-03-31 18:47
(Received via mailing list)
On Friday 31 March 2006 15:29, Justin F. wrote:
> It seems right to me that when you add something imprecise (Float) to
> something precise (BigDecimal) the result must be imprecise.

I hadn't thought about it like that - makes sense now.  It depends on
the
context though - a float might represent an exact value, although
arguably
you should store is as a rational if it is.

Ashley
Karl B. (Guest)
on 2006-05-03 20:58
(Received via mailing list)
> I thought of that- I figured maybe it was an optimisation.  But:
>
> irb(main):004:0> (BigDecimal("1.0000000000000000000000000001") + 1.0).class
> => Float
>
> So it looks like it always reduces the available precision.

I would write a bug report in the ruby section of www.rubyforge.org for
this.

Best regards,

Karl
This topic is locked and can not be replied to.