Corretta gestione arrotondamenti usando i long double
git-svn-id: svn://10.65.10.50/trunk@2201 c028cbd2-c16b-5b4b-a496-9718f37d4682
This commit is contained in:
parent
13913bf186
commit
4d2fb08b25
@ -149,6 +149,23 @@ char *real::string (
|
||||
return __string;
|
||||
}
|
||||
|
||||
// Childish algorithm faster and more accurate than powl(10.0, pow)
|
||||
HIDDEN long double ipow10(int pow)
|
||||
{
|
||||
long double n = 1.0;
|
||||
if (pow > 0)
|
||||
{
|
||||
for (int i = pow; i > 0; i--)
|
||||
n *= 10.0;
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = pow; i < 0; i++)
|
||||
n *= 0.1;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
// @mfunc real& | real | round | Arrotonda al numero di decimali passati
|
||||
real& real::round (
|
||||
int prec) // @parm Numero di decimali a cui arrotondare il numero (default 0)
|
||||
@ -161,8 +178,8 @@ real& real::round (
|
||||
{
|
||||
long double p = 1.0;
|
||||
if (prec != 0)
|
||||
{
|
||||
p = powl (10.0, prec);
|
||||
{
|
||||
p = ipow10(prec);
|
||||
_dec *= p;
|
||||
}
|
||||
_dec = floorl(_dec + 0.5);
|
||||
@ -177,7 +194,7 @@ real& real::ceil (int prec)
|
||||
long double p = 1.0;
|
||||
if (prec != 0)
|
||||
{
|
||||
p = powl(10.0, prec);
|
||||
p = ipow10(prec);
|
||||
_dec *= p;
|
||||
}
|
||||
_dec = ceill(_dec);
|
||||
@ -192,7 +209,7 @@ real& real::trunc(int prec)
|
||||
long double p = 1.0;
|
||||
if (prec != 0)
|
||||
{
|
||||
p = powl(10.0, prec);
|
||||
p = ipow10(prec);
|
||||
_dec *= p;
|
||||
}
|
||||
_dec = floorl(_dec);
|
||||
|
Loading…
x
Reference in New Issue
Block a user