double or float error

K

Kelvin

Hi:
I have
double d = 0.33

when I try to display this using:
cout << d;
it gives me 0.3329999999999999
i know it's caused by the way that computer stores float number...
but how can i get exactly 0.33 without using printf like arguement ??

thank you
 
P

Prawit Chaivong

Kelvin said:
Hi:
I have
double d = 0.33

when I try to display this using:
cout << d;
it gives me 0.3329999999999999
i know it's caused by the way that computer stores float number...
but how can i get exactly 0.33 without using printf like arguement ??

thank you

Hi Kelvin,
What's your compiler?
I've tested by using gcc3.2.3, the result of both cout and printf
are the same.
 
V

velthuijsen

GAH, must have been dreaming or something
This is wrong:
int OldWidth = cout.width(2);
cout << d;
cout. width(OldWidth);

should be:

int OldPrecision = cout.precision(3);
cout << d;
cout.precision(OldPrecision);

Note that this limits the total number of digits shown of a floating
point value. so if you get a value like 123.456 you get 123 not 123.456
 
K

Kelvin

Prawit Chaivong said:
Hi Kelvin,
What's your compiler?
I've tested by using gcc3.2.3, the result of both cout and printf
are the same.

Hi Prawit:
I'm using the VC 2005 express...
cout << d; is just an example that would show my problem in the simplist
way...

i'm lookin for precision control in C++... which i just couldn't find
anywhere...

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...
 
C

Chris Theis

Kelvin said:
Hi:
I have
double d = 0.33

when I try to display this using:
cout << d;
it gives me 0.3329999999999999
i know it's caused by the way that computer stores float number...
but how can i get exactly 0.33 without using printf like arguement ??

thank you

You can use the std::setprecision stream manipulator.

cout << std::setprecision(2) << d << endl; // use 2 or whatever
precision you want

HTH
Chris
 
V

velthuijsen

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top