473,401 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

Float !?

Hi

The following code is giving strange results...........

float fTest = 536495.61f;
cout << _T("float: ") << fTest << endl;

int dec, sign;
char* pszTest = _fcvt(fTest, 10, &dec, &sign);
cout << _T("string: ") << pszTest << endl;
cout << _T("dec: ") << dec << endl;
cout << _T("sign: ") << sign << endl;

It seems that the stored value for the float is 536495.625 and not
536495.61........... Why?
And why would the "cout" of a float not give the digits after the floating
point?

Regards
Chris
Nov 16 '05 #1
2 1503
OK back to the basics....... It's an internal representation problem.
I'm still wondering why "cout" of a float would not give the digits after
the floating point...
"Chris" <ch****************@swisscom.com> a écrit dans le message de news:
1069333936.520796@ftpgate...
Hi

The following code is giving strange results...........

float fTest = 536495.61f;
cout << _T("float: ") << fTest << endl;

int dec, sign;
char* pszTest = _fcvt(fTest, 10, &dec, &sign);
cout << _T("string: ") << pszTest << endl;
cout << _T("dec: ") << dec << endl;
cout << _T("sign: ") << sign << endl;

It seems that the stored value for the float is 536495.625 and not
536495.61........... Why?
And why would the "cout" of a float not give the digits after the floating
point?

Regards
Chris

Nov 16 '05 #2
Wil
>It seems that the stored value for the float is
536495.625 and not 536495.61........... Why?
Because a float has 32 bits, so about 8 decimal digits is
all the precision you'll get. Specifically, a floating-
point number has to be stored as

(+-) 1.b1b2b3...b23 x 2^b1b2b3...b8

so you get 1 bit for the sign of the number, 8 bits for
the exponent (which is expressed with a bias of 127, so
the exponent can take on values in the range [-127,127]
without a sign bit), and 23 bits for the mantissa. (The
leading "1" is suppressed, to give an extra precision
bit.) If you need more than 23 bits of precision in your
binary number, use a double instead of a float.

In your particular case, 536494 = 2^19 + 2^13 + 2^11 +
2^10 + 2^9 + 2^8 + 2^7 + 2^5 + 2^3 + 2, so after 19 binary
digits are used up, you still have 536495.61 - 536494 =
1.61 left to express. The leading 1 is suppressed, so you
have to express 0.61 and you have only 23 - 19 = 4 bits
left in your float. After you add 2^-1 + 2^-4 to get
0.5625, you have used up all the bits and you still have a
remainder of 0.0475. What the machine does is the best it
can with 4 bits, namely represent your 1.61 as 1 + 2^-1 +
(0 x 2^-2) + 2^-3 + (0 x 2^-4) = 1.625. Thus, your number
winds up being stored as 536495.625, as you discovered.
And why would the "cout" of a float not give the digits
after the floating point?
Again, the machine does "the best it can", this time of
guessing a good format for the output of a number of this
size with this precision. If you want to see the 3 digits
to the left of the zero, do something like

cout << setw(12) << setprecision(3) << fixed;
cout << fTest;

Your old FORTRAN hacker,
Wil
-----Original Message-----
Hi

The following code is giving strange results...........

float fTest = 536495.61f;
cout << _T("float: ") << fTest << endl;

int dec, sign;
char* pszTest = _fcvt(fTest, 10, &dec, &sign);
cout << _T("string: ") << pszTest << endl;
cout << _T("dec: ") << dec << endl;
cout << _T("sign: ") << sign << endl;

It seems that the stored value for the float is 536495.625 and not536495.61........... Why?
And why would the "cout" of a float not give the digits after the floatingpoint?

Regards
Chris
.

Nov 16 '05 #3

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

Similar topics

5
by: Pat | last post by:
Give two double-typed variable X and Y. If (X==Y) is true, then how about the following results: (float(X) > float(Y))? (float(X) < float(Y))? (float(X) >= float(Y))? ( X > float(Y) )? ( X...
14
by: Glen Able | last post by:
Should it be possible to create a custom class, 'Float', which would behave as a drop-in replacement for the builtin float type? As mentioned in another thread, I once tried this in rather a...
16
by: Gerald Lafreniere | last post by:
{ float F=123.456000; F*=1000; // Actually I used a for loop F*=10 three times. printf("%f\n", F); } This will produce something like 123456.00XXXX, where XXXX are garbage digits. Why...
6
by: Dave win | last post by:
Hi all: I'm confused with the expression "(float *())". Book says that this is a cast. But, I have no idea of this expr. why could this expr ignore the variable??? Thanx!!!
9
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;...
11
by: Marc Pelletier | last post by:
Hello, I am having trouble implementing the following callback: CNCSError CECWCompressor::WriteReadLine(UINT32 nNextLine, void **ppInputArray) where ppInputArray is a 3 by x array. The...
20
by: ehabaziz2001 | last post by:
That program does not yield and respond correctly espcially for the pointers (*f),(*i) in print_divide_meter_into(&meter,&yds,&ft,&ins); /*--------------pnt02own.c------------ ---1 inch = 2.51...
8
by: vjnr83 | last post by:
Hi, I have a doubt: what is the difference between float **p and float *p? Thanks in advance, Vijay
13
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...
3
by: Arnie | last post by:
Folks, We ran into a pretty significant performance penalty when casting floats. We've identified a code workaround that we wanted to pass along but also was wondering if others had experience...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.