• Subject: Re: Why do some math functions return -0
• From: David Kastrup <dak@...>
• Date: Mon, 20 Sep 2010 09:29:38 +0200

```HyperHacker <hyperhacker@gmail.com> writes:

> On Sun, Sep 19, 2010 at 20:11, KHMan <keinhong@gmail.com> wrote:
>> On 9/20/2010 4:45 AM, HyperHacker wrote:
>>>
>>> [snipped all]
>>> Note that a number displayed as -0.0000 could also be a  very small
>>> negative number rounded to fit the display, e.g.:
>>>>
>>>> =('%f'):format(-0.00000001)
>>>
>>> -0.000000
>>> %f rounds to six places by default. This is the proper meaning of
>>> negative zero - a negative value that rounded to zero.
>>
>> The last sentence is wrong. No book on computer arithmetic discusses a
>> negative zero in such a fashion. Do not mix negative zeros with rounding
>> behaviour. It is misleading with respect to the standard use of the
>> terminology.
>>
>>> It'd probably save you a lot of headache in the future to read up on
>>> how floating-point works, as it has a lot of potential pitfalls.
>>
>> Ironic.
>
> Who's Mike?
>
> I apologize for not taking the time to find a machine whose FPU
> behaviour differs from mine, and for somehow writing an explanation
> that manages to include too little and too much detail at the same
> time. I can see how it must be difficult to scroll past a message you

Which is probably why you didn't.

> The point of my explanation was to explain why -0 is sometimes
> generated

And you did not realize that in all the presented code, there was _no_
way that would _ever_ generate -0 unless there was a processing error
involved.

The specified code worked _exclusively_ with operations guaranteed to be
integers presentable in IEEE format.  If -0 (or something printed as -0
for whatever reason) was generated by the code, the code was buggy.

IEEE pretty clearly defines what kind of operations can lead to -0.

That _you_ have no clue about floating point does not mean that your
general fuzzy feeling about floating point operations being able to
invent things all on its own without rhyme and reason is grounded in
reality.

Anyway, I seem to remember that some printing problem producing -0 for
no apparent reason had been fixed in some Lua version in the past.
Maybe it is an older version for which this result appears.

> and that it's not usually meaningful in computer applications and safe
> to ignore, rather than wasting time performing a deep investigation
> into why exactly one particular person's system generates it in one
> particular situation.
>
> I also took the time to point out that I can't confirm that Lua and/or
> this user's system does in fact handle a -0 correctly since my own
> system doesn't want to generate one, and that there *are* meanings for
> -0 in some situations, such as when displaying a rounded value.

There are no rounded values in the given example.  If you bothered to
actually read the code, you would have noticed.

> (See http://en.wikipedia.org/wiki/Negative_Zero#Scientific_uses - this
> page also offers more explanation as to why -0 exists.) While it's
> true that not all systems *will* display -0 for (or only for) a
> rounded negative value, this *is* the intended meaning.

Again: there _are_ no rounded negative values involved.  Nor any
negative values at all.

Somebody calling himself "HyperHacker" should be able to read 10 lines
of simple code without his eyes glazing over.

--
David Kastrup

```