12 ms·
How does your programming language handle “minus zero” (-0.0)?
- bendiksolheim 6y agoDoes 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.
- kaladin_1 6y agoElixir does not distinguish signs in zero, 0. Therefore; -0.0 + 2.0 = 2.0 and 0.0 + 2.0 = 2.0
- kzemek 6y agoIsn't that true for all correct implementations of negative zero? It's still a neutral for addition
- worc3131 6y agoNot quite neutral. You return -0.0 only when needed. >>> 0.0+0.0 0.0 >>> 0.0+(-0.0) 0.0 >>> (-0.0)+0.0 0.0 >>> (-0.0)+(-0.0) -0.0
- thaumasiotes 6y ago> Not quite neutral. Hmm? Your examples show -0.0 being additively neutral in every case. +0.0 is the one that behaves weirdly.
- shultays 6y agoheh, looks like abs(-0.0) is slightly less than abs(+0.0)!
- chrisseaton 6y agoI don't understand what you think a result could look like here that does distinguish the sign? What are you expecting? -2.0? That doesn't make any sense. 2.0 is the correct answer under all circumstances as far as I can see.
- mcintyre1994 6y agoI think you're right that they're indistinguishable, in that -0.0 == 0.0 The division by 0 in that article gives the same ArithmeticError in each case, notice the 0.0 in both errors: iex(8)> 1.0 / -0.0 ** (ArithmeticError) bad argument in arithmetic expression: 1.0 / 0.0 :erlang./(1.0, 0.0) iex(8)> 1.0/0.0 ** (ArithmeticError) bad argument in arithmetic expression: 1.0 / 0.0 :erlang./(1.0, 0.0) It's also ignored when rounding: iex(10)> Float.round(-0.01, 1) 0.0 iex(11)> Float.round(0.01, 1) 0.0
- croes 6y agoThe inverse of zero is infinity? Shouldn't it be undefined?
- banachtarski 6y agoThe limit of a function 1/x as x approaches zero from the left or right is well defined as negative or positive infinity respectively. The limit is only undefined when no approach direction is specified (as the results from the left and right do not agree)
- thaumasiotes 6y agoNot if you know that it's positive or negative. The inverse of a negative infinitesimal can't be anything other than a negative infinite number, and the inverse of a positive infinitesimal can't be anything other than a positive infinite number. There's no difficulty with the definitions.
- ben509 6y agoThere is the difficulty that you don't actually have zero, you have postive and negative infinitesimal.
- thaumasiotes 6y agoThat is the meaning of "+0.0" and "-0.0". Actual zero is neither.
- deleted 6y ago[deleted]
- marcosdumay 6y agoFloating point numbers represent the extended real set, where infinity exists. What is completely different from the integral numbers most computers use, on those division by zero is undefined.
- tomkaos 6y agoDoes -0.0 == +0.0 ?
- The_rationalist 6y agoIt does
- FartyMcFarter 6y agoYes, at least in C++: https://godbolt.org/z/afqYbE https://godbolt.org/z/afqYbE Its bitwise representation is different, but when compared as floats they are equal.
- posterboy 6y agothe way c does because it is written in c++ using scanf lulz
- SloopJon 6y agoIt's also worth understanding whether +0.0 and -0.0 are equivalent in your environment: * Do they compare equal? * If they're convertible to boolean, do they both evaluate to false? * If you serialize/deserialize the value (e.g., to JSON and back), does it maintain the distinction? * If you send this value to a database (esp. one with stored procedures), how does its behavior compare to your language?
- sir_nop 6y agoThere are still (a few) computers in the world (Sperry Univac legacy support) using one's complement arithmetic, hence having a positive and negative zero in the architecture. https://en.wikipedia.org/wiki/Ones%27_complement https://en.wikipedia.org/wiki/Ones%27_complement
- quelsolaar 6y agoWell all computers supporting IEEE 754 floating point numbers have a positive and a negative 0. Thats quite a few. I prefer to store all my most important data in the sign bit of floats set to zero.
- faeyanpiraat 6y agoWhy? Isn’t that what bool is for?
- cbsks 6y agoYou missed the invisible sarcasm tag
- faeyanpiraat 6y agoˢᵒʳʳʸ
- deleted 6y ago[deleted]
- quelsolaar 6y agoIts also the key to solving this: How is the result of: a = 0; different from: a = -0; :-)
- The_rationalist 6y agoI was just thinking about this 2 days ago, the coincidence!
- TorKlingberg 6y agoI feel like floating point is a completely separate branch of programming that a lot of coders never use. When they do it's often a mistake, like for currency. Yet floating point is very useful for scientific calculations and simulations, which is what computers were all about for the first couple of decades. I have a suspicion that on a fundamental level, floating point isn't actually good for games or machine learning. They're just used because existing computers are so good at floating point number crunching, especially GPUs.
- linkdd 6y agoIn some game engines, Fixed Point Arithmetic is used instead of Floating Point, because it's faster, and the approximation is generally good enough for a game.
- quelsolaar 6y agoI dont think this is true. Integer math is used in some game engines because they need to be deterministic, between networked computers (and different CPUs rounds floating point numbers differently). I have written an engine like that, and i know that Starcraft 2 is all integer for the same reason. No one does it because its faster or easier. Its a pain.
- linkdd 6y agoI never said it was easier and I never said ALL game engines. And computing integers IS faster than computing floats, at least on a CPU.
- quelsolaar 6y ago>And computing integers IS faster than computing floats, at least on a CPU. In the abstract yes. In reality not so much. A lot of what floats are used for in games is vector math (you are often in a 2D/3D world), and vector math in fixed point requires a lot more work in integer math since you constantly need to shift down things in order to avoid overflow. Overflow bugs are a constant problem when doing dot products in fixed point integer math. Another common operation in vector math is square root (to normalize vectors) Modern CPU's do that very fast, and you can also use clever approximations that use the way floating point's are represented in hardware. Most of whats done in 32 bit floats in games, needs to be done with 64bit integers to manage these problems if you decide to use integers, and that means that your data grows, more cache misses, and things slow down. On top of this everything you do in a game needs to be drawn on the GPU so you need to convert everything to floats anyway to show them on screen, and converting between integers and floats is also slow.
- d3nj4l 6y agoRuby seems to handle this correctly: >> 1 / -0.0 => -Infinity Although you have to put the 0.0, otherwise it treats it as an integer and makes it 0.
- darig 6y agoShouldn't the answer be NaN or an illegal division by 0 fatal error/exception?
- fortran77 6y agoDividing by zero is "undefined" not "Infinity" because there is not a "well defined limit." https://en.wikipedia.org/wiki/Division_by_zero https://en.wikipedia.org/wiki/Division_by_zero
- d3nj4l 6y agoFrom the article you link: > Depending on the programming environment and the type of number (e.g. floating point, integer) being divided by zero, it may generate positive or negative infinity by the IEEE 754 floating point standard.
- sesutton 6y agoInterestingly Go will give you a negative 0 if you use strconv.ParseFloat("-0.0", 64) but I can't see any way to get -0.0 from a number literal.
- jeffbee 6y agoYou can't get a negative zero with a constant declaration, nor can you get NaN, infinity. These are documented language facts. """Numeric constants represent exact values of arbitrary precision and do not overflow. Consequently, there are no constants denoting the IEEE-754 negative zero, infinity, and not-a-number values."""
- magicalhippo 6y agoI recall there being an issue with double.GetHashCode() in the early versions of .Net, where it would return different hash values for 0.0 and -0.0. That got fixed, but it seems a variation with signed NaN recently got fixed[1] in .Net Core (taking care to handle all the possible NaN values). Floats are deceptively easy to use. Which is nice but kinda sucks too, given all the footguns they bring to the party. [1]: https://github.com/dotnet/runtime/issues/6307 https://github.com/dotnet/runtime/issues/6307
- TacticalCoder 6y ago> I recall there being an issue with double.GetHashCode() in the early versions of .Net, where it would return different hash values for 0.0 and -0.0. I never thought about that (I avoid FP as much as I can) but, oh boy, the can of worms! .Net (or Java) OOP where an object's hashcode needs to give the same (or not?) value for 0.0 or -0.0: this is the kind of stuff nightmares are made off. I'm sure this can be turned into some very funny "Java puzzler".
- mattgreenrocks 6y agoThe fact that NaN is technically a set of values in IEEE-754 (rather than just one agreed-upon value) was quite eye-opening to me.
- emergie 6y agoWhat is wrong in having different hashes for different numbers? https://float.exposed/0x0000000000000000 https://float.exposed/0x0000000000000000 https://float.exposed/0x8000000000000000 https://float.exposed/0x8000000000000000
- magicalhippo 6y agoIf you compare for equality then those two numbers, 0.0 and -0.0, are considered equal. It seems a lot of people consider that a good reason for them to have the same hash value, which I consider entirely fairly reasonable. Of course since NaN != x, whatever x is (including infinity and NaN), one could argue it's fine for different NaNs to return different hash codes. But I think most people think of NaN as one thing, and not the 9007199254740990 or so different NaN encodings[1] there are in a double. [1]: https://en.wikipedia.org/wiki/IEEE_754-1985#NaN https://en.wikipedia.org/wiki/IEEE_754-1985#NaN
- JoeAltmaier 6y agoSigned infinity is nowhere near the 'expected result'. Zero has no sign. NAN would be a better result off the top of my head. But I see from the standard that dividing by zero returns infinity with the xor of the dividend and divisor. This also makes no sense - since zero has no sign, its not possible to tell what kind of infinity would result. It's strange that the standard permits both the infinity result from dividing by zero, and simultaneously supports signed zero. <edit: add xor>
- pfsalter 6y agoPHP just refuses to divide by zero: php > $minus_zero = -0.0; php > $plus_zero = +0.0; php > var_dump(1.0 / $minus_zero); PHP Warning: Uncaught DivisionByZeroError: Division by zero in php shell code:1
- deleted 6y ago[deleted]
- bakje 6y agoActually as shown by your output it doesn't completely refuse to divide by zero, a warning means that execution continued. The result of that division is a float with a value of -INF, INF being a constant that PHP treats as infinite. But as of PHP 8 dividing by zero causes a fatal error and execution is halted. This site is really good for comparing results in different PHP versions: https://3v4l.org/9Tl1I https://3v4l.org/9Tl1I I actually wasn't aware that PHP had an INF contant but seeing the warning in your output prompted me to dig a little bit deeper :)
- johndoe42377 6y agoSince definition of zero is x - x, negative zero is utter bullshit.
- robert-brown 6y agoCommon Lisp handles negative zero quite well.
- ksaj 6y agoUnless it is clisp, which doesn't implement -0 even in floats.
- retrac 6y agoAs an aside, historically many of the early binary computers used ones complement. So they had both -0 and +0 integers as well. While ones complement is probably more obvious than twos complement, it was near-universally considered a terrible mistake by the mid-1960s, and everything of note since has been twos complement. It didn't die quickly though. The UNIVAC is still with us (in emulation) and that is likely the reason why the C standard addresses the question of negative zero integers. (Their handling is implementation specific, of course.)
- dnautics 6y agothe Erlang VM said "screw IEEE": iex(1)> -0.0 === 0.0 true there's also explicitly no infinity or NaN, there is a software throw for all (core) implemented function domain failures. This has, however, recently come up for Nx (numerical elixir) which had to implement and standardize ways to shim these IEEE concepts back into the VM for interop purposes.
- recursive 6y agoIs there any language that treats the zeros as unequal? That would seem like the real "screw IEEE".
- dnautics 6y agoin the specific case of erlang, and the erlang beam vms, 0 !== 0.0 Of course 0 == 0.0 I imagine if they had chosen to follow IEEE, it would have been that 0.0 !== -0.0 and 0.0 == 0.0.
- recursive 6y agoI don't know erlang, but it looks to me like that's a difference between types. (integer vs float) Is there a language that treats different float values of zero as unequal? That would be surprising to me. And possibly silly.
- dnautics 6y agoWhat a guess! I guessed "Julia" and that is correct: https://stackoverflow.com/questions/38601141/what-is-the-difference-between-and-comparison-operators-in-julia https://stackoverflow.com/questions/38601141/what-is-the-dif... In Julia 0.0 == -0.0 but 0.0 === -0.0 evaluates to false. Note that -0.0 and 0.0 are different at the binary representation level, which is what === checks for.
- OneWingedShark 6y ago> This has, however, recently come up for Nx (numerical elixir) which had to implement and standardize ways to shim these IEEE concepts back into the VM for interop purposes. This would have been an excellent place to use Ada. The package "Interfaces" has types exactly for IEEE Floats: `Interfaces.IEEE_Float_64`. You could then get rid of non-numeric representations (raising `Constraint_Error` instead) via the following subtype-definition: subtype Float is Interfaces.IEEE_Float_64 range Interfaces.IEEE_Float_64'Range; (I've thought about writing an Erlang in Ada, hoping to tie together Erlang's actors w/ Ada's Task where possible [ie focus on interop], but haven't found any Erlang language definition that's anywhere near recent.)
- brian_herman 6y agoPython Produces the following: minus_zero = -0.0 plus_zero = +0.0 parsed = float("-0.0") print(1/minus_zero) print(1/plus_zero) print(1/parsed) ZeroDivisionError Traceback (most recent call last) <ipython-input-1-747f1b708c86> in <module>() 2 plus_zero = +0.0 3 parsed = float("-0.0") ----> 4 print(1/minus_zero) 5 print(1/plus_zero) 6 print(1/parsed) ZeroDivisionError: float division by zero edit: Formatting