Debian Codegraph @

mingw-w64-crt/math/x86/asinh.c

raw
1 /**
2 * This file has no copyright assigned and is placed in the Public Domain.
3 * This file is part of the mingw-w64 runtime package.
4 * No warranty is given; refer to the file DISCLAIMER.PD within this package.
5 */
6 #include <math.h>
7 #include <errno.h>
8 #include <float.h>
9 #include "fastmath.h"
10
11 /* asinh(x) = copysign(log(fabs(x) + sqrt(x * x + 1.0)), x) */
12 double asinh(double x)
13 {
14 double z;
15 if (!isfinite (x))
16 return x;
17 z = fabs (x);
18
19 /* Avoid setting FPU underflow exception flag in x * x. */
20 #if 0
21 if ( z < 0x1p-32)
22 return x;
23 #endif
24
25 /* NB the previous formula
26 z = __fast_log1p (z + z * z / (__fast_sqrt (z * z + 1.0) + 1.0));
27 was defective in two ways:
28 1: It ommitted required brackets:
29 z = __fast_log1p (z + z * (z / (__fast_sqrt (z * z + 1.0) + 1.0)));
30 ^ ^
31 so would still overflow for large z.
32 2: Even with the brackets, it still degraded quickly for large z
33 (where z*z+1 == z*z).
34 e.g. asinh (sinh 356.0)) gave 355.30685281944005
35 */
36
37 const double asinhCutover = pow(2,DBL_MAX_EXP/2); // 1.3407807929943e+154
38
39 if (z < asinhCutover)
40 /* After excluding large values, the rearranged formula gives better results
41 the original formula log(z + sqrt(z * z + 1.0)) for very small z.
42 e.g. rearranged asinh(sinh 2e-301)) = 2e-301
43 original asinh(sinh 2e-301)) = 0.
44 asinh(z) = log (z + sqrt (z * z + 1.0))
45 = log1p (z + sqrt (z * z + 1.0) - 1.0)
46 = log1p (z + (sqrt (z * z + 1.0) - 1.0)
47 * (sqrt (z * z + 1.0) + 1.0)
48 / (sqrt (z * z + 1.0) + 1.0))
49 = log1p (z + ((z * z + 1.0) - 1.0)
50 / (sqrt (z * z + 1.0) + 1.0))
51 = log1p (z + z * z / (sqrt (z * z + 1.0) + 1.0))
52 */
53 z = __fast_log1p (z + z * (z / (__fast_sqrt (z * z + 1.0) + 1.0)));
54 else
55 /* above this, z*z+1 == z*z, so we can simplify
56 (and avoid z*z being infinity).
57 asinh(z) = log (z + sqrt (z * z + 1.0))
58 = log (z + sqrt (z * z ))
59 = log (2 * z)
60 = log 2 + log z
61 Choosing asinhCutover is a little tricky.
62 We'd like something that's based on the nature of
63 the numeric type (DBL_MAX_EXP, etc).
64 If c = asinhCutover, then we need:
65 (1) c*c == c*c + 1
66 (2) log (2*c) = log 2 + log c.
67 For float:
68 9.490626562425156e7 is the smallest value that
69 achieves (1), but it fails (2). (It only just fails,
70 but enough to make the function erroneously non-monotonic).
71 */
72 z = __fast_log(2) + __fast_log(z);
73 return copysign(z, x); //ensure 0.0 -> 0.0 and -0.0 -> -0.0.
74 }