Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ cout << [double] not printing decimal places

Tags:

c++

I have a simple C++ program that I thought would print out a double value defined in f and g as doubles ... but C++ is printing them out as integers. I checked the default precision of cout, and when I run it the output says the default precision should be 6 so I'm confused as to why the double isn't printing properly.

#include <iostream>
#include <stdio.h>
int main() {

    double f = 735416.416898;
    double g = f + 0.3;

    std::cout << "default precision is " << std::cout.precision() << std::endl;

    std::cout << "[c++] f = " << f << std::endl;
    std::cout << "[c++] g = " << g << std::endl;
    printf("[c] f = %f\n", f);
    printf("[c] g = %f\n", g);

    f = 735416.516898;
    g = f + 0.3;

    std::cout << "[c++] f = " << f << std::endl;
    std::cout << "[c++] g = " << g << std::endl;
    printf("[c] f = %f\n", f);
    printf("[c] g = %f\n", g);

    return 0;

}

The output I'm seeing is:

kiran@kiran-VirtualBox:~/test$ g++ -o test test.cpp 
kiran@kiran-VirtualBox:~/test$ ./test 
[c++] f = 735416
[c++] g = 735417
[c] f = 735416.416898
[c] g = 735416.716898
[c++] f = 735417
[c++] g = 735417
[c] f = 735416.516898
[c] g = 735416.816898

Because the default precision is showing up as 6, shouldn't the cout also be showing the 6 decimal places?

If I do

std::cout << std::fixed << f

it prints properly (w/ all the decimal places).

Just wondering if someone can explain this behavior to me? Is it expected? Am I expected to use std::fixed to force printing of doubles properly?

like image 317
Kiran K. Avatar asked Nov 22 '13 02:11

Kiran K.


2 Answers

This happens because of the distinction between the default notation and the fixed notation.

According to documentation,

  • Using the default floating-point notation, the precision field specifies the maximum number of meaningful digits to display in total counting both those before and those after the decimal point. Notice that it is not a minimum, and therefore it does not pad the displayed number with trailing zeros if the number can be displayed with less digits than the precision.
  • In both the fixed and scientific notations, the precision field specifies exactly how many digits to display after the decimal point, even if this includes trailing decimal zeros. The digits before the decimal point are not relevant for the precision in this case (emphasis is added).

The stream uses the default floating-point notation until (or unless) you switch it to fixed or scientific notation. Since your number has six digit before the decimal point, and the default precision is also set to six, the floating-point numbers look like integers.

like image 72
Sergey Kalinichenko Avatar answered Sep 20 '22 22:09

Sergey Kalinichenko


There are three floating-point notations:

  • std::fixed for fixed-point notation.
  • std::scientific for scientific notation.
  • (none) the default notation.

The default notation just uses the precision value to count all digits of the number, not just the decimal part. If you use a short number (like 3.1415) you can see that the number is displayed correctly.

It's also interesting to know, as cplusplus.com documentation cites, that:

The default notation can be selected by calling str.unsetf(ios_base::floatfield).


P.S.: for C++11 there are new notations: hexfloat for hexadecimal and defaultfloat for the so called (none) before.

like image 27
talles Avatar answered Sep 22 '22 22:09

talles