Skip to content

when x is NaN in trig functions return x rather than NaN #49285

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 11, 2023

Conversation

oscardssmith
Copy link
Member

see #48523 for reasoning.

@oscardssmith oscardssmith changed the title when x is NaN in trig functions return x rather than NaN when x is NaN in trig functions return x rather than NaN Apr 7, 2023
@oscardssmith oscardssmith added the maths Mathematical functions label Apr 7, 2023
@topolarity
Copy link
Member

topolarity commented Apr 7, 2023

I think this guarantee is destroyed by the LLVM optimizer anyway

From page 5 of https://arxiv.org/pdf/1603.09290.pdf:

The standard corresponds to IEEE 754-2008 but it only defines a single NaN value and does not distinguish between signalling and quiet NaNs. Thus, our implementation cannot verify whether an operation with NaN operands returns one of the input NaNs, propagating debug information encoded in the NaN, as recommended by the IEEE standard. In practice, LLVM does not attempt to preserve information in NaNs, so this limitation does not affect our ability to verify LLVM optimizations.

IMO that recommendation from the IEEE standard is silly. Although the discussion in #48523 rightly points out that payload tagging has useful applications, I don't think that extends to propagating the payload through arithmetic.

edit: But IIUC, this change is done for performance reasons, not to improve NaN propagation?

@oscardssmith
Copy link
Member Author

yeah. This is (I believe) a minor performance benefit, but also makes NaN propagation a little more reliable (although LLVM still is allowed to mess with us)

@Seelengrab
Copy link
Contributor

The "return x on NaN input" policy is consistent with x86, which (I think) is what we're doing in @fastmath, right? See also https://www.felixcloutier.com/x86/minps, though trigonometric equivalents in particular have.. underspecified behavior, raising floating point exceptions on SNaN, and unspecified behavior on QNaN (see https://www.felixcloutier.com/x86/fcos and https://www.felixcloutier.com/x86/fsincos)...

@N5N3 N5N3 merged commit 1aa65c3 into JuliaLang:master Apr 11, 2023
@oscardssmith oscardssmith deleted the return-better-nans branch April 11, 2023 16:09
Xnartharax pushed a commit to Xnartharax/julia that referenced this pull request Apr 19, 2023
…iaLang#49285)

* when x is NaN in trig functions return x rather than NaN

* prefer `isnan(x) | isnan(y)`

---------

Co-authored-by: mikmoore <95002244+mikmoore@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
maths Mathematical functions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants