How to print fixed point decimals using errs() output stream of llvm.
For example if now if am doing errs()<<3.3; it is showing in scientific notation. I want it in decimal notation. I don't want to print with cout, but with errs
You can use the format function from include/llvm/Support/Format.h to create C-printf-like strings:
errs() << format("%.3f\n", 3.3);
etc.
Related
Hi although I do something like this
#include <iomanip>
...
std::cout<<"Numbers "<<std::setprecision(2)<<numb1<< " "<<std::setprecision(2)<<numb2<<" "<<std::setprecision(2)<<numb3<<" "<< std::setprecision(4)<<numb4<<std::endl;
I do get
Numbers 14 1.5e+02 0.0053 & 220
How can I really make set the precision per column to get a consistent format and not this mixture of precision(5) and scientific format?
Use the std::fixed manipulator too which will switch off any reversion to scientific notation:
std::cout << std::fixed /*<< as before from here*/
If you want to switch the scientific notation back on again, then introduce the manipulator std::scientific.
Reference; http://en.cppreference.com/w/cpp/io/manip/fixed
I have some old C code I'm trying to replicate the behavior of in C++. It uses the printf modifiers: "%06.02f".
I naively thought that iomanip was just as capable, and did:
cout << setfill('0') << setw(6) << setprecision(2)
When I try to output the test number 123.456, printf yields:
123.46
But cout yields:
1.2+e02
Is there anything I can do in iomanip to replicate this, or must I go back to using printf?
[Live Example]
Try std::fixed:
std::cout << std::fixed;
Sets the floatfield format flag for the str stream to fixed.
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.
The three C format specifiers map to corresponding format setting in C++ IOStreams:
%f -> std::ios_base::fixed (fixed point notation) typically set using out << std::fixed.
%e -> std::ios_base::scientific (scientific notation) typically set using out << std::scientific.
%g -> the default setting, typically set using out.setf(std::fmtflags(), std::ios_base::floatfield) or with C++11 and later out << std::defaultfloat. The default formatting is trying to yield the "best" of the other formats assuming a fixed amount of digits to be used.
The precision, the width, and the fill character match the way you already stated.
I want to convert a given string into double without converting the value into decimal, if the string is in scientific format.
That is 1.23e1 should be saved as 1.23e1 and not as 12.3.
I checked stringstream, strtod, boost::lexical_cast and other methods
but all of these convert 1.23e1 into 12.3.
Is there a way that so that 1.23e1 can be saved as 1.23e1 instead of 12.3??
You're confusing the value with its representations.
12.3, 1.23e1, 0.123e2 and 123.0e-1 are all representations of the same value. They will also be stored in a double exactly the same representation, whichever one you input. The IEEE-754 format defines how a value is represented in a double-precision floating-point format. It's a binary format that looks nothing like "1.23e1".
So, ignore any perceived representation issues on your input. All you need to do is ensure that the output representation (i.e. when converting the double to a string representation of the value) is in the format you want. To do this, look at std::scientific:
double a = 12.3;
std::cout << std::scientific << a << "\n";
Output:
1.230000e+01
You can also manipulate the precision to obtain more or fewer digits:
std::cout << std::scientific << std::setprecision(2) << a << "\n";
Output:
1.23e+01
The following code will print value of a and b:
double a = 3.0, b=1231231231233.0123456;
cout.setf(std::ios::fixed);
cout.unsetf(std::ios::scientific);
cout << a << endl << b << endl
The output is:
3.000000
1231231231233.012451
You can see that a is outputed with fixed 6 count of decimals.
But I want the output like this:
3
1231231231233.012451
How can i set flags only once, and output the above result.
The stream inserts 0s following the double because the stream's default precision for the output of floating-point values is 6. Unfortunately there is no straightforward way of checking if the double represents a whole number (so you could then only print the integral part). What you could do however is cast the value to an integer.
std::cout << static_cast<int>(a);
The default formatting for floating point numbers won't support the formats as requested. There are basically three settings you could use:
std::fixed which will use precision() digits after the decimal point.
std::scientific which will use scientific notation with precision() digits.
std::defaultfloat which will choose the shorter of the two forms.
(there is also std::hexfloat but that just formats the number in an form which is conveniently machine readable).
What you could do is to create you own std::num_put<char> facet which formats the value into a local buffer using std::fixed formatting an strips off trailing zero digits before sending the values one.
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.