Skip to content

keep the sign of cos(y) in sinh near the overflow boundary - #361

Open
HuzaifaChaudary wants to merge 1 commit into
apple:mainfrom
HuzaifaChaudary:fix/sinh-overflow-cos-sign
Open

HuzaifaChaudary wants to merge 1 commit into
apple:mainfrom
HuzaifaChaudary:fix/sinh-overflow-cos-sign

Conversation

@HuzaifaChaudary

Copy link
Copy Markdown

#352 moved the sign of x into the phase on the large |x| path of sinh , but it does it with RealType(signOf: z.x, magnitudeOf: RealType.cos(z.y)) , which also throws away the sign of cos(y) . so whenever cos(y) is negative the real part has the wrong sign , and sin , which goes through sinh , gets the wrong imaginary part for a large imaginary input :

Complex<Double>.sinh(Complex(40, 3 * .pi/4))
// main  (8.32212591859309e+16, 8.322125918593091e+16)
// fixed (-8.32212591859309e+16, 8.322125918593091e+16)

Complex<Double>.sin(Complex(.pi, 40))
// main  (14.413190679920108, 1.1769263341851e+17)
// fixed (14.413190679920108, -1.1769263341851e+17)

the fixed values match mpmath . the fix multiplies cos(y) by the sign of x , the same way cosh already does with sin(y) .

the tests from #352 only use y = ±π/4 , where cos(y) is positive . this adds the same near overflow checks at y = ±3π/4 for sinh , and two for cosh so both functions cover that quadrant . the four new sinh real part checks fail on main for Float and Double , and the whole suite passes with the change .

i found it by comparing the complex elementary functions against mpmath on random inputs over a wide range of magnitudes . after this change everything else was within 5 ulp normwise , apart from a few tiny atan / atanh inputs where mpmath itself ran out of precision .

the large |x| path of sinh built its real part as
RealType(signOf: z.x, magnitudeOf: cos(y)) , which drops the sign of cos(y) .
so sinh(x + iy) had the wrong real part whenever cos(y) < 0 , and sin(z) ,
which goes through sinh , had the wrong imaginary part for large |im z| .
it now multiplies cos(y) by the sign of x , like cosh does with sin(y) .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant