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.
A90204c955db033cd975f7bb0ec9600b?d=identicon&s=25 Ashley Moran (Guest)
on 2006-03-31 16: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
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2006-03-31 16: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 Biedenharn		http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
+1 513-295-4739
A90204c955db033cd975f7bb0ec9600b?d=identicon&s=25 Ashley Moran (Guest)
on 2006-03-31 16:26
(Received via mailing list)
On Friday 31 March 2006 15:15, Rob Biedenharn 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
A90204c955db033cd975f7bb0ec9600b?d=identicon&s=25 Ashley Moran (Guest)
on 2006-03-31 16:26
(Received via mailing list)
I've also just realised I meant to post this to ruby-talk :)
Eea7ad39737b0dbf3de38874e0a6c7d8?d=identicon&s=25 Justin Forder (Guest)
on 2006-03-31 16:32
(Received via mailing list)
Ashley Moran 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
A90204c955db033cd975f7bb0ec9600b?d=identicon&s=25 Ashley Moran (Guest)
on 2006-03-31 16:47
(Received via mailing list)
On Friday 31 March 2006 15:29, Justin Forder 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
F9eaf9f5508d6ce31cf779445d77d2af?d=identicon&s=25 Karl Brodowsky (Guest)
on 2006-05-03 18: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.