Skip to content

Conversation

@Dayuxiaoshui
Copy link
Contributor

The ACOS operator was producing incorrect results for boundary values due to poor precision of ASIN's Taylor series expansion near x=±1.0.

Root cause:

  • ASIN used a 6-term Taylor series that converges slowly near boundaries
  • ACOS was implemented as acos(x) = π/2 - asin(x), inheriting ASIN errors
  • At x=1.0, ASIN error of 0.354874 (22.6%) caused ACOS to output 0.354874 instead of 0.0

Solution:

  • Modified ASIN to use system library function (asinf) for |x| >= 0.9
  • Modified ACOS to use system library function (acosf) for |x| >= 0.9
  • For |x| < 0.9, continue using Taylor series (accurate in this range)

This ensures high precision for boundary values while maintaining the existing behavior for values in the middle range.

Fixes #18580

The ACOS operator was producing incorrect results for boundary values
due to poor precision of ASIN's Taylor series expansion near x=±1.0.

Root cause:
- ASIN used a 6-term Taylor series that converges slowly near boundaries
- ACOS was implemented as acos(x) = π/2 - asin(x), inheriting ASIN errors
- At x=1.0, ASIN error of 0.354874 (22.6%) caused ACOS to output 0.354874
  instead of 0.0

Solution:
- Modified ASIN to use system library function (asinf) for |x| >= 0.9
- Modified ACOS to use system library function (acosf) for |x| >= 0.9
- For |x| < 0.9, continue using Taylor series (accurate in this range)

This ensures high precision for boundary values while maintaining the
existing behavior for values in the middle range.

Fixes apache#18580
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @Dayuxiaoshui, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the accuracy of the ACOS and ASIN trigonometric functions, particularly at their domain boundaries. By strategically switching to system library functions for extreme input values while retaining the efficient Taylor series for central values, the change ensures robust and precise mathematical computations without sacrificing performance where the series remains effective.

Highlights

  • Precision Improvement for ACOS and ASIN: This pull request resolves a precision issue in the ACOS operator, which previously produced incorrect results for boundary values like x=±1.0. The root cause was identified as the poor convergence of ASIN's 6-term Taylor series expansion near these boundaries, affecting ACOS due to its implementation as "π/2 - asin(x)".
  • Hybrid Implementation for ASIN and ACOS: A hybrid approach has been implemented for both ASIN and ACOS. For input values where "|x| >= 0.9", the system library functions ("asinf" and "acosf") are now utilized to ensure high precision. For values where "|x| < 0.9", the existing Taylor series expansion, which is accurate in this range, continues to be used.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request addresses a precision issue with ACOS and ASIN operators for boundary values (x=±1.0) by switching to system library functions when |x| >= 0.9. The changes for tir.asin look good, with an explicit domain check and conditional logic to use either the system library or a Taylor series expansion. For tir.acos, I've suggested adding a similar explicit domain check to ensure consistent and robust behavior for out-of-domain inputs, mirroring the implementation of tir.asin.

@tlopex
Copy link
Member

tlopex commented Dec 13, 2025

Thanks for the fix! Would you mind adding a test to cover the input? The test should be at test_tir_intric

@Dayuxiaoshui
Copy link
Contributor Author

@tlopex Ok, no problem, I'll go add some tests first.

…ests

- Lower threshold from 0.9 to 0.5 for asin/acos legalization
  * For |x| >= 0.5: use system library function for better accuracy
  * For |x| < 0.5: use Taylor series for efficiency
  * This improves precision for values near the threshold while maintaining
    performance for smaller values

- Add test_asin_acos_boundary_values() test function
  * Tests boundary values (±1.0)
  * Tests threshold switching point (±0.5)
  * Tests values below threshold (±0.49, ±0.3, 0.0)
  * Tests out-of-domain values (should return NaN)

This addresses precision issues with asin/acos near boundary values.
@Dayuxiaoshui
Copy link
Contributor Author

@tlopex

Copy link
Member

@tlopex tlopex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM Thanks!

@tlopex tlopex merged commit d375f74 into apache:main Dec 17, 2025
10 checks passed
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.

[Bug] Abnormal output result of ACOS

2 participants