Live data from Hacker News

How does your programming language handle “minus zero” (-0.0)?

lemire.me

161–170 of 219 posts

Re: How does your programming language handle “minus zero” (-0.0)?

#161
post #140

Earlier quoted context omitted.

Yes, in a way. What distinguishes irrational numbers from rational numbers is that all rational numbers can be represented by strings drawn from a regular language. For example, all strings generated by the regular language "-?\d+\.\d+?_\d+" (where "_" denotes the repeating decimal expansion as in 1/6 = 0.1_6) correspond to exactly one rational number and all rational numbers correspond to at least one string in this…

I'm not entirely sure I follow. Aren't pi and e irrational numbers? I also included .9_ as it is an easy trap to show we have two ways of writing 1 in standard decimal notation. Please read this whole post as a question. I'm genuinely not clear on the distinction.

Correct. Given the regular language I specified, each rational has an infinite number of matching strings: 1.0 = 1.00 = 1.000 = 0.9_99 = 1.0_0 etc. The point is that for every rational number you can think of, I can show you at least one string in my regular language to represent that number.

According to the finitists, this is a defining feature of a "number". Since the same can't be done for irrational numbers finitists conclude that irrational "numbers" aren't numbers. You probably agree that all numbers are (or can be represented by) symbols, but that not all symbols are numbers. So how do we distinguish symbols from numbers?

Re: How does your programming language handle “minus zero” (-0.0)?

#162
post #64

Earlier quoted context omitted.

In 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.

It might be a little faster for addition and subtraction in synthetic benchmarks -- and with the use of pipelining might end up being slower overall -- but multiplication and division are considerably faster with floating point.

division, yes, but multiplication is faster, but it's not huge. The limiting factor for multiplication is that it's O(N^2) in multiplied digits; so a 32-bit fixed point has 32x32 and IEEE fp32 multiplication has 23x23.

Re: How does your programming language handle “minus zero” (-0.0)?

#163

Earlier quoted context omitted.

Computers (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.

More information isn't better if that information isn't useful. Does x*0 evaluate to 0.0? Not with -0 around. Does x+0 evaluate to x? Maybe!

IEEE 754 requires that -0.0 and 0.0 compare equal.

> Does x*0 evaluate to 0.0

No, but it will compare equal, unless x is either infinite or a NaN.

> Does x+0 evaluate to x? Maybe!

Yes, unless x is either infinite or a NaN.

Re: How does your programming language handle “minus zero” (-0.0)?

#164
the 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.

Re: How does your programming language handle “minus zero” (-0.0)?

#165

the 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.

Is there any language that treats the zeros as unequal? That would seem like the real "screw IEEE".

Re: How does your programming language handle “minus zero” (-0.0)?

#166

Earlier quoted context omitted.

> When they do it's often a mistake, like for currency. The most beautiful FP bug I remember was a denial of service in some webservers where by setting the header "Accepted Language: en-gb;q=0.3 en;q=0.8 ..." to a specific value you could send the official Java floating-point parser in an infinite loop (and this affected several Java webservers). So at each webpage request, you were sending one CPU core of the webse…

There is nothing wrong with the protocol. The protocol has nothing to do with what representation is used by software that speaks the protocol. Software could just as well parse those values as integers. Just drop the decimal point and pad with zeroes. Or use a decimal type (but there is really no need to).

The protocol has an obvious implementation choice and some non-obvious alternatives.

If the protocol had nothing to do with it, I don't think you'd see the same bug come up in as many implementations

Re: How does your programming language handle “minus zero” (-0.0)?

#168
post #166

Earlier quoted context omitted.

There is nothing wrong with the protocol. The protocol has nothing to do with what representation is used by software that speaks the protocol. Software could just as well parse those values as integers. Just drop the decimal point and pad with zeroes. Or use a decimal type (but there is really no need to).

The protocol has an obvious implementation choice and some non-obvious alternatives. If the protocol had nothing to do with it, I don't think you'd see the same bug come up in as many implementations

[deleted]

Re: How does your programming language handle “minus zero” (-0.0)?

#169

I 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 learn…

Floats are essential for machine learning, scientific computing, and any other application where the goal is to model mathematical processes involving real numbers. The idea that there is something out there that could do the job better than floats is almost certainly wrong. I'm fascinated by how common it is for programmers to hate floats. Yes, if you write business software for a living you may not have much use fo…

For some reason many programmers love to hate technologies that are old.

Hate floats, love big decimals

Hate OOP, love functional

Hate RDBMS, love nosql

Hate html web pages, love SPA

In my opinion it's a confluence of influences.

1) the same social media attitudes poisoning society generally. "I've got an opinion, and it's worth just as much as yours". News flash - opinions are like arseholes, everyone has one.

2) Inexperience - devs who have only ever built chat apps and don't understand why relationships hence RDBMS are common and useful in line of business applications. Devs who have not tried to capture the water level in a tank or the voltage from a solar panel and don't get why floats are useful.

3) resume padding. nuff said

Re: How does your programming language handle “minus zero” (-0.0)?

#170
post #4

Earlier quoted context omitted.

Amusingly enough the Apple Watch reports temperature as 0° and sometimes as -0°, I've not determined what causes that, perhaps rounding around 0?

Probably not even rounding, but just printf. The following works on my zsh: $ printf '%.0f°\n' 0.3 0° $ printf '%.0f°\n' -0.3 -0°

That actually is rounding around zero (to zero decimal places, but it is rounding).
Post reply on HN