News for hackers. Stuff that matters.
Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by cesaref on Tuesday May 09, 2023 @04:13PM (#35879777)
As 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

Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by ssivark on Tuesday May 09, 2023 @04:20PM (#35879876)
This 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.
Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by wslh on Tuesday May 09, 2023 @04:30PM (#35879984)
Floating point numbers are dangerous but in 2023 we should find a way to improve the side effects.

Playing with Python3:

>>> +0.0==-0.0 True

The C in GCC below also returns 1:

#include <stdio.h>

int main()

{

        float a = +0.0;
        float b = -0.0;
        printf("%d\n", a == b);
}
Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by Y_Y on Tuesday May 09, 2023 @05:32PM (#35880645)
Erlang too, now and in the future will say that minuszero==zero, it's a different more specific operator that's changing.
Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by Kwpolska on Tuesday May 09, 2023 @06:01PM (#35880943)
The `is` operator is checking object identity by comparing addresses. It is useless, although it may sometimes produce reasonable-looking results due to optimisations.

Python 3.11 gives me this:

>>> -0 is 0 <stdin>:1: SyntaxWarning: "is" with a literal. Did you mean "=="? True

Because (a) -0 is an integer, and there is only one integer zero, (b) small numbers have only one instance in memory as an optimization.

Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by atemerev on Tuesday May 09, 2023 @04:28PM (#35879965)
We don't have fast hardware decimals, except on IBM mainframes. So, if you need fast financial calculations which are too complicated for integers (e.g. gasp division), you usually use decimally-normalized doubles and do it very very carefully.

This is a sad state of affairs, but nobody was fixing this in the last 30 years, so there's that.

Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by cubefox on Tuesday May 09, 2023 @04:34PM (#35880033)
Are there even any applications where one needs a) decimal precision and b) fast computation? Financial calculations don't need to be fast.
Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by zwischenzug on Tuesday May 09, 2023 @04:41PM (#35880115)
Gambling.
Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by nvy on Tuesday May 09, 2023 @04:37PM (#35880065)
High-frequency trading, perhaps.

Physics simulations too.

Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by wtallis on Tuesday May 09, 2023 @04:46PM (#35880159)
Physics simulations are a great example of where there's obviously no need to do arithmetic in decimal rather than binary floating point.
Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by atemerev on Tuesday May 09, 2023 @04:38PM (#35880081)
Algorithmic trading, of course. High-frequency trading in particular. But any trading, in fact, when you run simulations for thousands of instruments and search among millions of parameters.
Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by justeleblanc on Tuesday May 09, 2023 @05:30PM (#35880633)
So something that produces nothing of value to civilization. Gotcha.
Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by atemerev on Tuesday May 09, 2023 @05:38PM (#35880718)
Well, I assume you want to sell your hard-earned stocks of civilization-helping companies sometimes...
Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by justeleblanc on Tuesday May 09, 2023 @06:42PM (#35881289)
Nope. I'd rather there not be a thing called a stock market at all.
Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by nequo on Tuesday May 09, 2023 @07:08PM (#35881538)
Do you just plan to save your money in dollar bills hidden inside a pillow until you retire? If you want to invest it in any way, you probably want to avoid floating point numbers for keeping track of it.
Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by justeleblanc on Tuesday May 09, 2023 @08:36PM (#35882182)
I'd prefer living in a world where social security is enough for me not to worry about things like "saving money".
Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by atemerev on Wednesday May 10, 2023 @01:32AM (#35883986)
Well, communism worked juuust fine the all the times it was tried. And was quiiiite good for civilization.
Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by epgui on Wednesday May 10, 2023 @08:26AM (#35886351)
So no pensions, sovereign wealth funds, or scholarship funds? And by what mechanism is capital allocated?
Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by justeleblanc on Wednesday May 10, 2023 @09:03AM (#35886728)
Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by atemerev on Wednesday May 10, 2023 @11:47AM (#35889075)
Well, prepare the firing squads! What the world definitely needs is more fire squads, I presume.
Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by epgui on Friday May 12, 2023 @07:18PM (#35923734)
I’m not a fan of the response either, but that’s a bit disingenuous.
Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by Dylan16807 on Tuesday May 09, 2023 @07:54PM (#35881875)
High frequency traders effectively charge money to provide liquidity.

But they provide far more than I need. I'm not worried about liquidity when I eventually sell those stocks, and if I could opt out of buying extra liquidity I definitely would.

Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by kragen on Wednesday May 10, 2023 @01:33AM (#35883993)
more liquidity in a security drives spreads down, not up, so you have the effect backwards
Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by Dylan16807 on Wednesday May 10, 2023 @02:35AM (#35884334)
What in particular do you think I have backwards?

If I have a medium size order, then even though they lower the spread they also front-run and limit how much I can buy at that price. So I'd rather have them not be there.

If I have a tiny order, then I don't care what the spread is within reasonable bounds, and I still don't want them to be there.

Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by kragen on Wednesday May 10, 2023 @01:20PM (#35890413)
oh, well, if you're a speculator, often you really do lose money to hfts because they're better speculators than you, but it's unclear why anybody outside your immediate family should care about that

if you're an investor, otoh, the lower spread and greater liquidity means timely execution costs you less, not more. you aren't paying them for liquidity; they're paying you, or rather you're paying them, but much less than the spread you'd've paid an old-style open outcry market maker

(do you even remember markets before decimalization? minimum spread 12.5 cents)

of course you do need to execute intelligently; you can't just plop a million-dollar order in a hundred-million-a-day market and expect the market not to move against you

Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by Dylan16807 on Wednesday May 10, 2023 @02:09PM (#35891104)
> of course you do need to execute intelligently; you can't just plop a million-dollar order in a hundred-million-a-day market and expect the market not to move against you

It's fine for the market to move against big orders, I just want the movement to, like, take one second. I don't want anyone to change their position in response to an order that hasn't even resolved.

> (do you even remember markets before decimalization? minimum spread 12.5 cents)

Well I'm not suggesting we undo that.

> if you're an investor, otoh, the lower spread and greater liquidity means timely execution costs you less, not more. you aren't paying them for liquidity; they're paying you, or rather you're paying them, but much less than the spread you'd've paid an old-style open outcry market maker

In this scenario I'm a long-term investor so the cost means nothing to me. So it's a matter of whether I want HFTs to profit, and I don't, because their actions are often not win-win. If HFT worked somewhat differently I wouldn't mind them the same way.

Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by Zpalmtree on Tuesday May 09, 2023 @09:36PM (#35882590)
liquid markets are a very good thing for civilization
Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by mamcx on Tuesday May 09, 2023 @05:57PM (#35880905)
Business apps? Like all of them?

As matter of fact, business apps are by far the longest portion of the apps. Just considering all spreadsheets, almost all RDBMS, etc alone.

Binary Floating-point is what is ACTUALLY niche. And it not look like it just because is the default, similar how in certain niches "0" is "true".

Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by int_19h on Tuesday May 09, 2023 @10:06PM (#35882789)
Most business apps don't do anywhere near enough compute that it matters.
Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by FpUser on Tuesday May 09, 2023 @04:45PM (#35880149)
I would add generating large financial reports, enterprise billing etc. There is a huge business effect between for example 2 hours needed to calculate monthly invoices and 24 hours.
Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by wtallis on Tuesday May 09, 2023 @04:47PM (#35880172)
Is any of that actually bottlenecked by arithmetic rather than pointer chasing?
Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by syastrov on Tuesday May 09, 2023 @07:14PM (#35881588)
Probably more likely inefficient data access patterns, such as N+1 queries, inefficient algorithms, lack of parallelism
Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by kragen on Wednesday May 10, 2023 @01:39PM (#35890677)
no, 2 hours of addition on my cellphone at 2 gigahertz, 4 cores, and conservatively 3 additions per core per cycle would be 170 trillion additions

nobody needs 170 trillion additions for their nightly invoices

Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by ibiza on Tuesday May 09, 2023 @07:05PM (#35881507)
Power ISA includes decimal floating point, so it's available beyond mainframes.

https://en.wikipedia.org/wiki/Power_ISA

Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by dahjelle on Wednesday May 10, 2023 @07:24AM (#35885936)
I’m not qualified to evaluate it, but Douglas Crockford has at least made a proposal: https://www.crockford.com/dec64.html
Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by nawgz on Tuesday May 09, 2023 @04:22PM (#35879901)
> 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?

Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by jandrese on Tuesday May 09, 2023 @04:37PM (#35880071)
IMHO That was poorly worded. What it should have said is "if you find yourself using equals to compare floating point numbers...". With Floating Point your comparisons should always be less than or greater than. Precision artifacts make the equals unreliable, and you should always be mindful of that when dealing with them.
Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by zdragnar on Tuesday May 09, 2023 @04:32PM (#35880013)
Caveat: In the following, I use "comparison" to mean "check for equality".

Floating point numbers lose precision because binary arithmetic doesn't represent decimal in all cases ( 1/3 is an easy example). It's not hard to get into a situation where you're asking if a number is 0.0 but due to a precision error the number you have is 0.00000000000001 or whatever and it should have been zero.

If you're dealing with anything where precision is paramount (i.e. money or high precision machining) you should consider using something else that matches the real work precision you are trying to model.

You can, of course, use floats and test differences rather than strict equality (i.e. distance is < 0.01) and that's fine too... if you remember to do it, and account for the potential for small precision differences to propagate throughout your calculations and accumulate into larger errors.

Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by tobr on Tuesday May 09, 2023 @04:40PM (#35880099)
> Caveat: In the following, I use "comparison" to mean "check for equality".

You never use the word “comparison” again in the comment!

Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by Someone on Tuesday May 09, 2023 @07:36PM (#35881748)
> Floating point numbers lose precision because binary arithmetic doesn't represent decimal in all cases.

That’s not the reason. A counterexample is decimal arithmetic. That represents decimal in all cases, yet loses precision when doing calculations. Once you start doing division that applies even to arbitrarily sized decimal numbers.

The correct reason is because the subset of the reals that is representable as IEEE float isn’t closed under the operations people want to perform on them, including the basic ones of addition, multiplication and division.

Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by stefncb on Tuesday May 09, 2023 @04:26PM (#35879936)
They 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.
Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by recursive on Tuesday May 09, 2023 @06:49PM (#35881357)
Every 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.
Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by nawgz on Tuesday May 09, 2023 @07:22PM (#35881644)
I 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.

Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by Dylan16807 on Tuesday May 09, 2023 @07:58PM (#35881920)
Decimal is only lossless if you track an indefinitely-growing suffix to your number with a big bar over it to indicate "repeating". It's basically working with rational numbers but more painful.

Decimal numbers with any limit on digits, even a hundred, are lossy.

Decimal numbers as used by humans outside of computers have limits on digits, so they are lossy.

Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by tadfisher on Wednesday May 10, 2023 @12:55AM (#35883778)
Likewise, bit-limited integers are lossy as they are incomplete representations of ℤ, and are not reciprocal with division. The point being that, just like floating-point representations of ℝ, division will always be lossy in the general case unless you choose a symbolic algebra instead of numeric.
Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by nawgz on Wednesday May 10, 2023 @01:59PM (#35890957)
Maybe my point is uninteresting, but I feel you haven't really understood it.

Combining two FP numbers with arithmetic leads to possible errors that you can't represent with FP on every operation.

Combining two decimal numbers with non-division arithmetic never leads to that case. With division, sure, things are bad, but that's more of an exception than a rule to me.

This is because decimal numbers don't really come with any inherent limit on digits, and it's a bit strange to add a clause to the other's claims before making a counterargument.

Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by Dylan16807 on Wednesday May 10, 2023 @02:26PM (#35891348)
Well in part I didn't understand your point because you didn't mention division as an exception.

But even then, multiplication causes an explosion in number length if you do it repeatedly.

When you specifically talk about numbers not being in computers, I think it's fair to talk about digit limits. Most real-world use of decimals is done with less precision than the 16 digits we default to in computers. Let alone growing to 50, 100, 200, etc as you keep multiplying numbers together to perform some kind of analysis. Nobody uses decimal like that. Real-world decimal is lossy for a large swath of multiplication.

I agree that if you're doing something like just adding numbers repeatedly in decimal, and those numbers have no repeating digits, then you have a nice purity of never losing precision. That's worth something. But on the other hand if you started with the same kind of numbers in floating point, let's say about 9 digits long, you could still add a million of them without losing precision.

And nobody has said anything about irrational numbers as you dismissed in your other comment.

So in summary: decimal division, usually lossy; decimal multiplication, usually lossy; decimal addition and subtraction, lossless but with the same kind of source numbers FP is usually lossless too

Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by recursive on Wednesday May 10, 2023 @02:13PM (#35891163)
> Combining two decimal numbers with non-division arithmetic never leads to that case.

Most types are subject to overflow that has similar effects. Most of the FP error people encounter is actually division error. For example, the constant 0.1 contains an implicit division. The "tenths" place is defined by division by 10. I think that almost all perceptions of floating point lossiness come from this fact.

Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by recursive on Tuesday May 09, 2023 @09:24PM (#35882518)
Write 1/3 in decimal. You're required to do it in finite memory.
Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by kragen on Wednesday May 10, 2023 @01:08PM (#35890232)
you seem to be confusing the decimal system of representation with platonic abstract numbers, which is what gives rise to the apparent disagreement
Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by nawgz on Wednesday May 10, 2023 @01:56PM (#35890901)
Do I?

I think people are speaking shockingly imprecisely in this thread. here's my claim:

> Arithmetic (between two decimal numbers) is clearly not lossy in and of itself, only as implemented with computers

Counterarguments are basically on the level of "you cant represent Pi as a finite decimal number", which I find uninteresting.

Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by kragen on Wednesday May 10, 2023 @04:47PM (#35893274)
that's slightly incoherent and reflective of the same confusion i identified; there is strictly speaking, no such thing as a decimal number, only a decimal numeral. normally the difference is subtle, but in this case it's the essence of the issue

'decimal' means 'base 10'

numbers, the abstract entities we do arithmetic on, are not decimal or in any other base; it is correct that arithmetic on them is not in any sense lossy

decimal is a system of representation that represents these abstract numbers as finite strings of digits; these are called 'numerals'. it can represent any integer, but only some fractions, those whose denominator has no prime factors other than 2 or 5. such fractions arise non-lossily as the result of the arithmetic operator of division when, for example, the dividend is 3. representing these in decimal requires rounding them, so decimal is lossy

binary floating point is lossy in the same way, with the additional limitation to only being able to represent fractions whose denominator is a power of 2, whose numerator does not have too many significant bits (53 most often), and which are not too positive, too negative, or too small

there are other systems of represention for numbers that are not lossy in this way. for example, improper fractions, mixed numbers, finite continued fractions, and decimal augmented with overbar to indicate repeated digits

Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by kragen on Thursday May 11, 2023 @10:47AM (#35902946)
this should read 'when, for example, the divisor is 3 and the dividend is an integer not divisible by 3'
Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by HexDecOctBin on Tuesday May 09, 2023 @04:30PM (#35879989)
Ideally, you'll want to do distance calculations in fixed point numbers (of desired resolution). Floating point works well as a first approximation, but unless you can make sure that you are not going to end up wandering in the weeds (and ensuring that required understanding of computational numerics), you should probably replace them with fixed point once you understand the problem and the solution and know the limits involved.
Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by orthoxerox on Tuesday May 09, 2023 @04:28PM (#35879962)
This is specifically about equality. Comparing FPs for equality is very risky, as your numbers can differ by 0.00000000000001 without anyone noticing.

Strict inequality (> and <) comparison are generally fine as long as you avoid NaNs.

Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by nawgz on Tuesday May 09, 2023 @04:31PM (#35879997)
Ok, sure, so it was just for a specific case of comparison.

Well, followup, if some error Epsilon can be introduced during any manipulations, when you check for strict inequalities do you check x < y + Epsilon? Does the language implicitly do it for you?

Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by klodolph on Tuesday May 09, 2023 @05:01PM (#35880337)
The language just gives you the direct comparison.

If you know numerics, then you can come up with the correct value for epsilon. But that is hard work. There are various things that the language could do for you, like use a dynamic amount of precision, or interval arithmetic where the error bounds are saved—but! Most of the time people just want the answer faster and with less memory used, which is what you get with bare floats. The people who make it their job to care about numerical accuracy can do it better than the language runtime would anyway.

Most of the problems are more easily solvable at a higher level anyway. Like, imagine Clippy saying to you, “It looks like you’re inverting a matrix. Are you sure that’s a good idea?”

Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by zokier on Tuesday May 09, 2023 @04:26PM (#35879939)
Common way of comparing floats is to compare the difference to some epsilon value. See Python PEP-485 for example https://peps.python.org/pep-0485/
Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by BlueTemplar on Tuesday May 09, 2023 @04:38PM (#35880075)
It's the hard task of trying to figure out the magnitude of the expected errors (which can accumulate), in the simplest case of a single operation you compare within epsilon distance :

https://en.wikibooks.org/wiki/Floating_Point/Epsilon

Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by flatline on Tuesday May 09, 2023 @04:51PM (#35880206)
It is 2023 and our tooling still encourages the same mistakes people were making 40 years ago. Can we really not have equality operators that do a comparison with a 1% tolerance or something as a sensible default equivalence instead of blatantly wrong bitwise comparison?

I would even be happy with a default set of compiler warnings or errors, which I don’t believe I have ever seen.

Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by colejohnson66 on Tuesday May 09, 2023 @04:56PM (#35880280)
The problem with defining an “epsilon” (1% in your case) is that there is no value that would please everyone. For some, 1% will be fine, but others may need 0.0001%.

The solution is to either use decimal floats if they suit your need (and eat the performance penalty), or to use a linter that flags float comparisons by equality.

Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by dzaima on Tuesday May 09, 2023 @05:18PM (#35880519)
And then there's the question of how you use the epsilon - i.e. whether 1.2e-16 and 1e-20 are "within 1%". Sometimes those are very much different sizes, but if you're comparing sin(π) (i.e. the 1.2e-16) with some other almost-zero computation, they're very much within 1%.
Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by Someone on Tuesday May 09, 2023 @07:27PM (#35881690)
That’s bad enough, but it is not the problem with defining an “epsilon”, it’s a problem.

Another one is that you would lose transitivity on equality. With a 1% epsilon, you would have

   1.000 == 1.005
   1.005 == 1.010
   1.010 == 1.015
 
but

   1.000 != 1.015
You also would have to be careful on how to define that 1% error range. the naive "x is equal to all numbers between 0.99x and 1.01x" would mean 1.0 would be considered equal to 0.99, but 0.99 would not be considered equal to 1.0 (1.01 times 0.99 is less than 1.0)

You also lose that, if a == b and c == d it follows that a + c == b + d.

The behavior around zero also will go against intuition. If you consider 0.99 and 1.0 to be equal, do you really want 1.0E-100 and -1.0E-100 be different?

Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by chubot on Tuesday May 09, 2023 @04:59PM (#35880310)
From a user POV, I think == and === on floats should simply be undefined in any language. It should be a compile time or runtime error.

There can be a separate function `float_equals()` with explicit args that does what people want

The only reason to use the same syntax == is for POLYMORPHIC code that is actually correct.

But it's not going to be correct with floats, because they don't obey the same algebraic laws ... So the syntax should be different!

---

I believe the Erlang compiler optimization presented as justification for this change is a good example

The compiler wants to reason about code, independent of types

But that reasoning about the =:= operator is wrong for the float case.

Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by kragen on Wednesday May 10, 2023 @01:41AM (#35884038)
what people want varies, but often what people doing numerical programming want is to write their monomorphic code in infix syntax so they can more easily see bugs in it

they're already familiar with rounding errors

from that perspective you're suggesting taking a step backwards from fortran i toward assembly language

the erlang compiler bug presented is not an example of what you're talking about because you're talking about arithmetic, and the buggy comparison operator it's using is not an arithmetic comparison operator

Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by chubot on Wednesday May 10, 2023 @10:24AM (#35887836)
Fortran would be an interesting case, because say C++ is polymorphic, and so are all dynamic programming languages with more than 1 type (x == y could be a string or float comparison)

But I would still say that floating point equality is a vanishingly rare operation

I can't think of any real use case for it, other than maybe some (bad, incomplete) unit tests that assert 1.0 == x and assert 1.0 == y

And that use case is perfectly served by float_equals(), and arguably served better if it has some options. Although probably abs(x - y) < eps is just as good, and you don't even need float_equals()

---

Can you show some real use cases for floating point equality in good, production code? (honest question)

It's similar to hashing floats, which Go and Python do allow. I made an honest request for examples of float hashing:

https://lobste.rs/s/9e8qsh/go_1_21_may_have_clear_x_builtin#...

https://old.reddit.com/r/ProgrammingLanguages/comments/10bm2...

I didn't get any answers that appeared realistic. Some people said you might want to create a histogram of floats -- but that's obviously better served by bucketing floats, so you're hashing integers.

Another person said the same thing for quantizing to mesh.

One person suggested fraud detection for made-up values, but that was clearly addressed by converting the float to a bit pattern, and hashing the bit pattern.

----

This isn't a theoretical question since we're working on this for https://www.oilshell.org.

For that case it seems pretty clearly OK to omit === on floats, since I use awk and R for simple stats and percentages, and that's likely what people would do with a shell.

Though as always I'm open to concrete counterexamples.

Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by kragen on Wednesday May 10, 2023 @12:13PM (#35889463)
i gave some examples in https://news.ycombinator.com/item?id=35883963, though arguably those are not very concrete. also i pointed out there why unit tests that use exact comparison on floats are not necessarily bad: ieee-784 specifies bit-exact results for the five fundamental arithmetic operations, and if your compiler is introducing rounding errors, you want your tests to fail. (or introducing nonerrors, in the case where it's introducing fmas.)

even hashing is useful for memoization

i think probably you're asking in the wrong places

the lapack source might be a better place to look; we can probably suppose that if something is in lapack it's realistic and also not a bug

in dgelsx for example i find

          ELSE IF( ANRM.EQ.ZERO ) THEN
    *
    *        Matrix all zero. Return zero solution.
    *
             CALL DLASET( 'F', MAX( M, N ), NRHS, ZERO, ZERO, B, LDB )
and also (fairly dubious, at least to my uneducated eye)

    *     Determine RANK using incremental condition estimation
    *
          WORK( ISMIN ) = ONE
          WORK( ISMAX ) = ONE
          SMAX = ABS( A( 1, 1 ) )
          SMIN = SMAX
          IF( ABS( A( 1, 1 ) ).EQ.ZERO ) THEN
             RANK = 0
             CALL DLASET( 'F', MAX( M, N ), NRHS, ZERO, ZERO, B, LDB )
             GO TO 100
          ELSE
             RANK = 1
          END IF
as well as some sentinel-value checks

and in dlar1v:

                WORK( INDS+I ) = S*WORK( INDLPL+I )*L( I )
                IF( WORK( INDLPL+I ).EQ.ZERO )
         $                      WORK( INDS+I ) = LLD( I )
and also

                TMP = D( I ) / DMINUS
                IF(DMINUS.LT.ZERO) NEG2 = NEG2 + 1
                WORK( INDUMN+I ) = L( I )*TMP
                WORK( INDP+I-1 ) = WORK( INDP+I )*TMP - LAMBDA
                IF( TMP.EQ.ZERO )
         $           WORK( INDP+I-1 ) = D( I ) - LAMBDA
and also

          IF( ABS(MINGMA).EQ.ZERO )
         $   MINGMA = EPS*WORK( INDS+R1-1 )
of course float comparisons for ordering are much more common than float comparisons for equality, but there are numerous realistic examples of float comparisons for equality in lapack and, i think, in applied mathematics in general

but if you ask an audience of type theorists, sysadmins, and web developers, they won't know that; they aren't numerical analysts and probably haven't inverted a submatrix since sophomore math class, if ever

(there are 6550 files in lapack 3.9.0 of which i looked through 15 to find these examples, suggesting that there are on the order of 1000 realistic examples in there; eventually i'd probably find one that wasn't even comparing to zero)

plausibly instead of giving them floating point by default you should give them fixed point with, say, nine digits after the decimal point; that was the approach knuth took in tex, for example, to guarantee reproducibility, but it also gives human-predictable rounding. and in most cases it should be perfectly adequate for simple stats and percentages. as a bonus you get 64 bits of precision instead of 53

Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by kragen on Thursday May 11, 2023 @10:43AM (#35902877)
Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by chubot on Thursday May 11, 2023 @11:18AM (#35903423)
Thanks for actually going and looking! I would say that this supports the idea of float_equals(x, 0.0) or even float_equals_zero(x) :)

It wouldn't be too surprising to me if 90% of float equalities in real code are comparisons to 0.0 or 1.0 or -1.0 (that's what I meant in my original post, there was a typo)

The point about x >= 0 and x <= 0 is interesting. I suppose to be consistent, you would also remove those operators on floats, although for something awk-like, I don't think it matters either way.

---

The other point is that it looks like the Fortran code isn't actually using == or x.eq.zero polymorphically!

So the first point is that I would want to understand conceptually where float equality would be equal. You mentioned fixed points, although there I also suspect that abs(x - y) < eps is a better test in most situations, though certainly there could be counterexamples.

The second point is -- would any of those situations be polymorphic with int and float, or string and float? It depends on the language, but it seems vanishingly unlikely in most languages, and in particular in say in C++

I would probably go look at the equivalent Eigen code if it mattered with respect to Oil, but it probably doesn't

Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by kragen on Thursday May 11, 2023 @01:44PM (#35905579)
yeah, i also wrote a cube root routine using newton's method yesterday and came to the conclusion that |x - x'| < threshold was a better approach in that case. maybe in every case, maybe not

i agree that prefix notation is not as bad for these as i had thought before looking

i think there are plausible cases where your float might be polymorphic with double, complex, a vector, a matrix, or an element of a galois field, but i think it would only be polymorphic with int in cases like stopping change propagation when an observable value gets recomputed to the same value from new inputs, and in those cases what you need is exact bitwise equality, not arithmetic equality

an awk-like thing might be better with fixed-point, like tex and metafont; you could call the data type 'btc` and represent it as a 64-bit integer that's multiplied by 10*-8 for output

Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by Dylan16807 on Tuesday May 09, 2023 @07:50PM (#35881841)
The tolerance you need depends on how you calculated the number, so there's no way to have a sensible default.
Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by jacquesm on Wednesday May 10, 2023 @06:46AM (#35885718)
> Can we really not have equality operators that do a comparison with a 1% tolerance or something as a sensible default equivalence instead of blatantly wrong bitwise comparison?

Then it wouldn't really be an equality comparison any more. For instance according to your rule 1.0 and 1.01 are the same number, but they clearly are not. But if you want to do this regularly you could create a special type and overload the equality operator (if your language supports it, otherwise you'll have to call the function directly) that points to 'boolean about_equal(float f1, float f2, float epsilon)' or something equivalent where epsilon is the amount that you would allow f1 and f2 to deviate from each other to still call them equal. And you could define epsilon as ((abs(f1) + abs(f2)) / 100) .

The bitwise comparison isn't 'blatantly wrong', it's the expected behavior in just about every programming language. I'm not aware of any language that has a native float operator that does this but as you can see you can usually simply add one yourself if you really need it. But this need rarely comes up and usually indicates that you are doing something wrong.

Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by regularfry on Tuesday May 09, 2023 @04:52PM (#35880216)
I've always held that the numbers themselves are perfectly precise. It's the operations that don't do what you expect.

Of course, that observation may be more or less useful, depending on circumstances.

Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by ChainOfFools on Tuesday May 09, 2023 @06:33PM (#35881216)
Somewhere in the distant past a second grade me is staring at his math homework and fuming over the frustratingly ambiguous meaning of the minus sign. Is it an operator? A property? Both?
Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by thr-nrg on Tuesday May 09, 2023 @06:49PM (#35881361)
Math notation is to mathematics as poetry is to English.

The only acceptable grammar for math is s-expressions.

Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by Conscat on Tuesday May 09, 2023 @11:22PM (#35883269)
Well, idiomatic C++23 is an acceptable math notation too.
Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by thr-nrg on Wednesday May 10, 2023 @05:09AM (#35885265)
No it isn't. S-expressions are the simplest serialisation or trees. Trees are the simplest grammar that can encode mathematical expressions. C++ by comparison is as usable as doing cube roots using roman numerals.
Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by Dylan16807 on Tuesday May 09, 2023 @07:45PM (#35881806)
It's only ambiguous if you think it does two things. I say it does one thing.

-4 always removes 4, whether or not there is a number to the left.

Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by ChainOfFools on Tuesday May 09, 2023 @07:54PM (#35881878)
The problem arises with the way that this stuff is taught at a very elementary level, which then needs to be thrown away and relearned long after intuition and habits have formed around the old model. As an adult my reasoning is that it does indeed only do one thing: negation. it doesn't take anything away at all, if anything it actually adds information. Negating a four doesn't take it away, it just specifies a different (inverted) mapping of a given value with respect to zero. And this is still a very naive take, as someone with no background in number theory. But it's much more useful than say, Billy has four apples, he gives three to Mary, etc.
Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by Qem on Tuesday May 09, 2023 @08:32PM (#35882152)
We could make the minus sign go away if we adopted a balanced base (e.g. balanced ternary), that has both negative and positive digits. That way, just looking at the digits would make evident if a number is positive or negative, without any need for a minus sign.
Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by RhysU on Wednesday May 10, 2023 @07:43AM (#35886044)
I love that such systems were built once: https://www.wikipedia.org/wiki/Setun
Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by karpierz on Tuesday May 09, 2023 @07:32PM (#35881729)
How do you define numbers in a meaningful way without involving operators?

Without operators, all you have is a set of objects which you can't tell apart.

Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by regularfry on Wednesday May 10, 2023 @07:39AM (#35886016)
Oh, you have operators. They're just imprecise.
Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by addaon on Tuesday May 09, 2023 @08:09PM (#35882007)
> I've always held that the numbers themselves are perfectly precise. It's the operations that don't do what you expect.

I think that this is an oversimplification, and misses a key point.

Floating point numbers can be interpreted in (at least) two distinct ways.

In one use model, floating point numbers allow a sparse representation of selected points on the (extended) real number line. In this model, the numbers themselves are perfectly precise, and the basic operations do exactly what you'd expect -- but because the result of the expected real-number operation may not be one of the selected representable points, each operation is potentially paired with a second, implicit, rounding operation, and the consequences of the cumulative effects of this rounding operation take some thought.

This first model is often comfortable for those who have done low-level numerical program with sized integers, which are likewise a selected sparse representation of selected points on the integer line. While rounding comes up in fewer operations here, and is simpler, wrap-around becomes an issue.

However, a second model of floating point numbers is that each possible bit sequence represents an /interval/ on the (extended) real number line. In versions of this model, while the set of representable intervals is still very restricted, every point on the real number line falls into a representable interval, which is a property that sometimes eases (and sometimes seems to ease) analysis. In exchange, the operations are much more complex, and their limitations are much more obvious -- in particular, not only can no disjoint intervals be represented, but for any given point on the real line only one interval that contains it is representable -- there's no way to represent intervals of different sizes containing (some of) the same points.

This second model is what people are mostly thinking of when they say that floating point arithmetic is "not precise." In particular, both the inputs and outputs of operations have imprecise interpretations -- that is, the input interval contains infinitely many indistinguishable points, and the output interval contains infinitely many indistinguishable points, even if a real number equivalent of the problem would have a single number in the desired range.

When extended to true interval arithmetic, the second model can be very useful for understanding error propagation and uncertainty, but sadly this is rarely done.

Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by adgjlsfhk1 on Wednesday May 10, 2023 @01:12AM (#35883868)
the problem is the second interpretation is wrong. for example, exp(x) would give a higher value than it does since the average value of the exponent of a range is higher than the exponent of it's center.
Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by regularfry on Wednesday May 10, 2023 @07:38AM (#35886009)
Yep. For a sequence of operations to work on a range you do actually need to track both ends of the range, not just one of them. Otherwise what you end up implicitly doing is flipping lossily between a range interpretation and a point interpretation and back again.
Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by kragen on Wednesday May 10, 2023 @01:30AM (#35883974)
the second model is wishful thinking; the first model describes how ieee-784 arithmetic actually works

as you point out, the second model can be implemented

Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by sterlind on Tuesday May 09, 2023 @04:52PM (#35880223)
I'm assuming you mean equality comparison, rather than comparison in general!

but is there a better algebraic structure to use when thinking of floats? like, in terms of limits, or something that tracks significant digits and uncertainty? how do formal methods handle these?

Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by Y_Y on Tuesday May 09, 2023 @05:29PM (#35880626)
There's more than one way to skin that cat, but interval arithmetic is a good and simple model. You can take the part of the real line which maps to the single float you are thinking about, and then look at the image of that set under the whatever functions you are thinking about.
Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by toast0 on Tuesday May 09, 2023 @04:55PM (#35880259)
Since they're not changing ==, I wouldn't think =< and >= would change. Note, Erlang has a no arrow looking comparison rule, so <= is not a comparison operator; actually it's used in bitstring comprehensions, the bitstring version of list comprehensions which use <- [1]

[1] https://www.erlang.org/doc/reference_manual/expressions.html...

Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by abhgh on Tuesday May 09, 2023 @05:54PM (#35880867)
As a followup to this comment, in Python you have the option of using `isclose()` which is present in both `numpy`[1] and the standard `math` [2] libraries. This has been quite helpful for me in comparing small probability values.

[1] https://numpy.org/doc/stable/reference/generated/numpy.isclo...

[2] https://docs.python.org/3/library/math.html#math.isclose

Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by spacechild1 on Tuesday May 09, 2023 @07:06PM (#35881510)
In general, this is good advice.

However, there are cases where comparing floating point numbers for equality works just fine. For example, if you have a variable 'v' and want to update it to a new value 'f', you can do 'if (v == f)' to check if the variable would change. Or if you have a sentinel value -1.0, it is perfectly ok to do 'if (a == -1.0)'

In general, if you look for a number 'f' in variable 'v', you can safely use equality as along as you expect 'v' to be set to 'f', i.e. the value of 'v' is not the result of a computation. This may sound trivial, but this is often omitted when discussing floating point comparison. If someone knows a scenario where the examples above would break, I would be curious to hear! (My main field is audio programming and code like this is omnipresent.)

Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by munk-a on Tuesday May 09, 2023 @07:29PM (#35881712)
I know there is a fair bit of division on this point but I've always liked type mixing, in languages where this is available, to handle signal values and preferred separate variables where it is not. I'd prefer to `if (v IS NULL)` a nullable float compared to checking a constant value - or else to use a guard value to test the validity i.e. `if x = FREQUENCY_SET // enter logic involving the frequency stored in v` - that approach is even better if you can use tuples/structs to glue the status and value fields together.

I am a quickly crustifying developer who was an enthusiatic value packer in their younger years but I've changed my tune to be less afraid of using more variables if they are clearer - even if that comes at a cost to data efficiency (and like, an extremely negligible cost in most situations). Though I don't have any objections to value packing for serialization - that's a fundamentally different domain.

Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by stouset on Tuesday May 09, 2023 @07:24PM (#35881666)
> For example, if you have a variable 'v' and want to update it to a new value 'f', you can do 'if (v == f)' to check if the variable would change.

Isn't it strictly faster to simply assign? No branching, no branch predictions, only one instruction in all cases.

Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by spacechild1 on Tuesday May 09, 2023 @07:37PM (#35881756)
A typically pattern is:

  float freq = getParameter(FILTER_FREQUENCY);
  float coeff;
  if (freq != mFreq) {
    coeff = calculateCoeffFromFrequency(freq); // expensive
    mFreq = freq;
    mCoeff = coeff;
  } else {
    coeff = mCoeff; // use cached value
  }
  // use coeff
Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by bee_rider on Tuesday May 09, 2023 @10:06PM (#35882787)
Could you do some threshold-based comparison? What if freq is changed only a little tiny bit?
Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by spacechild1 on Tuesday May 09, 2023 @11:38PM (#35883357)
I can certainly imagine cases where this would make sense.
Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by karpierz on Tuesday May 09, 2023 @07:28PM (#35881697)
Assuming that all you want to do is assign, then sure. If you want to log the change or do any additional conditional logic, then that wouldn't work.
Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by kstenerud on Tuesday May 09, 2023 @07:30PM (#35881717)
It depends on whether you need a set operation or a test-and-set operation.
Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by kragen on Wednesday May 10, 2023 @01:26AM (#35883963)
cesaref's comment is not good advice, it's not advice at all, it's a snarky take on how floating point is trickier than it looks

almost invariably floating-point calculations end up in some kind of comparison

even equality comparison of floats is useful; there are an abundance of seminumerical algorithms which use it correctly on the results of computations. a trivial example is computing an attractive fixpoint by iterating a function and comparing each hare result to the previous iteration and to a tortoise. you have to be especially careful about x87 extended precision here

ieee-784 requires bit-exact results for the five fundamental operations

here is an example where your 'safe' generalization is not safe:

    #include <stdio.h>

    int main()
    {
      float f = 0.7;
      printf(f == 0.7 ? "ok\n" : "wtf\n");
      return 0;
    }
this prints 'wtf'
Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by shultays on Wednesday May 10, 2023 @02:49AM (#35884422)
well you shouldn't expect much if you are comparing floats to doubles
Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by spacechild1 on Wednesday May 10, 2023 @04:35AM (#35885097)
True, but it's an easy mistake to make.
Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by tialaramex on Wednesday May 10, 2023 @08:13AM (#35886223)
It's an easy mistake to make in C (and C++) because of their lackadaisical approach to type safety.

In Rust if we insist we want to compare an f32 (what C would call float) to an f64 (double), the language says it doesn't have a way to do that, highlighting our mistake. If we just try to compare an f32 with value 0.7 against 0.7, the type inference concludes 0.7 is also an f32 so it's equal, and if we do the same with an f64, this time by inference 0.7 is an f64 and so again it's equal.

Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by spacechild1 on Thursday May 11, 2023 @06:00AM (#35900017)
Rust's behavior makes so much more sense! IMO, implicit conversions is one of C++ biggest weaknesses. Yes, it may save some keystrokes, but it can easily lead to subtle (or not so subtle) bugs.
Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by kragen on Thursday May 11, 2023 @11:03AM (#35903205)
agreed
Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by spacechild1 on Thursday May 11, 2023 @05:42AM (#35899885)
> almost invariably floating-point calculations end up in some kind of comparison

I'm pretty sure OP meant comparison for equality, not comparison in general (which would be absurd, indeed).

> even equality comparison of floats is useful

That's exactly what I tried to point out. However, these are really special cases and you need to be careful and know what you are doing.

Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by kragen on Thursday May 11, 2023 @10:49AM (#35902982)
i think he was snarkily making an absurd comment for the humor value
Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to... by kragen on Wednesday May 10, 2023 @01:11AM (#35883866)
this is not about arithmetic comparison, it's about erlang's exact-equality operator