On VC++ 2008, ceil(-0.5)
is returning -0.0
. Is this usual/expected behavior? What is the best practice to avoid printing a -0.0
to i/o streams.

- 7,603
- 4
- 42
- 80
5 Answers
This is correct behavior. See Unary Operator-() on zero values - c++ and http://en.wikipedia.org/wiki/Signed_zero
I am partial to doing a static_cast<int>(ceil(-0.5));
but I don't claim that is "best practice".
Edit: You could of course cast to whatever integral type was appropriate (uint64_t, long, etc.)

- 1
- 1

- 2,080
- 15
- 19
-
I'm outputing as `double`, not casting. – ThomasMcLeod May 13 '11 at 03:28
-
1@ThomasMcLeod If you must use `double`, you must accept that negative zero is valid. If you don't want it to print the negative sign, you could add checking code like the following `std::cout << (ceil(x) == -0.0) ? 0.0 : ceil(x);`. Are you using double because you want the ".0"? – Chris Morgan May 13 '11 at 03:35
-
Right, it's a fixed width floating point output. I still don't see why `ceil` should ever return `-0.0`. – ThomasMcLeod May 13 '11 at 03:40
-
@ThomasMcLeod: `ceil` can return -0.0 because it returns `double` (or `float` or `long double`) and -0.0 is valid for all those types. If you look at your standard library's implementation of `ceil()` you'll likely discover that it uses `modf()`, which in turn uses `fmod()` to get the fractional part of the argument to `ceil()` and subtracts it from the argument to `ceil()`. In your case, this means it does `-0.5 + 0.5`. If you simply do `std::cout << (-0.5 + 0.5);`, what do you get? – Chris Morgan May 13 '11 at 04:02
-
2@Chris: that would be a *truly* bizarre implementation of `ceil( )`. – Stephen Canon May 13 '11 at 04:09
-
@Stephen, you're right, it's too late to do basic math apparently ;) I'm just going to post the link instead: http://www.google.com/codesearch/p?hl=en#aJtAZx52vVs/trunk/lib/math/ceil.c&q=ceil&sa=N&cd=1&ct=rc – Chris Morgan May 13 '11 at 04:10
-
@Chris, I get `-0.5 + +0.5 == +0.0`. But see my answer. – ThomasMcLeod May 13 '11 at 04:19
-
3@Chris: It's worth noting that the implementation you linked to actually *doesn't* conform to IEEE-754 (in the default rounding mode, it will return `+0.0` for `ceil(-0.5)`, whereas IEEE-754 says the result shall be `-0.0`). – Stephen Canon May 13 '11 at 04:35
ceil
in C++ comes from the C standard library.
The C standard says that if a platform implements IEEE-754 arithmetic, ceil( )
behaves as though its argument were rounded to integral according to the IEEE-754 roundTowardPositive rounding attribute. The IEEE-754 standard says (clause 6.3):
the sign of the result of conversions, the quantize operation, the roundToIntegral operations, and the roundToIntegralExact is the sign of the first or only operand.
So the sign of the result should always match the sign of the input. For inputs in the range (-1,0)
, this means that the result should be -0.0
.

- 103,815
- 19
- 183
- 269
I can't say that I know that it is usual, but as for avoiding printing it, implement a check, something like this:
if(var == -0.0)
{
var = 0.0;
}
// continue

- 1,676
- 3
- 21
- 31
-
3apparently operator `==` does not distinguish between `-0.0` and `+0.0`, (not that is matters in this case). – ThomasMcLeod May 13 '11 at 03:21
-
1@ThomasMcLeod: That's because -0.0 == +0.0 according to IEEE754. See http://en.wikipedia.org/wiki/Signed_zero#Comparisons – Chris Morgan May 13 '11 at 04:03
-
this code snippet is just to act as a buffer, to make sure that you are not printing the negative sign. – Nate Koppenhaver May 13 '11 at 17:58
I see why ceil(-0.5)
is returning -0.0
. It's because for negative numbers, ceil
is returning the integer part of the operand:
double af8IntPart;
double af8FracPart = modf(-0.5, & af8IntPart);
cout << af8IntPart;
The output here is "-0.0"

- 7,603
- 4
- 42
- 80
-
That's what I was alluding to in my (arithmetically incorrect) comment in my original answer. Thanks for posting the example. – Chris Morgan May 13 '11 at 04:15