Uses for negative zero floating point value?

痴心易碎 提交于 2019-11-27 13:58:19
NPE

From Wikipedia:

It is claimed that the inclusion of signed zero in IEEE 754 makes it much easier to achieve numerical accuracy in some critical problems[1], in particular when computing with complex elementary functions[2].

The first reference is "Branch Cuts for Complex Elementary Functions or Much Ado About Nothing's Sign Bit" by W. Kahan, that is available for download here.

One example from that paper is 1/(+0) vs 1/(-0). Here, the sign of zero makes a huge difference, since the first expression equals +inf and the second, -inf.

In addition Signed Zero Good For :

The zeroes can be considered as a variant of the extended real number line such that 1/−0 = −∞ and 1/+0 = +∞, division by zero is only undefined for ±0/±0.

Negatively signed zero echoes the mathematical analysis concept of approaching 0 from below as a one-sided limit, which may be denoted by x → 0−, x → 0−, or x → ↑0. The notation "−0" may be used informally to denote a small negative number that has been rounded to zero. The concept of negative zero also has some theoretical applications in statistical mechanics and other disciplines

Polynomial

There are only two real use-cases that I can see:

  1. You want to show that a value is negative but very very small (perhaps infinitessimal), i.e. too small to represent as a float or double.
  2. You are working with math that only allows negatives, but still want to display zero. There are a few cases in physics, complex numbers and number theory where this can be useful.

For the mostpart, it's not useful and should be avoided.

You may also want to take a look at this question: Is there a negative zero? and the IEEE 754 spec for floating point.

I'm making a measuring app, and the -0 is very useful for mixed numbers (such as separating into feet and inches).

Imagine that we have a variable "length" that we're trying to separate into "feet" and "inches".

(This is java code, but the same idea is true for C++).

feet = Math.signum(length) * Math.floor(Math.abs(length / 12));
// could also do feet = length>0 ? Math.floor(length / 12) : Math.ceil(length / 12)
inches = Math.abs(length) % 12;

If the length is between -1 feet and 0 feet, we'd want it to say -0 for the feet so we know it's negative.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!