This question already has answers here:
How to make C++ cout not use scientific notation
(8 answers)
Closed 3 years ago.
I try to write a large double number which is the time that takes my function to produce the result into txt file( using c++).
for exemple in my console, the function take 0.000029 (time unit), when i write the value into my txt file it is converted into : 2.9e-05
My question: how can i write the value as it is in the console i.e 0.000029 ?
here is my code:
*`
clock_t cPrec1=0;
double duration1 =0.0;
clock_t cTime1;
cTime1 = clock();
bool h= check(4, copy);
duration1 = ( cTime1 - cPrec1 ) / (double) CLOCKS_PER_SEC;
cPrec1 = clock();
outfile << space<< 1 << space << duration1<< space <<'\n' ;
printf(" saved\n");
`
Thanks for helping me.
You can use std::setprecision() and std::fixed function.
when you use a stream you can add a "<< std::setprecision(n)" at the beginning ( or even before the double number ) to set how many number after the dot you want to see. The parameter of this function is an integer that specify the number after the dot that are printed.
Another useful function is std::fixed that can be used like the previous on in the stream ( ex. "<< std::fixed" ). I report to you the definition of the function :
When floatfield is set to fixed, floating-point values are written using fixed-point notation: the value is represented with exactly as many digits in the decimal part as specified by the precision field (precision) and with no exponent part.
I leave you also 2 useful links for this function:
std::setprecision
std::fixed
Use:
outfile << std::setprecision(7) << duration1
Related
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!
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
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:
%f for fixed point notation (the format used by std::fixed for streams).
%e for scientific formatting (the format used by std::scientific for streams).
%g for the "best" version of fixed and scientific (the default for streams and since C++11 std::defaultfloat).
%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
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.
This question already has answers here:
Setprecision is Confusing
(3 answers)
Closed 9 years ago.
I have an homework in C++ in college and there is a line I couldn't understand at all:
cout << fixed << setprecision( 2 );
Can anyone help me to explain this line?
They are both stream manipulators. By using them with std::cout, you are (with fixed) setting it to use fixed floating-point notation and then with setprecision changing the number of decimals printed by cout. Try e.g.
double a = 0.123456789;
cout << a << fixed << a << setprecision(2) << a << endl;
To see what effect they have.
cout<<fixed<<setprecision(2)
This sets the standard output stream to a fixed-point notiation with precision of 2.
You are writing a real number fixed (float/double) to the standard output stream. setprecision sets the decimal precision to be used to format floating-point values on output operations.
Your fixed is most likely a double value.
cout means print to console.
fixed is what you will have printed. and setprecision is how many decimal points the precision will be rounded to.
so if fixed is
3.1967
you will see
3.2
I have a C++ program that takes in values and prints out values like this:
getline(in,number);
cout << setw(10) << number << endl;
I have an equivalent C program that takes in values and prints out like so:
fscanf(rhs, "%e", &number);
printf("%lf\n", number);
But while the C++ program prints out, 0.30951 the C program prints out 0.309510. More examples: C++: 0.0956439 C: 0.095644. It seems to print the same results as long as the value is 7 digits long, but if its shorter the 7 digits, it adds an extra 0 at the end. And if its longer than 7 digits, it rounds down to 6 digits. I would like the C results to match the C++ program. Any help would be appreciated.
Thanks.
Note: number is a float and number are read from a file.
Take advantage of the length and precision specifiers in C++ iostreams
std::cout.precision(4);
std::cout << std::setw(10) << number << "\n";
Take advantage of the length and precision specifiers in C formatted print statements:
printf( "%6.4lf", number );
Prints four decimal places in a "cell" six characters wide.
You can use a wildcard character for either length or precision to provide that value at runtime:
int precision = 4;
printf( "%6.*lf", precision, number );
printf("%g\n", number);
Will solve your problem, %lf is used for double, %f is used for float, and %g is used for float when you want to display all the decimal places and cut off zeros.
I am trying to set the two decimal numbers for double type data entered by the user, and I have the proper header file , but the result on the display is only integer, no decimal ?
I do really appreciate any help.
You would want to use the following format.
cout << setprecision(# of places past decimal) << fixed << varName << endl;
The fixed Input output manipulator is what tells it that you are setting the precision for the number of places after the decimal point.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Prevent scientific notation in ostream when using << with double
I get 1e-1 as result after a computation how can I convert the result from exponent to dot notation i.e., 0.1 ? Why is it automatically converted to exponential notation!!
You can use the fixed I/O manipulator to force the number to be printed in fixed-point notation:
double d = 42.0;
std::cout << std::fixed << d;
(std::scientific does the opposite: it forces the number to be printed in scientific notation)
Oracle (generally) doesn't do binary numbers (some support was added in 10g).
Numbers are held in an internal format and, unless you use an implicit or explicit TO_CHAR, it is up to the "client" to display them (or any desired "prettifying").
select to_number('1e-1') num,
to_char(to_number('1e-1'),'9.9EEEE') sci_num,
to_char(to_number('1e-1')) std_num
from dual;
NUM SCI_NUM ST
--------------- --------- --
.10 1.0E-01 .1