2

I need to read floating-point values from a file.

Basic sample code of how I do this:

int main() 
{
    float number;
    ifstream inputFile;

    inputFile.open("testfile.dat");

    inputFile >> number;

    cout << number << endl;

    return 0;
}

The first line in the file is: 13212.13131. But when I cout 'number' the displayed number is: 13212.1

The problem is part of the decimal gets dropped and in other cases all of it gets dropped. Why does this happen, and how can I solve this problem?

The point of reading the number from the file is to do mathematical calculations with it.

phuclv
  • 37,963
  • 15
  • 156
  • 475
Craig
  • 548
  • 9
  • 24
  • Try to set appropriate stream [flags](http://www.cplusplus.com/reference/ios/ios_base/fmtflags/) – Jovibor Mar 21 '15 at 23:08
  • Remember - the "precision" you see in cout is *NOT* necessarily the actual precision of the stored value. You can change the displayed precision using [ios_base::precision](http://www.cplusplus.com/reference/ios/ios_base/precision/) or [std::setprecision](http://www.cplusplus.com/reference/iomanip/setprecision/). Also: you might consider using double instead of float. – FoggyDay Mar 21 '15 at 23:21
  • 1
    Six significant figures for a `float` is about right, isn't it? – Lightness Races in Orbit Mar 22 '15 at 03:23
  • And note that this is happening on *output,* not on input. – user207421 Mar 22 '15 at 03:43

2 Answers2

3

First, floating-point precision on output (for both std::cout and printf) is 6 decimal digits by default. You need std::setprecision() to get it print more digits. But you'll then get to the limit of float type.

On most systems float is IEEE-754 single precision, therefore it can only store about 7 digits of significant. The nearest to 13212.13131 is 1.3212130859375E4. If you need more precision, you must use double, which has about 15-16 digits of precision on most systems.

Read more: Is floating point math broken?

phuclv
  • 37,963
  • 15
  • 156
  • 475
  • So if I'm understanding what you are saying correctly, The value stored in number is correct the problem arises when I want to cout the value? – Craig Mar 22 '15 at 06:00
  • yes. It's acccurate to the float's precision. Printing more digits will only introduce more garbage – phuclv Mar 22 '15 at 11:38
1

Try using std::setprecision():

cout << setprecision(14) << number << endl;

You will need to

#include <iomanip>

If that doesn't solve it you should try debugging it and see what the number actually is (13212.13131 or 13212.1).

  • The output when I include setprecision() is: 13212.1309 which is still not correct. This also does not solve the problem if I were to do mathematical calculations with 'number'. I am a bit of a noob in c++, how do you debug? – Craig Mar 21 '15 at 23:05
  • 2
    @Craig For that see the `FLT_DIG` value defined in `float.h`, which should be 6. This is the precision (number of digits) of a `float`. If you need more precision, use a `double`. – wimh Mar 21 '15 at 23:14
  • Q: How do you debug? A: That depends largely on your compiler, and your preferences. Microsoft Visual C/C++ (Windows) and XCode (Mac) both have their own graphical debuggers. For Linux, I use g++ (compiler) and gdb (debugger) - both command line. – FoggyDay Mar 21 '15 at 23:27
  • a -1 for suggesting that `float` can store a number with 14 significant decimal digits. I don't know any system on which this is true. You're basically teaching the OP to misunderstand rendered output. In fact you don't answer the question at all, so another good reason to downvote :P – Lightness Races in Orbit Mar 22 '15 at 03:24