# FloatDomainError while rounding

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.sumMath::log10(@a):@extra.sumMath::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)

And I am facing an " ‘round’: Infinity (FloatDomainError)". How can I
fix it.

Thanks for any Hints.
Raj

On Tue, Sep 18, 2012 at 2:04 PM, raj g. [email protected] wrote:

@num = @extra.last==0 ?

And I am facing an " ‘round’: Infinity (FloatDomainError)". How can I
fix it.

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
from (irb):24:in `round’

Jesus.

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.

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

On Tue, Sep 18, 2012 at 4:23 PM, raj g. [email protected] 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.

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.

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

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 18.09.2012 16:51, Alex G. 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
from (irb):23:in `round’
from (irb):23

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.

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.

On 20.09.2012 05:04, raj g. wrote:

any solution for this to avoid this FloatDoaminError?
Are you really sure that such a large number is the correct output from

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

http://www.ruby-doc.org/stdlib-1.9.3/libdoc/bigdecimal/rdoc/BigDecimal.html

Hi Alex,

Thanks for that I’ll definitely try it out.

Raj

Alex G. wrote in post #1076502:

On 18.09.2012 16:51, Alex G. 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
from (irb):23:in `round’
from (irb):23

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