precision of a double ?
Author Message precision of a double ?

hi,

I'm looking for a function such as void setprecision(int precision , double
in, double *out ) .
this function can set the precision  of a double  (numbre of decimals ).
for exemple , a double "35.425" and a precision "1" are the inputs ; the
output is "35.4".
thank you.
rhim.
--

Sun, 02 Feb 2003 03:00:00 GMT  precision of a double ?

Quote:
>I'm looking for a function such as void setprecision(int precision , double
>in, double *out ) .

This is a lousy interface for such a function.  Something more
sensible would be double round(double x, int decimals).

Quote:
>this function can set the precision  of a double  (numbre of decimals ).
>for exemple , a double "35.425" and a precision "1" are the inputs ; the
>output is "35.4".

double round(double x, int decimals)
{
double powten = pow(10, decimals);

return floor(x * powten + 0.5) / powten;
}

However, don't expect

printf("%f\n", round(35.425, 1));

to display "35.4".  This number cannot be exactly represented in binary
floating point, just as 1/3 cannot be exactly represented in decimal.

OTOH,

printf("%.1f\n", 35.425);

will (most likely) display "35.4", even without using round().

Dan
--
Dan Pop
CERN, IT Division

Mail:  CERN - IT, Bat. 31 1-014, CH-1211 Geneve 23, Switzerland
--

Mon, 03 Feb 2003 03:00:00 GMT  precision of a double ?
thank you Dan .
your funtion works , but I always have the zeros .
if the precsion is 1 and the input    : 3.4587  then
the out put : 3.4000
can I have 3.4 instead of 3.4000

rhim.

Quote:

> >I'm looking for a function such as void setprecision(int precision ,
double
> >in, double *out ) .

> This is a lousy interface for such a function.  Something more
> sensible would be double round(double x, int decimals).

> >this function can set the precision  of a double  (numbre of decimals ).
> >for exemple , a double "35.425" and a precision "1" are the inputs ; the
> >output is "35.4".

>     double round(double x, int decimals)
>     {
> double powten = pow(10, decimals);

> return floor(x * powten + 0.5) / powten;
>     }

> However, don't expect

>     printf("%f\n", round(35.425, 1));

> to display "35.4".  This number cannot be exactly represented in binary
> floating point, just as 1/3 cannot be exactly represented in decimal.

> OTOH,

>     printf("%.1f\n", 35.425);

> will (most likely) display "35.4", even without using round().

> Dan
> --
> Dan Pop
> CERN, IT Division

> Mail:  CERN - IT, Bat. 31 1-014, CH-1211 Geneve 23, Switzerland
> --

--

Tue, 04 Feb 2003 03:00:00 GMT  precision of a double ?

Quote:

> I'm looking for a function such as void setprecision(int precision , double
> in, double *out ) .
> this function can set the precision  of a double  (numbre of decimals ).
> for exemple , a double "35.425" and a precision "1" are the inputs ; the
> output is "35.4".

This is usually called "rounding".  It's really not very hard:
#include <assert.h>
#include <math.h>
double round2mod(double x, double module) {
if (module == 0) return x;
assert(module > 0);
if (x < 0) return -round2mod(-x, module);
return x + module/2 - fmod(x + module/2, module);
}
void setprecision(int precision, double in, double *out) {
*out = round2mod(in, pow(10, -precision));
}
--

Tue, 04 Feb 2003 03:00:00 GMT  precision of a double ?

Quote:

> This is a lousy interface for such a function.  Something more
> sensible would be double round(double x, int decimals).

I agree with the sentiment but not with the function name.
round is a standard <math.h> function.
--

Tue, 04 Feb 2003 03:00:00 GMT  precision of a double ?

Quote:
>thank you Dan .
>your funtion works , but I always have the zeros .
>if the precsion is 1 and the input    : 3.4587  then
>the out put : 3.4000
>can I have 3.4 instead of 3.4000

Yup.  Just use %.1f in your printf call.  Or, if the precision is
variable, %.*f and include the precision as an argument, right before
the value you want displayed.

Dan
--
Dan Pop
CERN, IT Division

Mail:  CERN - IT, Bat. 31 1-014, CH-1211 Geneve 23, Switzerland
--

Fri, 07 Feb 2003 03:00:00 GMT

 Page 1 of 1 [ 6 post ]

Relevant Pages