# FloatDomainError while rounding

**URL:** <https://rubytalk.org/t/floatdomainerror-while-rounding/66308>\
**Category:** ruby-talk\
**Created:** [18 September 2012 12:04 UTC](https://rubytalk.org/t/floatdomainerror-while-rounding/66308 "2012-09-18T12:04:03Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [18 September 2012 12:04 UTC](https://rubytalk.org/t/floatdomainerror-while-rounding/66308/1 "2012-09-18T12:04:03Z")

</div>

I am Trying to Round a number in my code which is as follows

> **···**
>
> -------------------------------------------  
> param :a, 2, 5  
> param :b, 10, 49  
> param :extra1, 0, 3  
> @extra2 = @extra1 == 0 ? (1..3).choose : 0  
> @extra = [@extra1 , @extra2]  
> @coeff = []  
> (@extra.size).times{|i| @coeff[i] = @extra[i] == 0 ? (2..3).choose : 1 }  
> @num = @extra.last==0 ?  
> @extra.sum\*Math::log10(@a):@extra.sum\*Math::log10(@b)
> 
> @den1 = @coeff.first \* Math::log10(@a)  
> @den2 = @coeff.last \* Math::log10(@b)
> 
> @answer = @extra.last == 0 ? (-1\*@num)/(@den1-@den2) :  
> (@num)/(@den1-@den2)  
> @answer = (@answer\*100).round/100.to\_f
> 
> ------------------------------------------------
> 
> And I am facing an " 'round': Infinity (FloatDomainError)". How can I  
> fix it.
> 
> Thanks for any Hints.  
> Raj
> 
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Jesus\_Gabriel\_y\_Gala](https://avatars.discourse-cdn.com/v4/letter/j/96bed5/32.png) [@Jesus\_Gabriel\_y\_Gala](https://rubytalk.org/u/Jesus_Gabriel_y_Gala)\
**Post date:** [18 September 2012 12:11 UTC](https://rubytalk.org/t/floatdomainerror-while-rounding/66308/2 "2012-09-18T12:11:31Z")

</div>

Can you print @den1 and @den2 after setting them? If they are equal,  
then the divisor will be 0, hence the Infinity error.

1.9.2p290 :023 \> a = 1.0/0  
=\> Infinity  
1.9.2p290 :024 \> (a \* 100).round  
FloatDomainError: Infinity  
&nbsp;&nbsp;from (irb):24:in `round'

Jesus.

> **···**
>
> On Tue, Sep 18, 2012 at 2:04 PM, raj g. \<lists@ruby-forum.com\> wrote:
> 
> > I am Trying to Round a number in my code which is as follows
> > 
> > -------------------------------------------  
> > param :a, 2, 5  
> > param :b, 10, 49  
> > param :extra1, 0, 3  
> > @extra2 = @extra1 == 0 ? (1..3).choose : 0  
> > @extra = [@extra1 , @extra2]  
> > @coeff =   
> > (@extra.size).times{|i| @coeff[i] = @extra[i] == 0 ? (2..3).choose : 1 }  
> > @num = @extra.last==0 ?  
> > @extra.sum\*Math::log10(@a):@extra.sum\*Math::log10(@b)
> > 
> > @den1 = @coeff.first \* Math::log10(@a)  
> > @den2 = @coeff.last \* Math::log10(@b)
> > 
> > @answer = @extra.last == 0 ? (-1\*@num)/(@den1-@den2) :  
> > (@num)/(@den1-@den2)  
> > @answer = (@answer\*100).round/100.to\_f
> > 
> > ------------------------------------------------
> > 
> > And I am facing an " 'round': Infinity (FloatDomainError)". How can I  
> > fix it.

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [18 September 2012 14:23 UTC](https://rubytalk.org/t/floatdomainerror-while-rounding/66308/3 "2012-09-18T14:23:36Z")

</div>

Yes I have printed @den1 and @den2 but both of them are not same. even  
than I face the same problem when I tried to round @answer.

&nbsp;&nbsp;&nbsp;If I print @answer without rounding I am not getting the error and  
getting a Number with long floating point Which is not required for me.

Raj

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [20 September 2012 10:33 UTC](https://rubytalk.org/t/floatdomainerror-while-rounding/66308/4 "2012-09-20T10:33:59Z")

</div>

Hi Alex,

&nbsp;&nbsp;&nbsp;Thanks for that I'll definitely try it out.

Raj

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Jesus\_Gabriel\_y\_Gala](https://avatars.discourse-cdn.com/v4/letter/j/96bed5/32.png) [@Jesus\_Gabriel\_y\_Gala](https://rubytalk.org/u/Jesus_Gabriel_y_Gala)\
**Post date:** [18 September 2012 15:08 UTC](https://rubytalk.org/t/floatdomainerror-while-rounding/66308/5 "2012-09-18T15:08:16Z")

</div>

OK. Let's simplify. Can you show the minimal piece of code that  
produces the error. Like:

a = ?  
b = ?  
coeff = [?,?]  
num = ?

den1 = coeff.first \* Math::log10(a)  
den2 = coeff.last \* Math::log10(b)  
p den1  
p den2  
answer = num/(den1-den2)  
p answer  
answer = (answer\*100).round/100.to\_f

Can you assign some example values to the variables (where I placed  
the ?) from your program, run this and show us the output?  
BTW, I'm not sure you need instance variables (variables starting with  
@). Unless you are actually inside an instance of a class and you need  
to keep all those values around.

Jesus.

> **···**
>
> On Tue, Sep 18, 2012 at 4:23 PM, raj g. \<lists@ruby-forum.com\> wrote:
> 
> > Yes I have printed @den1 and @den2 but both of them are not same. even  
> > than I face the same problem when I tried to round @answer.
> > 
> > &nbsp;&nbsp;&nbsp;If I print @answer without rounding I am not getting the error and  
> > getting a Number with long floating point Which is not required for me.

---

<div class="post-metadata">

**Author:** ![Alex\_Gutteridge1](https://avatars.discourse-cdn.com/v4/letter/a/58f4c7/32.png) [@Alex\_Gutteridge1](https://rubytalk.org/u/Alex_Gutteridge1)\
**Post date:** [18 September 2012 15:51 UTC](https://rubytalk.org/t/floatdomainerror-while-rounding/66308/6 "2012-09-18T15:51:56Z")

</div>

In that case @num must be Infinity (or -Infinity). Possibly @a or @b are 0 and so the logarithms you are calling earlier generate -Infinity. Either way, as Jesus says the simplest thing is to just print out all the intermediate variables until you see what's wrong.

> **···**
>
> On 18.09.2012 15:23, raj g. wrote:
> 
> > Yes I have printed @den1 and @den2 but both of them are not same. even  
> > than I face the same problem when I tried to round @answer.
> > 
> > &nbsp;&nbsp;&nbsp;If I print @answer without rounding I am not getting the error and  
> > getting a Number with long floating point Which is not required for me.
> > 
> > Raj
> 
> --  
> Alex Gutteridge

---

<div class="post-metadata">

**Author:** ![Alex\_Gutteridge1](https://avatars.discourse-cdn.com/v4/letter/a/58f4c7/32.png) [@Alex\_Gutteridge1](https://rubytalk.org/u/Alex_Gutteridge1)\
**Post date:** [18 September 2012 15:58 UTC](https://rubytalk.org/t/floatdomainerror-while-rounding/66308/7 "2012-09-18T15:58:57Z")

</div>

> > Yes I have printed @den1 and @den2 but both of them are not same. even  
> > than I face the same problem when I tried to round @answer.
> > 
> > &nbsp;&nbsp;&nbsp;If I print @answer without rounding I am not getting the error and  
> > getting a Number with long floating point Which is not required for me.
> > 
> > Raj
> 
> In that case @num must be Infinity (or -Infinity). Possibly @a or @b  
> are 0 and so the logarithms you are calling earlier generate  
> -Infinity. Either way, as Jesus says the simplest thing is to just  
> print out all the intermediate variables until you see what's wrong.

Apologies, I didn't read your second paragraph closely enough. If your number is above the Float maximum limit it will also die with this error (so not necessarily Infinity, but a large number):

> > (Float::MAX\_10\_EXP).round

=\> 308

> > (1E308).round

=\> 100000000000000001097906362944045541740492309677311846336810682903157585404911491537163328978494688899061249669721172515611590283743140088328307009198146046031271664502933027185697489699588559043338384466165001178426897626212945177628091195786707458122783970171784415105291802893207873272974885715430223118336

> > (1E309).round

(irb):23: warning: Float 1E309 out of range  
FloatDomainError: Infinity  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):23:in `round'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):23

> **···**
>
> On 18.09.2012 16:51, Alex Gutteridge wrote:
> 
> > On 18.09.2012 15:23, raj g. wrote:
> 
> --  
> Alex Gutteridge

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [20 September 2012 04:04 UTC](https://rubytalk.org/t/floatdomainerror-while-rounding/66308/8 "2012-09-20T04:04:17Z")

</div>

Alex Gutteridge wrote in post #1076502:

> > > Raj
> > 
> > In that case @num must be Infinity (or -Infinity). Possibly @a or @b  
> > are 0 and so the logarithms you are calling earlier generate  
> > -Infinity. Either way, as Jesus says the simplest thing is to just  
> > print out all the intermediate variables until you see what's wrong.
> 
> Apologies, I didn't read your second paragraph closely enough. If your  
> number is above the Float maximum limit it will also die with this error  
> (so not necessarily Infinity, but a large number):
> 
> > > (Float::MAX\_10\_EXP).round
> 
> =\> 308
> 
> > > (1E308).round
> 
> =\>

100000000000000001097906362944045541740492309677311846336810682903157585404911491537163328978494688899061249669721172515611590283743140088328307009198146046031271664502933027185697489699588559043338384466165001178426897626212945177628091195786707458122783970171784415105291802893207873272974885715430223118336

> > > (1E309).round
> 
> (irb):23: warning: Float 1E309 out of range  
> FloatDomainError: Infinity  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):23:in `round'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):23

Thanks for the explanation. So How can we avoid this problem? Is there  
any solution for this to avoid this FloatDoaminError?

> **···**
>
> > On 18.09.2012 16:51, Alex Gutteridge wrote:
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Alex\_Gutteridge1](https://avatars.discourse-cdn.com/v4/letter/a/58f4c7/32.png) [@Alex\_Gutteridge1](https://rubytalk.org/u/Alex_Gutteridge1)\
**Post date:** [20 September 2012 08:22 UTC](https://rubytalk.org/t/floatdomainerror-while-rounding/66308/9 "2012-09-20T08:22:40Z")

</div>

Are you really sure that such a large number is the correct output from your algorithm?

If so, then AFAIK your only option is to use BigDecimal in your calculations rather than Float:

> **[Class: BigDecimal (Ruby 1.9.3)](https://ruby-doc.org/stdlib-1.9.3/libdoc/bigdecimal/rdoc/BigDecimal.html)**
>
> Class : BigDecimal - Ruby 1.9.3

> **···**
>
> On 20.09.2012 05:04, raj g. wrote:
> 
> > Alex Gutteridge wrote in post #1076502:
> > 
> > > On 18.09.2012 16:51, Alex Gutteridge wrote:
> > > 
> > > > > Raj
> > > > 
> > > > In that case @num must be Infinity (or -Infinity). Possibly @a or @b  
> > > > are 0 and so the logarithms you are calling earlier generate  
> > > > -Infinity. Either way, as Jesus says the simplest thing is to just  
> > > > print out all the intermediate variables until you see what's wrong.
> > > 
> > > Apologies, I didn't read your second paragraph closely enough. If your  
> > > number is above the Float maximum limit it will also die with this error  
> > > (so not necessarily Infinity, but a large number):
> > > 
> > > > > (Float::MAX\_10\_EXP).round
> > > 
> > > =\> 308
> > > 
> > > > > (1E308).round
> > > 
> > > =\>
> > 
> > 100000000000000001097906362944045541740492309677311846336810682903157585404911491537163328978494688899061249669721172515611590283743140088328307009198146046031271664502933027185697489699588559043338384466165001178426897626212945177628091195786707458122783970171784415105291802893207873272974885715430223118336
> > 
> > > > > (1E309).round
> > > 
> > > (irb):23: warning: Float 1E309 out of range  
> > > FloatDomainError: Infinity  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):23:in `round'  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):23
> > 
> > Thanks for the explanation. So How can we avoid this problem? Is there  
> > any solution for this to avoid this FloatDoaminError?
> 
> --  
> Alex Gutteridge
