473,508 Members | 2,079 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

double or float error

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
--
{ Kelvin@!!! }
Jul 23 '05 #1
7 1690


Kelvin wrote:
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
--
{ Kelvin@!!! }


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

Jul 23 '05 #2
try
int OldWidth = cout.width(2);
cout << d;
cout. width(OldWidth);

Jul 23 '05 #3
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

Jul 23 '05 #4
"Prawit Chaivong" <pr******@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...


Kelvin wrote:
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
--
{ Kelvin@!!! }


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

--
{ Kelvin@!!! }
Jul 23 '05 #5

"Kelvin" <ch***************@y.a.h.o.o.c.o.m.h.k.donot.spa m> wrote in message
news:l1ype.51998$tt5.50100@edtnps90...
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
Jul 23 '05 #6
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/~d...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.

Jul 23 '05 #7
maybe you should swap the
d/=10 and d*=10

"Kelvin" <ch***************@y.a.h.o.o.c.o.m.h.k.donot.spa m> schrieb im
Newsbeitrag news:l1ype.51998$tt5.50100@edtnps90...
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
--
{ Kelvin@!!! }

Jul 23 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
1557
by: John Hunter | last post by:
I am using pycxx 5.2.2 to generate some extension code. I want to extract some doubles from some python sequences When I do double l( Py::Float(rect) ); double b( Py::Float(rect) ); and...
2
11431
by: Pepijn Kenter | last post by:
Dear experts. I have a vector<float> and want to convert that to a vector<double>. I optimistically tried: #include <vector> #include <iostream> using namespace std; int main() {
22
3677
by: bq | last post by:
Hello, Two questions related to floating point support: What C compilers for the wintel (MS Windows + x86) platform are C99 compliant as far as <math.h> and <tgmath.h> are concerned? What...
9
2390
by: Sisyphus | last post by:
Hi, I have some software that does the following (in an attempt to determine whether the double x, can be represented just as accurately by a float): void test_it(double x) { float y = x;...
6
7252
by: James Thurley | last post by:
According to the docs, floats are 32 bit and doubles are 64 bit. So using floats should be faster than using doubles on a 32 bit processor, and my tests confirm this. However, most of the Math...
8
12884
by: Quinn Kirsch | last post by:
Hi all, the following c# example baffles me. my understanding of the managed code of visual .net is that all casts like this would be safe, but this example seems to contradict this notion. if...
2
13429
by: tkirankumar | last post by:
Hi all, uname -a SunOS cbmrsd1a1 5.10 Generic_118833-17 sun4us sparc FJSV,GPUZC-M g++ -v Reading specs from /usr/local/lib/gcc-lib/sparc-sun-solaris2.10/3.3.2/specs Configured with:...
116
35678
by: Dilip | last post by:
Recently in our code, I ran into a situation where were stuffing a float inside a double. The precision was extended automatically because of that. To make a long story short, this caused...
13
6153
by: Shirsoft | last post by:
I have a 32 bit intel and 64 bit AMD machine. There is a rounding error in the 8th digit. Unfortunately because of the algorithm we use, the errors percolate into higher digits. C++ code is...
30
3996
by: copx | last post by:
I am writing a program which uses sqrt() a lot. A historically very slow function but I read on CPUs with SSE support this is actually fast. Problem: C's sqrt() (unlike C++ sqrt()) is defined to...
0
7226
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7125
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7328
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7388
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7499
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5631
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3199
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1561
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
422
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.