Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
Ruby
Rounding to X digits
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Markus, post: 4446129"] Peeling one layer of the onion, they differ because: irb(main):033:0> printf "%20.15f",(134.45 * (1.0/0.1)) 1344.500000000000000=> nil irb(main):034:0> printf "%20.15f",(134.45/0.1) 1344.499999999999773=> nil I suppose this is not unexpected (my mama warned me 'bout floats) but it is a little unexpected--no, I'm wrong, 1/5 is a repeating decimal base 2, so it's perfectly expected. The tricky bit is, which form (if either of them) will always (or at least, more generally) give the correct result? I can't see off hand that either will be intrinsically "better" but I could be missing a point. I suspect (SWAG) that the hybrid: def to_the_nearest(n) if self.abs < 1.0 (self/n).round/n else m = 1.0/m (self*m).round/m end end would do better than both, but I haven't tested it. -- Markus P.S. Mine has mostly been tested on values < 1.0, thus my suspicion that it works well in that domain. Typing this though, I realize that it was tested FOR CONFORMANCE WITH A PRE-EXISTING SYSTEM* which itself may have been buggy. * Think 25 year old spaghetti FORTRAN, then sigh in bliss realizing that your imagination is much nicer than the ugly facts. [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Ruby
Rounding to X digits
Top