uniform output of signed 0.0 with ostream - c++

Is there any standard way to set an ostream to print signed 0.0 uniformly without sign?
Or is this the simplest possible solution?:
double d = -0.0;
std::cout << ( d==0 ? 0.0 : d);
Edit:
I don't want to abs all, because I want to see non-0 negative numbers. I just don't want to see -0.0 (and I mean real -0, not very near ones). Can happen if you have e.g. a data set double data[100]; and mirror it: for(...) data[i]*=-1;. And a -0.0 output because of floatfield fixed and small precision would also be OK for near 0 negative numers.

You can also try to print it like this
std::cout << ( 0.0 + d);

double d = 0;
std::cout.precision(1);
std::cout.setf(std::ios::fixed);
std::cout << d << std::endl;
This prints 0.0. The precision() call sets the (maximum) number of decimal places to display, and the std::ios::fixed manipulator tell is to use all of those decimal places even if they are zero.
EDIT: If you're seeing a minus sign, as others have pointed out, it could be due to a rounding error. Note that there is also such a thing as "negative zero" in IEEE floating point numbers. If this is the real problem then you'll have to write code to check if your number is "close to" zero and if so, print it as exactly zero.

Related

c++ long double printing all digits with precision

Regarding my question I have seen a post on here but did not understand since i am new to C++. I wrote a small script which gets a number from user and script prints out the factorial of entered number.
Once I entered bigger numbers like 30, script does not print out all the digits.Output is like 2.652528598 E+32 however What I want is exact number 265252859812191058636308480000000. Could someone explain how to get all digits in long double.Thanks in advance
You can set the precision of the output stream to whatever you want in order to get your desired results.
http://www.cplusplus.com/reference/ios/ios_base/precision/
Here is an extract from the page, along with a code example.
Get/Set floating-point decimal precision
The floating-point precision determines the maximum number of digits to be written on insertion operations to express floating-point values. How this is interpreted depends on whether the floatfield format flag is set to a specific notation (either fixed or scientific) or it is unset (using the default notation, which is not necessarily equivalent to either fixed nor scientific).
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.
This decimal precision can also be modified using the parameterized manipulator setprecision.
// modify precision
#include <iostream> // std::cout, std::ios
int main () {
double f = 3.14159;
std::cout.unsetf ( std::ios::floatfield ); // floatfield not set
std::cout.precision(5);
std::cout << f << '\n';
std::cout.precision(10);
std::cout << f << '\n';
std::cout.setf( std::ios::fixed, std:: ios::floatfield ); // floatfield set to fixed
std::cout << f << '\n';
return 0;
}
Possible output:
3.1416
3.14159
3.1415900000
Notice how the first number written is just 5 digits long, while the second is 6, but not more, even though the stream's precision is now 10. That is because precision with the default floatfield only specifies the maximum number of digits to be displayed, but not the minimum.
The third number printed displays 10 digits after the decimal point because the floatfield format flag is in this case set to fixed.

How to compare doubles with 5 digits of precision?

I am trying to compare 2 doubles which fall in [0.0, 1.0].
My function (taken from https://stackoverflow.com/a/17341 )-
inline bool isEqual(double x, double y)
{
const double epsilon = 0.000001;
return fabs(x - y) < epsilon;
}
Usage-
cerr << isEqual(1.000001, 1.000002) << endl;
cerr << isEqual(1.000010, 1.000020) << endl;
The output is-
0
0
Whereas I am expecting first to be true, second to be false. Please tell me where I am going wrong and how to fix it?
1.000001 when limited to the usual 64-bit IEEE floating point representation is actually 1.0000009999999999177333620536956004798412. Likewise 1.000002 is actually 1.0000020000000000575113290324225090444087. The two are ever more slightly apart than 0.000001.
You can use a very slightly larger comparison value to catch this:
const double epsilon = 0.0000011;
It really isn't possible to completely eliminate any rounding problems with floating point numbers.

Round a double number when printing to cout

I want to print only one digit after the decimal dot.
So I write:
double number1 = -0.049453;
double number2 = -0.05000;
cout.setf(ios::fixed,ios::floatfield);
cout.precision(1);
cout << number1 << endl;
cout << number2 << endl;
My output is:
-0.0
-0.1
I want that the first line will be 0.0 (cause -0.0 is 0.0). How should I change my code that in a case of -0.0, it will print 0.0?
And about the second line, why doesn't it print 0.0 (or -0.0)?
Any help appreciated!
To print floating point numbers, they are rounded up if they are greater or equal to the "middle" of two possible outputs, so -0.05 becomes -0.0 intentionally.
Negative zero is the result if a negative number is rounded up to zero, also intentionally. The internal floating point format (IEEE 754) distinguishes between negative and positive zero (see this link), and it seems like C++ output streams stick to this behavior.
You can overcome both problems by separating printing the sign and the absolute value of the floating point number.
cout << (number <= -.05 ? "-" : "") << abs(number);
Explanation of the comparison: For numbers equal to or smaller than -0.05, rounding abs(number) (which is then >= 0.05) will be non-zero, so only then you want the sign to appear.
Note that the threshold value in this comparison depends on the output format you chose. So you should keep this printing method as local as possible in your code, where you know the exact format you want to print floating pointers.
Live example
Note: float and double behave exactly the same regarding these points.

c++ rounding special float values to integers

I am having some trouble rounding some special float numbers to integers.
I need to round a float number to an integer (if and only if) the first three float point values are zeros or 9's.
For example if the number was 4.0001 I need to round this to 4. and if the number was 4.9998 I need to round it to 5. Other than that, the values should be displayed as they are.
In other words I need to print an integer only if the above two rules were met, otherwise I should print float numbers,
How can one achieve this in C++.
Regards
If you're interested in the fractional part, modf is your friend. Say
something like:
double
conditionallyRound( double original )
{
double dummy;
double frac = modf( fabs( original ), &dummy );
return frac < 0.001 || frac > 0.999
? round( original )
: original;
}
If x should be rounded, then the maximum difference between x and round(x) will be 0.0001.
Of course, you should be aware that binary floating-point cannot exactly represent 0.0001, so this will always be an approximation.

gnu c++ floating number precision

I have simple question about floating number,
double temp;
std::cout.precision(std::numeric_limits<double>::digits10);
temp = 12345678901234567890.1234567890;
std::cout << (temp < std::numeric_limits<double>::max()) << std::endl;
std::cout << std::fixed << std::endl;
std::cout << temp << std::endl;
However, the output I get is this,
1
12345678901234567168.000000000000000
The value of temp is still within the range of double, however, the value is completely different. I am wondering what have I done wrong here?
Thanks.
A double has only 15.95 decimal digits of precision. You've already exceeded this number of digits in the integer part of the value, hence the loss of precision in the last few digits, and the lack of any useful digits after the decimal point.
You should probably take a look at this: http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html before doing any more work with floating point values.
It's not completely different. It's correct to 16 digits or so. That's about what you can expect from a double.
A double can only store a limited amount of precision. It works out to about 15 decimal digits.
Here's a helpful article on how floating point numbers are represented, and the implications of that representation: Float
IEEE 754 is not precise for any given value - for example http://www.cprogramming.com/tutorial/floating_point/understanding_floating_point.html and http://support.microsoft.com/kb/42980
-358974.27 can't be represented on float according to http://ridiculousfish.com/blog/posts/float.html and I remember (though I'm too lazy to test it) that even something "simple" like 2.2 or 2.3 can't be accurately represented even as a double.