Menu

#515 Incorrect sign output from asinh

v1.0 (example)
open
nobody
None
5
2021-10-13
2015-11-25
stinos
No

Consider the following:

#include <stdio.h>
#include <string.h>
#include <math.h>

void printdbl( double x )
{
  long long unsigned hexValue;
  memcpy( &hexValue, &x, sizeof( hexValue ) );
  printf( "x = %.5g ; hex = %016llx \n", x, hexValue );
}

void showasinh( double x )
{
  printdbl( x );
  printdbl( asinh( x ) );
}

int main()
{
  showasinh( 0.0 );
  return 0;
}

Output:

x = 0 ; hex = 0000000000000000
x = -0 ; hex = 8000000000000000

Expected output:

x = 0 ; hex = 0000000000000000
x = 0 ; hex = 0000000000000000

As far as I can tell this is simply becasue the implementation of asinh mimics copysign using

return ( x > 0.0 ? z : -z);

so I think the fix would be to just use

return ( x >= 0.0 ? z : -z);

Discussion

  • WinPORTS

    WinPORTS - 2016-05-26

    You could try the latest GCC (i.e 6.1.0)

    x = 0 ; hex = 0000000000000000
    x = 0 ; hex = 0000000000000000

     
  • David James

    David James - 2021-09-07

    I don't think return ( x >= 0.0 ? z : -z); would be correct, since it would then give 0.0 for asinh(-0.0) (it should give -0.0, which it currently does). (I think atanh is coded per the suggestion, and gives the incorrect 0.0 for atanh(-0.0)).

    Isn't it possible to just use copysign?

     
  • stinos

    stinos - 2021-09-07

    Fair point, should indeed likely just use copysign.

     

Log in to post a comment.