# BigDecimal + Float =\> Float?

**URL:** <https://www.ruby-forum.com/t/bigdecimal-float-float/93352>\
**Category:** Rails\
**Created:** [March 31, 2006, 4:01pm UTC](https://www.ruby-forum.com/t/bigdecimal-float-float/93352 "2006-03-31T16:01:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ashley\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@Ashley\_M](https://www.ruby-forum.com/u/Ashley_M)\
**Post date:** [March 31, 2006, 4:01pm UTC](https://www.ruby-forum.com/t/bigdecimal-float-float/93352/1 "2006-03-31T16:01:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Ashley\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@Ashley\_M](https://www.ruby-forum.com/u/Ashley_M)\
**Post date:** [March 31, 2006, 4:17pm UTC](https://www.ruby-forum.com/t/bigdecimal-float-float/93352/2 "2006-03-31T16:17:05Z")

</div>

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](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid  
+1 513-295-4739

---

<div class="post-metadata">

**Author:** ![Ashley\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@Ashley\_M](https://www.ruby-forum.com/u/Ashley_M)\
**Post date:** [March 31, 2006, 4:26pm UTC](https://www.ruby-forum.com/t/bigdecimal-float-float/93352/3 "2006-03-31T16:26:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Ashley\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@Ashley\_M](https://www.ruby-forum.com/u/Ashley_M)\
**Post date:** [March 31, 2006, 4:26pm UTC](https://www.ruby-forum.com/t/bigdecimal-float-float/93352/4 "2006-03-31T16:26:26Z")

</div>

I’ve also just realised I meant to post this to ruby-talk 🙂

---

<div class="post-metadata">

**Author:** ![Ashley\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@Ashley\_M](https://www.ruby-forum.com/u/Ashley_M)\
**Post date:** [March 31, 2006, 4:47pm UTC](https://www.ruby-forum.com/t/bigdecimal-float-float/93352/5 "2006-03-31T16:47:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Ashley\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@Ashley\_M](https://www.ruby-forum.com/u/Ashley_M)\
**Post date:** [May 3, 2006, 6:58pm UTC](https://www.ruby-forum.com/t/bigdecimal-float-float/93352/6 "2006-05-03T18:58:12Z")

</div>

> 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](http://www.rubyforge.org) for  
this.

Best regards,

Karl

---

<div class="post-metadata">

**Author:** ![Ashley\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@Ashley\_M](https://www.ruby-forum.com/u/Ashley_M)\
**Post date:** [March 31, 2006, 4:32pm UTC](https://www.ruby-forum.com/t/bigdecimal-float-float/93352/7 "2006-03-31T16:32:29Z")

</div>

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
