From: Alex Gutteridge Date: 2012-09-20T17:22:40+09:00 Subject: Re: FloatDomainError while rounding 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 >> 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? 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: http://www.ruby-doc.org/stdlib-1.9.3/libdoc/bigdecimal/rdoc/BigDecimal.html -- Alex Gutteridge