The Math.Sign function with x = -0.

Merged to the master branch

The Math.Sign function with x = -0.

Postby Robert » Tue Jun 19, 2018 7:41 pm

Sign returns +0 when x = -0.
I think this should be considered an error.

There does not seem to be a Sign function defined in the IEEE 754-2008 standard, the only relevant operation I can find is the "isSignMinus" function (section 5.7.2, page 25). This does distinguish between +0 & -0.

I am currently working on producing versions of the commented, but suggested functions, in module Math that work correctly for +/-INF & + / -0 since several of the current suggestions do not.
Since several of these suggestions use the Sign function, we need to decide on any change before I can continue.

This correction could be implemented by replacing the first line below by the second.
Code: Select all
      | 0, 2: FSTPst0; RETURN 0.0
      | 0, 2: FSTPst0; RETURN x


(This suggestion agrees with, for example, Javascript: https://developer.mozilla.org/en-US/doc ... /Math/sign)
User avatar
Robert
 
Posts: 1023
Joined: Sat Sep 28, 2013 11:04 am
Location: Edinburgh, Scotland

Re: The Math.Sign function with x = -0.

Postby Josef Templ » Thu Jun 21, 2018 4:28 pm

In Java it is also the same as you suggested:

public static double signum(double d)
Returns the signum function of the argument; zero if the argument is zero, 1.0 if the argument is greater than zero, -1.0 if the argument is less than zero.
Special Cases:

If the argument is NaN, then the result is NaN.
If the argument is positive zero or negative zero, then the result is the same as the argument.

- Josef
User avatar
Josef Templ
 
Posts: 2038
Joined: Tue Sep 17, 2013 6:50 am

Re: The Math.Sign function with x = -0.

Postby Josef Templ » Fri Jun 22, 2018 5:58 am

Is there anything else required for supporting negative zero?
For example: conversion to/from string or parsing a constant like -0.0 in the compiler.

- Josef
User avatar
Josef Templ
 
Posts: 2038
Joined: Tue Sep 17, 2013 6:50 am

Re: The Math.Sign function with x = -0.

Postby Robert » Fri Jun 22, 2018 7:47 am

Josef Templ wrote:Is there anything else required for supporting negative zero?
For example: conversion to/from string or parsing a constant like -0.0 in the compiler.

An interesting question.
1 - Around about BlackBox 1.2 the compiler did have problems with the constant -0.0. I drew this to Oms's attention, and it was fixed. I am unaware of any other compiler issues.

2 - There are (many!) compiler issues with NaNs, but that is a different, and harder, issue.

3 - Conversion to string. My formatter (LibFmtrs) has the option to display the sign of zero (it has several other REAL number formatting options). It does this by post-processing the string returned from the standard Strings library module. So I have no need to consider changes here.

4 - Conversion from string. Don't think any changes are required. Both the compiler and the Strings module can read -0.0 correctly.
(I have a private hack in Strings that allows the unicode minus (−) sign to be used as well as the ASCII hyphen sign (-). In many fonts the unicode minus looks much better. This does not affect the compiler, which still rejects unicode minus − when it expects -.)

5 - I will have more to say about module Math and -0.0. In the table below the value of ArcSinh(-0.) looks suspicious. Can anyone test this value in other respectable libraries, such as C# or Java, and let me know what they do? (An easy way to test the sign of zero is to print 1 / x; 0.0 -> INF, -0.0 -> -INF).

Code: Select all
System.Math: ArcSinh & ArcTan ...
        x                    ArcSinh (x)         ArcTan (x)
         −∞                  −∞                 −1.570796327
      −500.                 −6.907756279        −1.568796329
       −50.                 −4.605270171        −1.550798993
        −5.                 −2.312438341        −1.373400767
        −1.                 −0.881373587        −0.785398163
        −0.5                −0.481211825        −0.463647609
        −0.                  0.                 −0.         
         0.                  0.                  0.         
         0.5                 0.481211825         0.463647609
         1.                  0.881373587         0.785398163
         5.                  2.312438341         1.373400767
        50.                  4.605270171         1.550798993
       500.                  6.907756279         1.568796329
          ∞                   ∞                  1.570796327
User avatar
Robert
 
Posts: 1023
Joined: Sat Sep 28, 2013 11:04 am
Location: Edinburgh, Scotland

Re: The Math.Sign function with x = -0.

Postby Josef Templ » Fri Jun 22, 2018 5:36 pm

My experiments show that negative zero is converted to positive zero with Strings.StringToReal.
Is this correct? I would expect -0.0.

- Josef
User avatar
Josef Templ
 
Posts: 2038
Joined: Tue Sep 17, 2013 6:50 am

Re: The Math.Sign function with x = -0.

Postby Robert » Fri Jun 22, 2018 5:40 pm

Josef Templ wrote:My experiments show that negative zero is converted to positive zero with Strings.StringToReal.
Is this correct? I would expect -0.0.

Don't think so ...
Code: Select all
PROCEDURE  TestStrings*;
  VAR
    str  :  ARRAY  80  OF  CHAR;
    x    :  REAL;
    res  :  INTEGER;
  BEGIN
    f.SetToEnd;
    f.SetOpts ({Fmtrs.showNegZero});

    Strings.StringToReal ('-0.0', x, res);
    f.StrRealLn ('-0.0     :', x, 12, 8)
  END  TestStrings;

Code: Select all
-0.0     : −0.
User avatar
Robert
 
Posts: 1023
Joined: Sat Sep 28, 2013 11:04 am
Location: Edinburgh, Scotland

Re: The Math.Sign function with x = -0.

Postby Josef Templ » Fri Jun 22, 2018 5:43 pm

Sorry, this was a typo. I meant RealToString.

- Josef
User avatar
Josef Templ
 
Posts: 2038
Joined: Tue Sep 17, 2013 6:50 am

Re: The Math.Sign function with x = -0.

Postby Robert » Fri Jun 22, 2018 5:45 pm

Josef Templ wrote:Sorry, this was a typo. I meant RealToString.

Not checked, but I'm 100 % sure that you are correct.
User avatar
Robert
 
Posts: 1023
Joined: Sat Sep 28, 2013 11:04 am
Location: Edinburgh, Scotland

Re: The Math.Sign function with x = -0.

Postby Robert » Fri Jun 22, 2018 5:47 pm

I think the current behaviour is the correct default behaviour. Printing out -0.0 frequently will confuse most people most of the time. The ability to see negative zeros is useful, but only on (very) rare occasions. My Fmtr defaults to hiding the sign of zero.
User avatar
Robert
 
Posts: 1023
Joined: Sat Sep 28, 2013 11:04 am
Location: Edinburgh, Scotland

Re: The Math.Sign function with x = -0.

Postby Josef Templ » Fri Jun 22, 2018 6:27 pm

Robert wrote:I think the current behaviour is the correct default behaviour. Printing out -0.0 frequently will confuse most people most of the time. The ability to see negative zeros is useful, but only on (very) rare occasions. My Fmtr defaults to hiding the sign of zero.


For me this is the same kind of bug as in Math.Sign.
Most people will never notice the difference because they don't use negative zeros.
If they use it, they will expect to see what they get.

- Josef
User avatar
Josef Templ
 
Posts: 2038
Joined: Tue Sep 17, 2013 6:50 am

Next

Return to Resolved (Features)

Who is online

Users browsing this forum: No registered users and 1 guest

cron