10 ms·
Does anyone have a good example of a programming problem where we _need_ signed zero? Or where it makes things significantly simpler? As far as I know, there is
by bendiksolheim 6y ago
Does anyone have a good example of a programming problem where we _need_ signed zero? Or where it makes things significantly simpler? As far as I know, there is no distinction between -0 and +0 in math, so I have never really understood why this is a thing in computers.
- johnmyleswhite 6y agoNegative zero was introduced because of branch cuts: https://people.freebsd.org/~das/kahan86branch.pdf https://people.freebsd.org/~das/kahan86branch.pdf
- bombcar 6y agoAmusingly enough the Apple Watch reports temperature as 0° and sometimes as -0°, I've not determined what causes that, perhaps rounding around 0?
- portmanteaufu 6y agoSometimes the sign is used to indicate which "direction" the temperature is moving. If it was -10° overnight and it's -0° now, the puddles outside will still be frozen. If it was 10° overnight and it's 0° now, the puddles will still be liquid. (Edit: no idea whether this applies to the Apple watch, it's just a use case for -0 with regards to temperature.)
- majewsky 6y agoYour predictions about the state of the puddle are most likely right, but not for the reasons that you think. Air temperature is commonly measured at 2m above ground. An measurement of 0° air temperature does not imply 0° ground temperature. The more significant effect is that the ground has a higher thermal capacity than the air, so it changes temperature more slowly throughout the day. If it's been colder before and now it's 0°, the ground is still way below 0° and thus puddles are frozen. If it was warmer and now the air has cooled down to 0°, the ground is going to be a bit warmer still and thus puddles are liquid. Denoting this difference as +0° and -0° does not seem very useful since the same effect is going to be nearly equally significant at 1° or -2°. (Sidenote: The thermal capacity of the ground is also the reason why air temperature is not measured anywhere near the ground.)
- bombcar 6y agoThe thermal capacity of the ground is also why, if you're insulating a basement or slab, you want to insulate the edges and DOWN - but you don't need to insulate in the center of the slab (as eventually the ground reaches the ambient temperature and acts as a huge thermal mass).
- majewsky 6y agoProbably not even rounding, but just printf. The following works on my zsh: $ printf '%.0f°\n' 0.3 0° $ printf '%.0f°\n' -0.3 -0°
- a1369209993 6y agoThat actually is rounding around zero (to zero decimal places, but it is rounding).
- majewsky 6y agoYou're right. What I meant to say was that it's possible the programmers working on the weather app didn't add explicit rounding, they just passed the value through a formatter that did the rounding implicitly.
- netzone 6y agoI believe that's actually a standard presentation, I've seen it in several weather graphs. It's basically a rounding yeah, signifying it's just slightly below zero, but not enough to round to -1°.
- sly010 6y agoMaybe the actual temperature value has a few more digits that are truncated during formatting: "-0.001" -> "-0"
- chrisseaton 6y agoComputers (obviously) have to approximate the majority of actual mathematical numbers, as they do not have infinite storage. If you've got two numbers, +0.0000001 and -0.0000001, but you can't represent that precision, can you see how it's less bad to round to +0.0000 and -0.0000 rather than to just 0.0000? It's encoding strictly more information.
- bendiksolheim 6y ago> it`s less bad Really good point. My approach to this was "if it’s not used in any mathematical algorithms, why do we need it in computers?". But in your example, you retain some information even though you can’t represent the whole truth. Thanks!
- numlock86 6y agoOk, so what's true? -0<+0 or -0==+0
- deleted 6y ago[deleted]
- chrisseaton 6y agoYou can look these things up for yourself - they're standardised in most language's implementations in something called IEEE 754. In the cases you've asked about they're false and true. Is this what you want in all cases? No. Is this what you want in some cases? Yes. It's a tradeoff. You can still observe the difference by dividing by zero (which should be another indication that these aren't real numbers as we're conventionally understand them.)
- BeetleB 6y ago> they're standardised in most language's implementations in something called IEEE 754 There is the IEEE 754, and there is the language's standard. One should always look at the latter because it's often the case that the language doesn't fully conform to IEEE 754.
- 6y ago
- ben509 6y ago0 does double duty as meaning itself and to indicate underflow due to multiplying very small numbers. If you have a function that can experience underflow it can be useful to preserve the sign of the underflowing value. Otherwise you'd need more checks to get that information.
- banachtarski 6y agoIt’s used when writing SIMD code all the time to quickly mask bits as needed or to check the sign of a floating point number with an SSE intrinsic. _mm_xor_ps(_mm_set1_ps(-0.f), reg) as an example negates all four components of reg.
- pfortuny 6y agoExactly for things like OP’s argument: 1/-0 is -inf, and that may be important in asymptotics
- isolli 6y agoA distinction can be made when considering the limit of a sequence. If a sequence converges to zero from positive values (sometimes written -> 0+), then the inverse of the sequence will diverge to +inf, while if the sequence converges to zero from negative values (-> 0-), the inverse will diverge to -inf. As a sibling comment wrote, rounding numbers to -0 and +0 can provide extra information, though it may not be useful in all contexts.
- patrec 6y agoWhen does this need arise? Well, otherwise inverting a value can change it's sign and in particular inverting -∞ twice will give you +∞, and being off by "2∞" is a pretty large error for a lot of computations ;) You can end up with zeros and infinities pretty easily because you overflow or underflow the range of floating point precision, and generally you want something sensible to happen in typical cases, even if some common arithmetic identities necessarily break down. I would actually like a true signed zero (or rather "epsilon" value ), so -0 and +0 as distinct from "normal" 0 which is truly unsigned, neither positive nor negative. The former two would only arise from underflow, and the reason this is useful that if you underflow from below zero and invert that you want to get -∞ and if you underflow from above zero and invert that you want to get +∞. Inverting a signless zero should give NaN (instead it gives +∞, which is nonsense in basically any case where the domain is not inherently the non-negative reals already and the 0 did not come about by and underflow; in particular 1/0 should be NaN). If anyone knows why this design was not chosen and what fundamental downsides it has, I'd love to hear it. Obviously representing three zeros is a tad more annoying, but IEEE754 has a lot of stuff that's annoying implementation wise but was added for nicer numerical behavior (e.g. denormals, and of course various "global" rounding modes etc. which probably qualify as a mistake in retrospect).
- amelius 6y ago∞ is not a number. Using it as a number is a hack invented by mathematicians.
- steerablesafe 6y ago∞ is not used as a number by mathematicians. Maybe by engineers.
- chithanh 6y agoThat is not entirely correct. Schmieden and Laugwitz for example developed in the 1950s a nonstandard Analysis which adjoins an infinitely large element (called Ω) to the natural numbers. The basic idea was a formula A(Ω) was true if A(n) was true for almost all finite natural n. While it wasn't immensely useful going forward, it helped to clarify the use of infinity and infinitesimals in earlier work.
- forgetfulness 6y agoFloating point math was devised to be used in numerical methods that are applied iteratively, like gradient descent. In that world what you are always doing is "converging" to solutions step by step, and a negative or positive zero can tell you which direction you're converging from.
- jerf 6y agoI agree with many of the other replies, but I would also add, perfectly serious and with no sarcasm, do not be fooled; computers do not use "math" numbers. They use what they use; machine-word bounded integers and IEEE floats most commonly, unbounded integers, some variants on rational numbers, and sometimes some more exotic things, but they never use real numbers. They're bounded to the computable numbers. So whether or not there's a -0 or +0 in some "math" isn't relevant, because computers aren't using "some math" but very specific mathematical constructs that must be understood on their own terms. I haven't seen very many mathematical systems that have a reified "NaN" object that can be explicitly passed around as a valid value to functions. (I know of many systems that have a "bottom" but I would say bottom is usually presented not as an object you can "have" but as a property of some mathematical object. Haskell for instance uses this idea; there are some trivial ways to have or produce something that has the property of being "bottom", like calling "error", but you can't just "have the bottom value".) Moreover, there are mathematical systems in which such things can appear. There are many exotic and interesting number systems in the mathematical world, which even a bachelor's degree in mathematics may only scratch the surface of, depending on which junior/senior level courses you take. Real numbers are without a doubt the most studied, and they do not contain a -0 (though even then you'll still see it show up sometimes as a special notation in limits), but they are the beginning of number systems, not the end. I mention all this because it's important; it's a very common misconception that computers use the numbers like you learned in school, and it will lead to nothing but pain. The next misconception is that, ok, sure, they aren't those numbers exactly, but they're so close that I don't have to worry about it. This works as long as you don't push your floats too hard, and a lot of us don't, but also breaks down surprisingly quickly. It's important for programming professionals to understand in general that IEEE floats are their own thing. Whenever I use them I do at least take a couple of seconds to double-check mentally that the sharp pointy bits aren't going to stab me, even for simple things like adding durations of a request to some floating point accumulator for metric purposes.
- brandmeyer 6y agoSign/magnitude ADCs almost have signed zero in hardware, by generating a sign bit and zero or more magnitude bits. The typical method to map to voltages doubles the span between adjacent values and skips over zero. So a 2-bit sign/magnitude ADC measures values in the series {-3, -1, 1, 3}. So strictly speaking this number system doesn't have a representation of zero at all. Tiny measurements either round up or round down to +/- 1.
- an_d_rew 6y agoIt’s pretty common when, for example, you’re computing the flows in a bifurcating field. Take 2d laminar flow around a circle. The flow field splits right in the middle. Signed zeros ensure that, along with graceful underflow, the local solution is not wonky. Lots of complex-arithmetic examples too.
- linuxlizard 6y agoLatitude / Longitude coordinate as floating point. https://gis.stackexchange.com/questions/211796/if-degrees-is-zero-how-should-degree-minute-second-notation-reflect-the-positiv https://gis.stackexchange.com/questions/211796/if-degrees-is... I made the above post due to a bug in the GPS on our routers. We had a customer close to the meridian in England. http://www.thegreenwichmeridian.org/tgm/articles.php?article=5 http://www.thegreenwichmeridian.org/tgm/articles.php?article... Western hemisphere is negative longitude, eastern hemisphere is positive. If your degrees are 0 (within ~100km? I can't remember exactly), then you still need to know if you're east or west of the meridian.
- boomlinde 6y ago> Does anyone have a good example of a programming problem where we _need_ signed zero? Or where it makes things significantly simpler? Maybe when you're implementing floating point numbers in a way that's simple and widely applicable? I'm only half joking, too, though I can't tell you what exactly having a distinct sign bit simplifies. I can say off the bat that it is probably useful that a very small quantity that might otherwise lose enough precision to round to zero maintains its sign regardless.