From: Douglas A. Gwyn
Subject: Re: precision of a double ?
Date: 18 Aug 2000 00:00:00 GMT
Message-ID: <clcm-20000818-0004@plethora.net>
Newsgroups: comp.lang.c.moderated
"rhim.m" wrote:
> 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));
}
--