Last active
February 5, 2023 09:15
-
-
Save mrbid/ac6724a81987ba21f14c485899aef8bc to your computer and use it in GitHub Desktop.
tanh implementations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // James William Fletcher (github.com/mrbid) (June 2022) | |
| #include <stdio.h> | |
| #include <math.h> | |
| static inline float mycosh(float f) | |
| { | |
| return (powf(2.718281746F, f)*0.5f) + (powf(2.718281746F, -f)*0.5f); | |
| } | |
| static inline float mysinh(float f) | |
| { | |
| return 0.5f * (powf(2.718281746F, f) - powf(2.718281746F, -f)); | |
| } | |
| static inline float mytanh1(float f) | |
| { | |
| return mysinh(f) / mycosh(f); | |
| } | |
| static inline float mytanh2(const float x) | |
| { | |
| return ((expf(x) - expf(-x))/(expf(x) + expf(-x))); | |
| } | |
| static inline float mytanh3(float f) | |
| { | |
| const float pe = powf(7.389056206F, f); | |
| return (pe-1.f) / (pe+1.f); | |
| } | |
| int main() | |
| { | |
| printf("mycosh(): %.9f\n", mycosh(0.2f)); | |
| printf("coshf(): %.9f\n\n", coshf(0.2f)); | |
| printf("mysinh(): %.9f\n", mysinh(0.2f)); | |
| printf("sinhf(): %.9f\n\n", sinhf(0.2f)); | |
| printf("tanhf(): %.9f\n", tanhf(0.2f)); | |
| printf("mytanh1(): %.9f\n", mytanh1(0.2f)); | |
| printf("mytanh2(): %.9f\n", mytanh2(0.2f)); | |
| printf("mytanh3(): %.9f\n", mytanh3(0.2f)); | |
| return 0; | |
| } | |
| /* | |
| mycosh(): 1.020066738 | |
| coshf(): 1.020066738 | |
| mysinh(): 0.201335996 | |
| sinhf(): 0.201336011 | |
| tanhf(): 0.197375327 | |
| mytanh1(): 0.197375312 | |
| mytanh2(): 0.197375312 | |
| mytanh3(): 0.197375342 | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment