Search Issue Tracker

Postponed means that the issue was either a feature request or something that requires major refactoring on our side. Since that makes the issue not actionable in the close future we choose to close it as Postponed and add it on our internal roadmaps and technical debt pages instead.

Postponed

Votes

1

Found in

5.3.2f1

Issue ID

767816

Regression

No

"1/Mathf.Abs(-0f)" returns -Infinity instead of +Infinity

Scripting

-

Steps to reproduce:
1. Open attached project and scene named "main".
2. Press play button.
3. Notice the output of "Console" window:
"1f / Mathf.Abs(0f) = Infinity"
"Mathf.Abs(-0f) = 0"
"1f / Mathf.Abs(-0f) = -Infinity"

Expected result:
Using "1f / Mathf.Abs(-0f)" should return "+Infinity" (the same value as using "1f / Mathf.Abs(0f)").

Reproduced with:
5.1.0f3, 5.2.4f1, 5.3.4f1, 5.3.4p4, 5.4.0b17.

Comments (1)

  1. T.E.M.

    May 25, 2016 20:11

    -Infinity is a (potentially harmful) side effect rather then the actual bug. Your second test, "Mathf.Abs(-0f) = 0" can't test the output because the correct IEEE 754 behavoir is for -0 and 0 to be treated as equal by the comparison operator.

    However with bit testing you can determine if a float is 0 or -0 which exposes that Mathf.Abs(-0) is actually returning -0 when it should be returning 0. This may be a Mono framework bug. You can update your project with this struct so it can directly test whether the sign bit is set (negative) or not set (positive).

    // using System.Runtime.InteropServices
    [StructLayout(LayoutKind.Explicit)]
    public struct FloatBits
    {
    const uint NegativeBit = 1u << 31;

    [FieldOffset(0)]
    public float Value;

    // Shares the same address in memory as Value, allowing its bit to be read and modified
    [FieldOffset(0)]
    uint Bits;

    // Set to true to make a value negative, false to make it positive.
    public void SetNegativeBit(bool value)
    {
    if (value)
    {
    Bits |= NegativeBit;
    }
    else
    {
    Bits &= ~NegativeBit;
    }
    }

    public bool IsNegative()
    {
    return (Bits & NegativeBit) != 0;
    }
    }

Add comment

Log in to post comment

All about bugs

View bugs we have successfully reproduced, and vote for the bugs you want to see fixed most urgently.