Re: Rounding a floating point number
- From: Keith Thompson <kst-u@xxxxxxx>
- Date: Mon, 25 Feb 2008 09:43:34 -0800
jacob navia <jacob@xxxxxxxxxx> writes:
"How can I round a number to x decimal places" ?
This question keeps appearing. I would propose the following
solution
Yes, the question keeps appearing. The best response, IMHO, is to ask
the questioner why he wants to do that.
#include <float.h>
#include <math.h>
double roundto(double x, int digits)
{
int sgn=1;
if (x == 0)
return 0;
if (digits > DBL_DIG)
digits = DBL_DIG;
else if (digits < 1)
digits = 1;
Silently changing any ``digits'' value less than 1 to 1 seems like a
bad idea. For example, rounding to 0 digits is perfectly sensible;
roundto(1.234, 0) should return 1.0. For that matter,
roundto(123456.0, -2) should probably return 123400.0.
I haven't thought much about whether values greater than DBL_DIG might
make sense in some cases. You might just return the value of x
directly.
if(x < 0.0) {
sgn = -sgn;
x = -x;
}
double p = floorl(log10l(x));
Why do you use floorl and log10l in both roundto and roundtol?
It's worth mentioning that floorl and long10l are new in C99, and may
not be supported by all implementations.
A solution not using transcendental functions *might* be faster.
p--;
p = digits-p;
double pow10 = pow(10.0, p);
You're also mixing declarations and statements, which is perfectly
legal in C99, but may not be supported by all compilers. If this were
to be added to the FAQ, either this should be mentioned or all
declarations should be moved to the beginning of the block.
It's very possible that computing 10**p by repeated multiplication is
going to be faster than calling pow(), which is a transcendental
function. (It's also possible that some pow() implementations
optimize the case where the second argument is equal to an integer
value; I haven't checked.)
It would be nice if the standard library provided a variant of the
pow() function whose second parameter is an integer (type int is
probably adequate).
return sgn*floor(x*pow10+0.5)/pow10;[similar code snipped]
}
long double roundtol(long double x, int digits)
#include <stdio.h>
int main(void)
{
double d = 1.7888889988996678;
long double ld = 1.7888889988996678998877L;
for (int i = 0; i<=DBL_DIG;i++) {
printf("i=%d: %.15g\n",i,roundto(d,i));
}
printf("\n 1.7888889988996678\n");
for (int i = 0; i<=LDBL_DIG;i++) {
printf("i=%d: %.18Lg\n",i,roundtol(ld,i));
}
printf("\n 1.7888889988996678998877L\n");
return 0;
}
--------------------------------------------------------------------
I would propose it to add it to the FAQ.
FAQ 14.6 proposes
(int)(x / precision + 0.5) * precision
Your roundto(x, 2) should be equivalent to the above with
precision==0.01. On the other hand, the FAQ's solution fails when (x
/ precision + 0.5) is outside the range of type int.
I compiled and ran your program and got:
i=0: 1.79
i=1: 1.79
i=2: 1.789
i=3: 1.7889
[...]
I've already pointed out your odd treatment of i=0. I think you have
an off-by-one error as well.
Finally, I question the usefulness of this entire approach (and the
following is something that probably should be added to the FAQ).
(And didn't we discuss this at some length a while ago?)
To take a concrete example, rounding the value 1.2345 to 2 decimal
places cannot be done exactly in binary floating-point. On one
system, the actual stored value for 1.23 is
1.229999999999999982236431605997495353221893310546875
Certainly most of those digits are not significant; the point is that
1.23 cannot be stored exactly, and that in this case it's
mathematically less than 1.23.
What is the purpose of rounding a floating-point value to N decimal
places? 99% of the time [*], the only purpose is to provide a textual
representation of the number. Storing an approximation of the rounded
value in a floating-point representation is not the best way to
accomplish that. Just pass the original *unrounded* value to printf
(or fprintf, or sprintf) with an appropriate format:
printf("%.2f\n", 1.2345);
Attempting to convert 1.2345 to a floating-point representation that
closely approximates the value 1.23 merely wastes CPU cyles and
introduces more possiblities for error.
The question of rounding a floating-point number to a specified number
of decimal places is an opportunity to teach the questioner a thing or
two about how floating-point arithmetic works. By accepting the
premise of the question, you pass up this opportunity.
There may well be cases where the kind of internal rounding you
describe is useful (perhaps in financial calculations). In such
cases, it might make more sense to store the value in fixed-point (a
scaled integer), or to keep the unrounded value in a structure along
with an integer specifying the rounding to be applied later. Some,
but by not means all, financial problems can be solved by storing the
number of cents rather than the number of dollars (or euros, or
whatever). In the remaining cases where you really need a
floating-point approximation of the rounded value, it's likely that a
specific algorithm is required by financial regulations.
In however many cases remain after that, either the solution proposed
in the FAQ or your solution (with corrections) could be useful.
If you can present a non-artificial case where computing an
approximate floating-point rounded result, rather than doing the
rounding on output, is actually required, I'd be interested in seeing
it.
[*] Yes, I made up the 99% figure, but it's not an unreasonable guess.
--
Keith Thompson (The_Other_Keith) <kst-u@xxxxxxx>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
.
- References:
- Rounding a floating point number
- From: jacob navia
- Rounding a floating point number
- Prev by Date: http response parser in c
- Next by Date: Re: Converting unsigned long to string in C
- Previous by thread: Re: Rounding a floating point number
- Next by thread: Re: Rounding a floating point number
- Index(es):
Relevant Pages
|