C ++ ceil和负零

扫码查看
本文介绍了C ++ ceil和负零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在VC ++ 2008中, ceil(-0.5)正在返回 -0.0 。这是通常/预期的行为吗?避免将 -0.0 打印到i / o流中的最佳做法是什么。

解决方案

ceil 来自C标准库。



C标准说如果平台实现IEEE-754算术, ceil()表现为根据IEEE-754 roundTowardPositive 舍入属性将其参数四舍五入为整数。 IEEE-754标准说明(第6.3节):

因此,结果的符号应始终与输入的符号相匹配。对于范围( - 1,0)中的输入,这意味着结果应为 -0.0


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.

解决方案

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):

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.

这篇关于C ++ ceil和负零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 02:42
查看更多