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
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 On Mar 31, 2006, at 9:00 AM, 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. > > Ashley > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
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. > > -RobI 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
I''ve also just realised I meant to post this to ruby-talk :)
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
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
> 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