This question already has answers here:
Cout long double issue
(4 answers)
Closed 6 years ago.
in C++ I'm defining the following types:
double doubleType = 1.6e-300;
long double longDoubleType = 1.6e-300;
I'll then print the values using:
cout << "double is of size " << sizeof(doubleType) << " and value is " << doubleType << endl;
cout << "long double is of size " << sizeof(longDoubleType) << " and value is " << longDoubleType << endl;
my output reads:
double is of size 8 and value is 1.6e-300
long double is of size 12 and value is -1.43863e-264
Whats causing the difference in the interpretation of the values?
It doesn't seem likely that you got this behavior with std::cout. More likely, you were using printf with the wrong format specifier, something like:
printf("%g", longDoubleType);
This is undefined behavior. It's permitted to do just about anything, but one of the common ways it plays out is that some of the bits of longDoubleType are interpreted as a double instead of a long double.
With printf, I can reproduce your output. With std::cout, I can't.
Related
This question already has answers here:
Dividing 1/n always returns 0.0 [duplicate]
(3 answers)
Closed 8 years ago.
I have a problem with displaying and keeping any floating number or double or long or floating type this is the code :
int sd, n;
float d;
d=n/sd.capacity();
cout << d.toFloat() << endl;
cout << n << " / " << sd.capacity() << " = " << d << endl;
In the output I have d equal to 0 everytime, sd.capacity() and n are never 0, so all the time n is lower than sd.capacity value, d = 0 but it should never 0. The variable d does not contain anything but 0.
int/int results into int. So this does a problem.
You need to have atleast one of the operands as float.
Since, you are telling that n always < than sd.capacity(), so n/sd.capacity() in int division always gives 0.
I suppose you should do,
d= (float)n / (float)sd.capacity();
Does this solve it?
This question already has answers here:
How do I print a double value with full precision using cout?
(17 answers)
Closed 8 years ago.
I'm trying to convert a string into a double but my double gets cut off at the 3rd decimal point.
My string looks like this: "-122.39381636393"
After it gets converted it looks like this: -122.394
void setLongitude(string longitude){
this->longitude = (double)atof(longitude.c_str());
cout << "got longitude: " << longitude << endl;
cout << "setting longitude: " << this->longitude << endl;
}
Output example:
got longitude: -122.39381636393
setting longitude: -122.394
I want it to maintain all the decimal points, any tips?
I would write this code if I were you:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "-122.39381636393";
std::cout.precision(20);
cout << "setting longitude: " << stod(str) << endl;
return 0;
}
Basically, you would change things like:
precision for the printing
stod rather than low-level operation to get the double back from the string.
You can see it on ideone running.
It is probably the printing that is truncating precision, not the conversion from string to double.
Look at ios_base::precision http://www.cplusplus.com/reference/ios/ios_base/precision/
e.g.
cout.precision(10);
cout << "setting longitude: " << this->longitude << endl;
The proper C++11 solution is to use stod - String TO Double. You probably should use a try ... catch around that function as it throws an exception if your string is not a valid number.
However, the code you have, using atof is perfectly [assuming no bugs in your particular standard C library] converting to double (despite the name being Ascii TO Float, it returns a double value), you are just not printing enough digits, use precision or setprecision to inform cout how many digits to use, e.g.
cout << "Setting longitude: " << setprecision(15) << this->longitude << endl;
You will need to include <iomanip> for setprecision to work.
This question already has answers here:
How can I pad an int with leading zeros when using cout << operator? [duplicate]
(7 answers)
Closed 9 years ago.
Here's the code I'm trying to change
string binary = "000000100001000100010000000100000"
bitset<32> set(binary);
cout << hex << set.to_ulong() << endl;
The code shows 2112010 but I want it to show 02112010.
std::cout << std::setfill('0') << std::setw(5) << i << std::endl;
that is the same number you can format it with the 0 by using format specifiers if you need to retain the zero you need to store it as a string,
This question already has answers here:
Round double to 3 points decimal [duplicate]
(4 answers)
Closed 4 years ago.
I've used the following to round my values to 2 decimal points:
x = floor(num*100+0.5)/100;
and this seems to work fine; except for values like "16.60", which is "16.6".
I want to output this value like "16.60".
The way I'm outputting values is the following:
cout setw(12) << round(payment);
I've tried the following:
cout setw(12) << setprecision(2) << round(payment);
But that gave me answers like
1.2e+02
How can I output the values correctly?
This is because std::setprecision doesn't set the digits after the decimal point but the significant digits if you don't change the floating point format to use a fixed number of digits after the decimal point. To change the format, you have to put std::fixed into your output stream:
double a = 16.6;
std::cout << std::fixed << std::setprecision(2) << a << std::endl;
you can use printf / sprintf or other similar functions. Following code will format floating point value into two digits after decimals. Refer to the printf manual for more formatting info
float f = 1.234567
printf("%.2f\n", f);
From Trevor Boyd Smith's comment:
If you are allergic to printf and friends there is the type safe C++ version in #include <boost/format.hpp> which you can use to do:
float f = 1.234567;
cout << boost::format("%.2f") % f << endl;
This question already has answers here:
How do I print a double value with full precision using cout?
(17 answers)
Closed 6 years ago.
I want to output the value of a double in it's full precision. However, when using the cout function, it only displays the first 6 digits even though there is around 15-16 digits of precision.
How do I get my program to display the entire value, including the magnitude (power) component?
Use the setprecision() manipulator:
http://www.cplusplus.com/reference/iostream/manipulators/setprecision/
You can also force scientific notation with the scientific manipulator:
http://www.cplusplus.com/reference/iostream/manipulators/scientific/
cout << scientific << setprecision(15) << my_number << endl;
you could use something like this :
#include <iomanip>
cout << setprecision (9) << double_value << endl;
more iomanipulators, here
You're looking for setprecision (code taken from link):
int main () {
double f =3.14159;
cout << setprecision(15) << f << endl;
return 0;
}