14 ms·
In Erlang/OTP 27, +0.0 will no longer be exactly equal to -0.0
- EdSchouten 4y agoQuite amazing how so many environments of floating point values make the mistake of treating them as equal. YAML 1.2 does the same thing: https://yaml.org/spec/1.2.2/#10214-floating-point https://yaml.org/spec/1.2.2/#10214-floating-point
- ilyt 4y agoBecause they are equal in real math
- Taniwha 4y agoYes but this is floating point - +0.0 represents all numbers between 0 and the smallest +ve FP number, -0.0 represents all numbers between 0 and the smallest -ve FP number - both can represent actual 0 - remember these can be generated by underflow so those meanings can be important in context
- zokier 4y agoFloats are not real numbers though
- deleted 4y ago[deleted]
- garbagecoder 4y agoThey are a way of approximating Real (the set not as an adjective) numbers, though. Real numbers are exact when they are equal to an integer or a rational, because, depending on how you construct them, either that's what the cut contains or that's where the series converges. And also because that's what the most common spec demands.
- recursive 4y agoFloats are a subset of the reals.
- yifanl 4y agofloating point math is math like any other, just with a vastly different number system. There are valid arguments on both side if we should hide or expose those differences in the implementation, but there's always going to be pretty big implications (i.e. the compiler behaviour mentioned here)
- murderfs 4y agoIs it surprising that so many environments that use IEEE 754 floating point numbers obey IEEE 754 on equality? IEEE 754-2008 5.11: "Comparisons shall ignore the sign of zero (so +0 = −0)."
- cesaref 4y agoAs a general rule, if you find yourself comparing floating point numbers, that's probably not what you want. I'm wondering what they are going to do with other operators, >=, <= etc
- ssivark 4y agoThis smells like the right way to think about things. Whenever thinking of real life measurements represented as real numbers, equality is always fuzzy — up to some resolution, and it would be better to make that explicit.
- nawgz 4y ago> if you find yourself comparing floating point numbers, that's probably not what you want Can you motivate this with an example for me? For instance, I think of games or location data encoded as FP; then, clearly, comparing them is a critical task to know questions like "what is closer" and so on. What am I missing?
- stefncb 4y agoThey meant using the equal operator, because floating point is inexact and can produce different representations for the same number depending on how it was obtained. Greater and less than are fine. Equal is usually implemented by seeing if a number fits inside a tight range.
- recursive 4y agoEvery finite floating point value precisely represents an exact quantity. Arithmetic isn't lossless though. This isn't a special property of floats. Decimals behave the same way.
- nawgz 4y agoI was with you until the end First point: FPs are unique. Clearly true Second point: Combing FPs isn't lossless, across most/all arithmetic. Clearly true, the root of our discussion Third point: decimal arithmetic is lossy too. Strongly disagree. The system of representation is what is lossy - floating point. Arithmetic (between two decimal numbers) is clearly not lossy in and of itself, only as implemented with computers.
- yccs27 4y agoCan anyone fill me in what the meaning of == vs =:= in Erlang is? Since after the change +0. == -0. but not +0. =:= -0.
- ollien 4y ago=:= is supposed to be "exactly equals". An example given in the docs[1]: 1> 1==1.0. true 2> 1=:=1.0. false You can think of it as analogous to == vs === in JS, except that the "type safety" here really only refers to different numeric types (and in JS there is no such distinction) [1]: https://www.erlang.org/doc/reference_manual/expressions.html#term-comparisons https://www.erlang.org/doc/reference_manual/expressions.html...
- duskwuff 4y agoThis feels like a bad idea. Treating +0 and -0 as unequal because they have different bit patterns is no more correct than treating two NaNs as equal because they have the same bit pattern. Zero is zero.
- jbverschoor 4y agoUnless you’re approaching the limit of 0 from one side But yeah, you’re right
- andrewmcwatters 4y agoZero is zero, but signs are a direction. There are multiple parts to numbers. Stand in place and turn around: how far did you go from where you started? Nonetheless, I agree. It's a bad idea.
- quietbritishjim 4y agoBy that logic there should be e.g. +4.0 and +4.0- (for when you go 4m forwards vs when you go 4m forwards but also turn around at the end).
- andrewmcwatters 4y agoIn vector maths, this would just be +4.0.
- Dylan16807 4y agoAnd the same is true of zero. Especially when you consider a two-dimensional vector. How would you encode/interpret facing in a complex zero?
- mananaysiempre 4y agoIEEE 754’s idea was that if you got a zero in a floating-point calculation (which, at the time, meant one with physical quantities), it’s probably underflow, not a legitimate result, so the FPU might as well try to give you at least a sign even if it can’t give you any significant digits. Though 754 says comparison must treat positive and negative zero as equal nevertheless, you have to deliberately choose to look at the sign (with e.g. C99’s copysign) to see the difference.
- zokier 4y agoThe word 'exactly', omitted from HN title, is relevant here: > The == operator will continue to return true for 0.0 == -0.0.
- dang 4y agoWe've re-exactlied it. Thanks!
- codegeek 4y ago-0.0 = 0 +0.0 = 0 Hence -0.0 = +0.0 right ?
- munk-a 4y agoYup - both of the bit patterns for 754 here were assigned the value of 0 and the standard instructed comparison operators to treat the two values as equal. The bit patterns for both -0.0 and +0.0 are assigned to the same real world value - there is no reason to distinguish them outside of circuits that actually execute floating point arithmetic.
- kragen 4y agoit's important to distinguish them in programs written for those circuits and in compilers that transform those programs because they have significantly and intentionally different behavior
- ComplexSystems 4y agoThere is a huge misconception here which really should be clarified in the title. Erlang has an "exactly equal" or strict equality operator, called =:=, which is different from just usual equality, which is ==. The usual equality operator will keep having +0.0 = -0.0, and floating point arithmetic will keep behaving as you would expect... It's that we no longer have +0.0 =:= -0.0, which is a very different thing (and frankly, makes sense - there should be some special operator that can differentiate between these two values).
- Trufa 4y agoWhat’s the use case for different values?
- lliamander 4y agoIt's a natural consequence of the IEEE 754 standard for floating point numbers.
- Tuna-Fish 4y agoTo be clear, it was added to the standard because there was user demand for it. Zero is already special cased in IEEE 754 (along with the rest of the denormals, because of the implied leading mantissa bit), the implementation would be no harder if you just only had a single zero. However, back when the standard was being created, when a single zero value was proposed there was pushback from people in the user community who depended on knowing which side of the zero they underflowed on their own floating point implementations. Many people think that negative zero is just an engineering artefact. This is not true, it is a feature that was asked for, debated, and then added... ... almost certainly because it was an engineering artefact in a previous system that someone managed to depend on, https://xkcd.com/1172/ https://xkcd.com/1172/ style.
- munk-a 4y ago> Many people think that negative zero is just an engineering artefact. This is not true, it is a feature that was asked for, debated, and then added... Allowing zero to be represented with two bit patterns was a feature that was advocated for to make bit operations easier - it was not decided that both zeroes should be distinguishable. This absolutely is an engineering artifact but you're quite correct that it was quite intentional.
- jbgreer 4y agoAs someone who programmed assembly on a one's complement machine* 40 years ago, this discussion is interesting, from both the "haven't we learned anything" and the "makes sense to me" camps. More interesting to me is how they are introducing this change, both in the previous OTP and next, and how they will arm people with tools to hopefully identify and understand the impact. I wonder how many folks will actually be impacted? * Sperry Univac 1100/62
- bjourne 4y agoNegative zero is very, very annoying because it means that many floating point identities flies out of the window. x + 0.0 != x, x * 0.0 != 0.0, etc. It makes it much harder for the compiler to optimize arithmetic. Thus, at least some SIMD circuits do not follow ieee754 to the letter because it would result in performance degradations.
- stkdump 4y agoThose identities do hold though, as long as zero and negative zero compare equal, which according to ieee754 they should. Performance degredations happen in other places such as denormals.
- kragen 4y ago1/0!=1/-0
- pklausler 4y agoI find it so strange that IEEE-754 SQRT(-0.) is -0., even though (-0.)*(-0.) is +0.
- userbinator 4y agoOne wonders how different the world would be if IEEE754 decided to use two's complement instead of sign-magnitude. Or alternatively, if two's complement was rare and almost all mainstream hardware (due to some historical reason with inertia) was sign-magnitude.
- civilized 4y agoA lot of people are arguing that 0 is not a distinct number from -0 in regular math. But regular math doesn't have +Inf, -Inf, or NaN either.
- fijiaarone 4y agoGive Erlang/Elixir the full JavaScript!
- MarekKnapek 4y agoWhat about NaN? Does Erlang have NaN? How does Erlang compare two variables containing a NaN (or expressions evaluating to a NaN)? There are (2^24)-2 different NaNs in a 32bit IEEE 754 float, and there are (2^53)-2 different NaNs in a 64bit IEEE 754 double. The IEEE 754 standard says, that a NaN value should compare to any other number (including any other NaN) as not equal. Yes, the expression `x == x` could return `false`.
- klodolph 4y agoErlang has =:= and ==. Kind of like how Python has is and ==, JavaScript has == and ===, and Lisp has eq, eql, and equal. The change in the article is about =:=, so NaN =:= NaN does not have to give the same answer as NaN == NaN.
- okaleniuk 4y agoThis makes sense. They are different entities and you have to have a way to acknowledge that they are. Breaking strict equality seems a fitting way to do so. In Geometry for Programmers, I reference a case when an automated test system caught a "zero-not-zero" bug, and I couldn't reproduce it with a unit test until I finally realized that the test system was comparing XMLs not numbers. In print, "-0." and "0." are, of course, different, although in runtime they pretend to be the same.
- zaphirplane 4y agoI would have guessed that a backwards incompatible change like that is a big release thing. Even if it affects a tiny tiny group
- di4na 4y agoIt is, hence why it will be in a major release in more than a year, with an intermediate warning mode in releases before that.
- im3w1l 4y agoHot take as a non-user of Erlang. There may be arguments for and against 0.0 =:= -0.0. You could probably do either. But I would be terrified of changing from one to another.
- rpaddock 4y agoObscure Factoid: It is illegal to display -0 on a scale in Canada. Ran into that long ago when designing electronics for a scale.