Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,686 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,268 people online right now. Registration is fast and FREE... Join Now!




Float number result

 
Reply to this topicStart new topic

Float number result, Why did it give this number

mikjall77
post 4 Sep, 2008 - 07:46 AM
Post #1


New D.I.C Head

*
Joined: 18 Apr, 2007
Posts: 46



Thanked 1 times
My Contributions


I am taking a C++ class and one of our assignments is to format output for a number. We are suppose run a number first using double and then trying it as a float and formatting those numbers to 3, 4, 5, and 6 decimal positions. The number used is 42.98765. The code for the formatting is (replace double for float is get the double values):
CODE

int numberOne = 32;
float numberTwo = 42.98765f;

cout << "|" << setw(10) << setprecision(3) << fixed << numberTwo << "|" << endl
       << "|" << setw(10) << setprecision(4) << fixed << numberTwo << "|" << endl
       << "|" << setw(10) << setprecision(5) << fixed << numberTwo << "|" << endl
       << "|" << setw(10) << setprecision(6) << fixed << numberTwo << "|" << endl;

I can understand after running it as a double why it would add a 0 for the final number (setprecision(6)) so the answer would be '| 42.987650|'. I can understand that because, if I remember right, double is precise up to 15 decimal places. When I run it as a float, it comes up with '| 42.987652|'. Why does it decide to add a 2 and not a 0. I realize, again, if I remember right, float is precise up to 6 decimal places.

Any explanation of this is greatly appreciated!
User is offlineProfile CardPM

Go to the top of the page

jwwicks
post 4 Sep, 2008 - 02:05 PM
Post #2


D.I.C Head

Group Icon
Joined: 31 Jul, 2008
Posts: 59



Thanked 6 times

Dream Kudos: 200
My Contributions


Hello Mikjall77,

QUOTE(mikjall77 @ 4 Sep, 2008 - 08:46 AM) *

I am taking a C++ class and one of our assignments is to format output for a number. We are suppose run a number first using double and then trying it as a float and formatting those numbers to 3, 4, 5, and 6 decimal positions. The number used is 42.98765. The code for the formatting is (replace double for float is get the double values):

I can understand after running it as a double why it would add a 0 for the final number (setprecision(6)) so the answer would be '| 42.987650|'. I can understand that because, if I remember right, double is precise up to 15 decimal places. When I run it as a float, it comes up with '| 42.987652|'. Why does it decide to add a 2 and not a 0. I realize, again, if I remember right, float is precise up to 6 decimal places.

Any explanation of this is greatly appreciated!


It has to do with the way floating point numbers are represented within a computer. As you're probably aware all data in a computer is binary. Floating point numbers turn out to be a sum of 1 divided by a power of 2 such as 1/2^0 + 1/2^1 + 1/2^2 etc...

The more precision a number has the less likely round off errors will be displayed...

JW
User is offlineProfile CardPM

Go to the top of the page

perfectly.insane
post 5 Sep, 2008 - 02:18 AM
Post #3


D.I.C Addict

Group Icon
Joined: 22 Mar, 2008
Posts: 557



Thanked 46 times

Dream Kudos: 25

Expert In: C/C++

My Contributions


Float has 23 bits of mantissa. That's 8388608 different possibilities, with different positions for the decimal point. If you put the decimal point between the 83 and 88607, you'll notice only 5 digits, not 6.

This post has been edited by perfectly.insane: 5 Sep, 2008 - 02:22 AM
User is offlineProfile CardPM

Go to the top of the page

mikjall77
post 8 Sep, 2008 - 03:00 AM
Post #4


New D.I.C Head

*
Joined: 18 Apr, 2007
Posts: 46



Thanked 1 times
My Contributions


Thanks to both of you for your explanations. It definitely helps me understand it a little more.
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 07:19AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month