Fix tan returning -nan for inputs where abs(x) >= 2^63#5114
Fix tan returning -nan for inputs where abs(x) >= 2^63#5114dlang-bot merged 1 commit intodlang:masterfrom
Conversation
|
Attn @WalterBright who I assume wrote the original asm? |
|
|
|
Missing unittests? |
|
We should probably document what is the domain of this function, as well as others. And what the behaviour should be when it is too far outside that range - I'd have to do some looking up on that though. |
|
@ibuclaw on that topic it's worth noting that I added unit tests to verify the new behavior. The output is imprecise for such large inputs, but I think imprecision is preferable to NaN. |
|
Yeah, I just looked at the cephes code that I used for reference for the generic implementations (gdc uses this). It says that it should return |
|
It's worth noting that the documentation here suggests using fprem to bring the input into range when it's outside of it, which is what the implementation was unsuccessfully doing before and should correctly do now. Either way is fine by me, but if my change isn't the desired behavior I'll leave it up to someone else to make it right. |
|
This is a straightforward bugfix that does not alter functionality except fixing the presumed undesirable case of returning NaN for large positive or negative inputs, in which cases the result may not be accurate but is still well-defined. The functionality change made by this PR was clearly the intended functionality when the implementation was written. The diff is tiny; this is the least intrusive conceivable change that will address this bug. I suggest that this PR should be merged and, if there are any decisions made later about the desired behavior of the tangent function for large positive or negative inputs, then those should be addressed in a separate PR and not withhold the correction of what is obviously incorrect and unexpected behavior. |
Please add a bugzilla issue and link it with this PR, so that the change is visible in the changelog. https://github.com/dlang-bots/dlang-bot
CIs need to pass first - you seem to have trailing whitespace. Do you need help fixing this? |
It actually wasn't written by me, the code dates back to at least DMD 0.043, it was probably written by Walter in 2003 or earlier, maybe even in the 80's. |
|
@pineapplemachine With don signing off on this, just fix the whitespace issue and I'll pull. |
|
The spaces have been nuked |
|
Also added an issue to bugzilla: https://issues.dlang.org/show_bug.cgi?id=17562 |
The fptan instruction pushes a 1.0 onto the FPU register stack after a successful operation, but when abs(input) >= 2^63 the C2 flag is set to indicate that the input was out of bounds, and it doesn't push the 1.0. Prior to this PR, the top value of the FPU stack was popped irrespective of whether C2 was set, which in the case of an out-of-bounds input caused the input to be removed from the stack and ultimately resulted in an incorrect return value of -nan. This PR changes this behavior, only popping after fptan when C2 was not set. See: http://x86.renejeschke.de/html/file_module_x86_id_109.html * Added unit tests for handling out-of-range inputs of fptan
|
Auto-merging since Don gave LGTM. |
|
You forgot to approve the PR: I think the bot could/should warn about this, see also: |
|
Thanks for your pull request, @pineapplemachine! We are looking forward to reviewing it, and you should be hearing from a maintainer soon. Some tips to help speed things up:
Bear in mind that large or tricky changes may require multiple rounds of review and revision. Please see CONTRIBUTING.md for more information. Bugzilla references
|
|
Closed/reopened to show that the bot recognized the issue (not sure what went wrong with the first hook -I have enabled more logging (I have opened a PR to enable more logging: dlang/dlang-bot#105)
Hehe, our bot requires this formally ;-) |
|
Just saw this, was the cephes (non x86 as) path tested? If not the gdc and ldc gods will come for you. 😉 |
The fptan instruction pushes a 1.0 onto the FPU register stack after a successful operation, but when abs(input) >= 2^63 the C2 flag is set to indicate that the input was out of bounds, and it doesn't push the 1.0.
Prior to this PR, the top value of the FPU stack was popped irrespective of whether C2 was set, which in the case of an out-of-bounds input caused the input to be removed from the stack and ultimately resulted in an incorrect return value of -nan. This PR changes this behavior, only popping after fptan when C2 was not set.
See: http://x86.renejeschke.de/html/file_module_x86_id_109.html