D
David Corby
Hi everybody, I've got a problem.
I'm trying to round a double to a particular number of significant
digits, in this case 5, but I can't figure out a way around getting
_exactly_ what I want instead of _about_ what I want. Taken down to just
the part relevant to my question, here's the code, the desired result,
and the actual result...
double somenumber = round(2453126.7227083333 * 100000.0) / 100000.0;
desired: somenumber = 2453126.72271
actual: somenumber = 2453126.72271099999
I've implemented this exact function in a other languages (Ruby, PHP)
and never run into this problem, so there's got to be SOME kind of
solution. Before I start ripping through Ruby's interpreter's source to
find out how they managed this, I was hoping that somebody who reads
this board has come up with a solution already.
Unfortunately, I need this number in floating-point, or at least I'll
need to be able to reliably convert it to floating-point. Any ideas?
I'm trying to round a double to a particular number of significant
digits, in this case 5, but I can't figure out a way around getting
_exactly_ what I want instead of _about_ what I want. Taken down to just
the part relevant to my question, here's the code, the desired result,
and the actual result...
double somenumber = round(2453126.7227083333 * 100000.0) / 100000.0;
desired: somenumber = 2453126.72271
actual: somenumber = 2453126.72271099999
I've implemented this exact function in a other languages (Ruby, PHP)
and never run into this problem, so there's got to be SOME kind of
solution. Before I start ripping through Ruby's interpreter's source to
find out how they managed this, I was hoping that somebody who reads
this board has come up with a solution already.
Unfortunately, I need this number in floating-point, or at least I'll
need to be able to reliably convert it to floating-point. Any ideas?