Solved

[noob][c++]  cout + float + precision

Posted on 2007-11-16
10
3,352 Views
Last Modified: 2012-06-21
I am trying to do this



#include <iostream>
#include <iomanip>

//
// if I can adjust the float things then it can be used
//


using namespace std;

int main()
{
      float date;
      date = 12003932;
      cout << setprecision ( 11 ) << date << endl;
      cout << date << endl;
      cout << (date/10000) << endl;
      date = ((date/10000) + 1 ) * 10000;
      
      cout << date << endl;
      
      return 0;
      
}




this is the output




12003932
12003932
1200.3931885
12013932




for some reason this pops up


1200.3931885


when it shouldn't.


how do I fix it?
0
Comment
Question by:Troudeloup
  • 4
  • 4
  • 2
10 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 250 total points
ID: 20299471
floating numbers are not stored as decimals, indeed them are stored in inverse of the powers of 2, as explained here:
http://en.wikipedia.org/wiki/IEEE_754

So unless you control the display precision by yourself you will have thos unexpected result, try with:
cout << setprecision(4) << (date/10000) << endl;
     
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 250 total points
ID: 20300857
setprecision ( 8 )
or what do you want to happen?
0
 

Author Comment

by:Troudeloup
ID: 20300873
i just want cout to display the whole float exactly what they are.
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 250 total points
ID: 20300901
setprecision(4) will show "as you expected", but the number is really 1200.3931885
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 250 total points
ID: 20300925
The exact value, depending on your float implementation, is probably something like
1200.3931884765625
With a double, you could increase the precision to
1200.39319999999997889972291886806488037109375
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 250 total points
ID: 20300968
Or you could use int date, and get exactly
12003932
12003932
1200
12010000
0
 

Author Comment

by:Troudeloup
ID: 20300980
ok,

float a;

a = 12002323;


cout << a << endl;

should say

1.2002323e7

and

cout << a/10000 << endl;

should say

1.2002323e3





does it work simply like that?


because if the number would change unexpectedly to something else...



0
 

Author Comment

by:Troudeloup
ID: 20300989
i need to go into decimal values, so I think i can use only float
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 250 total points
ID: 20301085
<< scientific << setprecision ( 8 )
0
 

Author Comment

by:Troudeloup
ID: 20301105
nice.

can I display float like this


12003939

instead of


1.2003939


?
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
format the code in java 6 93
canBalance challenge 34 85
splitOdd10 challenge 5 107
Problem to event 3 97
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question