Skip to content
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

Add inverse hyperbolic functions asinh(), acosh() & atanh() #78404

Merged
merged 1 commit into from
Sep 1, 2023

Conversation

jynus
Copy link
Contributor

@jynus jynus commented Jun 18, 2023

GDScript has the following built-in trigonometry functions:

  • sin()
  • cos()
  • tan()
  • asin()
  • acos()
  • atan()
  • atan()
  • sinh()
  • cosh()
  • tanh()

However, it lacks the hyperbolic arc (also known as inverse
hyperbolic) functions:

  • asinh()
  • acosh()
  • atanh()

Implement them by just exposing the C++ Math library, but clamping
its values to the closes real defined value.
For the cosine, clamp input values lower than 1 to 1.
In the case of the tangent, where the limit value is infinite,
clamp it to -inf or +inf.

References #78377
Fixes godotengine/godot-proposals#7110

@jynus
Copy link
Contributor Author

jynus commented Jun 18, 2023

Aside from me being super clumsy with the syntax (working on it) I am not super-sure about the clamping to infinity- want to see what people think and will update docs to reflect those special cases if agreed.

@kleonc
Copy link
Member

kleonc commented Jun 20, 2023

cc @lawnjelly Any thoughts on clamping atanh(x) to infinities part? (see godotengine/godot-proposals#7110 (comment) and following comments)

@lawnjelly
Copy link
Member

cc @lawnjelly Any thoughts on clamping atanh(x) to infinities part? (see godotengine/godot-proposals#7110 (comment) and following comments)

I don't think I've used it, but yes clamping result to INFINITY does seem preferable to NaN, as with the other safe math functions.

@jynus
Copy link
Contributor Author

jynus commented Jun 21, 2023

This is what it now would say on acosh:

Returns the hyperbolic arc (also called inverse) cosine of x, returning a value in radians. Use it to get the angle from an angle's cosine in hyperbolic space if x is larger or equal to 1. For values of x lower than 1, it will return 0, in order to prevent acosh from returning @GDScript.NAN.

var a = acosh(2) # Returns 1.31695789692482
cosh(a) # Returns 2

var b = acosh(-1) # Returns 0

For atanh:

Returns the hyperbolic arc (also called inverse) tangent of x, returning a value in radians. Use it to get the angle from an angle's tangent in hyperbolic space if x is between -1 and 1 (non-inclusive).

In mathematics, the inverse hyperbolic tangent is only defined for -1 < x < 1 in the real set, so values equal or lower to -1 for x return negative @GDScript.INF and values equal or higher than 1 return positive @GDScript.INF in order to prevent atanh from returning @GDScript.NAN.

var a = atanh(0.9) # Returns 1.47221948958322
tanh(a) # Returns 0.9

var b = atanh(-2) # Returns -inf
tanh(b) # Returns -1

I've also added checks of infinity sign in tests.

Copy link
Member

@AThousandShips AThousandShips left a comment

Choose a reason for hiding this comment

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

LGTM

@akien-mga akien-mga changed the title Add inverse hyperbolic functions asinh(), acosh() & atanh() Add inverse hyperbolic functions asinh(), acosh() & atanh() Aug 2, 2023
@kleonc
Copy link
Member

kleonc commented Aug 4, 2023

Needs a rebase/update after #78108.

GDScript has the following built-in trigonometry functions:

- `sin()`
- `cos()`
- `tan()`
- `asin()`
- `acos()`
- `atan()`
- `atan()`
- `sinh()`
- `cosh()`
- `tanh()`

However, it lacks the hyperbolic arc (also known as inverse
hyperbolic) functions:

- `asinh()`
- `acosh()`
- `atanh()`

Implement them by just exposing the C++ Math library, but clamping
its values to the closest real defined value.
For the cosine, clamp input values lower than 1 to 1.
In the case of the tangent, where the limit value is infinite,
clamp it to -inf or +inf.

References godotengine#78377
Fixes godotengine/godot-proposals#7110
@jynus
Copy link
Contributor Author

jynus commented Sep 1, 2023

I rebased to head. Let's see if that works, as I was getting some errors on HEAD before the patch applied - it was giving me some unit testing errors unrelated to the Math module.

@jynus
Copy link
Contributor Author

jynus commented Sep 1, 2023

The errors I got were wrong compiling flags I was using. Unit tests for the new functionality (and the rest) still work normally after manual rebase @kleonc

@akien-mga akien-mga modified the milestones: 4.x, 4.2 Sep 1, 2023
@akien-mga akien-mga merged commit d134473 into godotengine:master Sep 1, 2023
15 checks passed
@akien-mga
Copy link
Member

Thanks!

@jynus jynus deleted the arc-hyperbolic branch September 5, 2023 14:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add inverse hyperbolic functions asinh(), acosh() & atanh()
5 participants