Question

So I want to use printf so that I can make columns line up but printf can't seem to print doubles that need scientific notation. It just comes out as a 0 but with cout it comes out fine. 'in' and 'fn' are structs 'x' 'y' and 'z' are doubles

Code

printf("Facet Normal: %lf %15lf %15lf\n", in->fn.x, in->fn.y, in->fn.z);
cout << "cout test: " << in->fn.x << endl;

Output

Facet Normal: -0.000000       -0.894426        0.447215
cout test: -9.6137e-08

I can't seem to get printf to work correctly. I had the entire function working correctly with cout but like I said, I'd like things to line up niftily.

Edit:

As Oli said, using %e does get it to print correctly. By using %e throughout though it puts everything in scientific notation and a lot of the numbers in the data set don't in reality really need it. Cout seems to convert between %lf and %e as needed. Is there an easy way to get printf to get this behavior as well?

Answer:

%f is for both float and double (since float arguments are promoted to double); %lf is for long double. f prints with no exponent, e prints with an exponent, and g uses whichever looks better (following specific rules). – Keith Thompson

%g gets the exact behavior I was looking for!

Was it helpful?

Solution 2

It seems you want to get the "best" formatting (which is the default for std::ostream): you can use %g (%Lg for long double) to have the formatting function decide how the values should be formatted. There are four format specifiers:

  1. %f for fixed point notation (the format used by std::fixed for streams).
  2. %e for scientific formatting (the format used by std::scientific for streams).
  3. %g for the "best" version of fixed and scientific (the default for streams and since C++11 std::defaultfloat).
  4. %a for an exact/hex representation of the floating point number (since C++11 std::hexfloat).

The formatting flags can used both in lowercase and uppercase to indicate whether any characters should be lowercase or uppercase (e.g. e vs. E). Note that the l length specifier is actually not relevant for the floating point formatting. You might need to use L, though, when formatting long double

OTHER TIPS

As the std::printf() reference says, just use %e:

std::printf( "One double in decimal scientific notation! %e" , my_double );

But the correct C++ way is to use std::cout and some manipulators, std::scientific in this case:

std::cout << "One double in decimal scientific notation!" << std::scientific << my_double;

Note that the format of std::cout forms part of its state, that is, you only have to configure it once, the format is applied to any output operation after the setting, and before other format change:

std::cout << std::scientific;

std::cout << std::pow( 10.0 , 10.0 ) << std::endl;
std::cout << std::pow( 10.0 , 20.0 ) << std::endl;
std::cout << std::pow( 10.0 , 30.0 ) << std::endl;

1e11
1e21
1e31

If you want to control the output from std::cout, use setw() and setfill(). http://www.cplusplus.com/reference/iomanip/setw/

You can also set the precision, etc. I will leave that to you to explore.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top