to round off to a certain digit... i tried:
double RoundOff( double d, int precision )
{
int p = precision;
while( p-- )
d *= 10;
d = (int)d;
p = precision;
while( p++ )
d /= 10;
return d;
}
this function seems fine... but it produces the problem i stated...
Uh no this function is something I'm going to hand the guy who tought
me C++ when I went to school so that he has a prime example of how not
work with doubles to show to his students.
If you want to work extensively with floating point types go and read:
http://www.physics.ohio-state.edu/~dws/grouplinks/floating_point_math.pdf
Note that this was written before the standarisation of C++ so you
might need to adapt the code to current standards.
A few things that just pop in why this function is wrong.
What happens if d is larger then 2^31-1 (after multiplication)
What happens if you try a precision greater then 16.
What happens if you feed in a double like 1.345e-5 with a precision
value of 4
How big will roundoff errors be.
And the biggest of all the function doesn't do what you think it should
do. It doesn't hand you a nicely cut to precision length double, it
hands you a (most likely) mangled representation of the double you put
in.