Early exit from tanh for values >=20 <=-20 as JS loses precision #411
+7
−3
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
tanh tends to Infinity very quickly. See the calculations of tanh at the boundary integers of 19 and 20 below. The value is calculated via wolfram alpha then pasted into a JS console to check if JS can represent the value.
tanh(19)
https://www.wolframalpha.com/input/?i=tanh+19
result = 0.99999999999999993721734415903940939665164417761315093858
JS: 0.9999999999999999
tanh(20)
https://www.wolframalpha.com/input/?i=tanh+20
result = 0.999999999999999991503291489416822045438558191190987257130
JS: 1
As can be seen, tanh(20) is equal to 1. Since tanh(Infinity) is also equal to 1 we can treat anything >= 20 as 1.
tanh is an 'odd' function so the same applies to negative values.
Benefits of exiting early:
Note: there are values between 19 and 20 that also round to 1 but the source should be kept clean at an integer value.