@@ -34,7 +34,7 @@ fn cosh32(z: Complex(f32)) Complex(f32) {
34
34
35
35
if (ix < 0x7f800000 and iy < 0x7f800000 ) {
36
36
if (iy == 0 ) {
37
- return Complex (f32 ).init (math .cosh (x ), y );
37
+ return Complex (f32 ).init (math .cosh (x ), x * y );
38
38
}
39
39
// small x: normal case
40
40
if (ix < 0x41100000 ) {
@@ -45,7 +45,7 @@ fn cosh32(z: Complex(f32)) Complex(f32) {
45
45
if (ix < 0x42b17218 ) {
46
46
// x < 88.7: exp(|x|) won't overflow
47
47
const h = @exp (@abs (x )) * 0.5 ;
48
- return Complex (f32 ).init (math . copysign ( h , x ) * @cos (y ), h * @sin (y ));
48
+ return Complex (f32 ).init (h * @cos (y ), math . copysign ( h , x ) * @sin (y ));
49
49
}
50
50
// x < 192.7: scale to avoid overflow
51
51
else if (ix < 0x4340b1e7 ) {
@@ -68,7 +68,7 @@ fn cosh32(z: Complex(f32)) Complex(f32) {
68
68
if (hx & 0x7fffff == 0 ) {
69
69
return Complex (f32 ).init (x * x , math .copysign (@as (f32 , 0.0 ), x ) * y );
70
70
}
71
- return Complex (f32 ).init (x , math .copysign (@as (f32 , 0.0 ), (x + x ) * y ));
71
+ return Complex (f32 ).init (x * x , math .copysign (@as (f32 , 0.0 ), (x + x ) * y ));
72
72
}
73
73
74
74
if (ix < 0x7f800000 and iy >= 0x7f800000 ) {
@@ -123,7 +123,7 @@ fn cosh64(z: Complex(f64)) Complex(f64) {
123
123
}
124
124
// x >= 1455: result always overflows
125
125
else {
126
- const h = 0x1p1023 ;
126
+ const h = 0x1p1023 * x ;
127
127
return Complex (f64 ).init (h * h * @cos (y ), h * @sin (y ));
128
128
}
129
129
}
@@ -153,20 +153,29 @@ fn cosh64(z: Complex(f64)) Complex(f64) {
153
153
return Complex (f64 ).init ((x * x ) * (y - y ), (x + x ) * (y - y ));
154
154
}
155
155
156
- const epsilon = 0.0001 ;
157
-
158
156
test cosh32 {
157
+ const epsilon = math .floatEps (f32 );
159
158
const a = Complex (f32 ).init (5 , 3 );
160
159
const c = cosh (a );
161
160
162
- try testing .expect ( math . approxEqAbs ( f32 , c .re , -73.467300 , epsilon ) );
163
- try testing .expect ( math . approxEqAbs ( f32 , c .im , 10.471557 , epsilon ) );
161
+ try testing .expectApproxEqAbs ( -73.467300 , c .re , epsilon );
162
+ try testing .expectApproxEqAbs ( 10.471557 , c .im , epsilon );
164
163
}
165
164
166
165
test cosh64 {
166
+ const epsilon = math .floatEps (f64 );
167
167
const a = Complex (f64 ).init (5 , 3 );
168
168
const c = cosh (a );
169
169
170
- try testing .expect (math .approxEqAbs (f64 , c .re , -73.467300 , epsilon ));
171
- try testing .expect (math .approxEqAbs (f64 , c .im , 10.471557 , epsilon ));
170
+ try testing .expectApproxEqAbs (-73.46729221264526 , c .re , epsilon );
171
+ try testing .expectApproxEqAbs (10.471557674805572 , c .im , epsilon );
172
+ }
173
+
174
+ test "cosh64 musl" {
175
+ const epsilon = math .floatEps (f64 );
176
+ const a = Complex (f64 ).init (7.44648873421389e17 , 1.6008058402057622e19 );
177
+ const c = cosh (a );
178
+
179
+ try testing .expectApproxEqAbs (std .math .inf (f64 ), c .re , epsilon );
180
+ try testing .expectApproxEqAbs (std .math .inf (f64 ), c .im , epsilon );
172
181
}
0 commit comments