OLD | NEW |
---|---|
1 // The following is adapted from fdlibm (http://www.netlib.org/fdlibm), | 1 // The following is adapted from fdlibm (http://www.netlib.org/fdlibm), |
2 // | 2 // |
3 // ==================================================== | 3 // ==================================================== |
4 // Copyright (C) 1993-2004 by Sun Microsystems, Inc. All rights reserved. | 4 // Copyright (C) 1993-2004 by Sun Microsystems, Inc. All rights reserved. |
5 // | 5 // |
6 // Developed at SunSoft, a Sun Microsystems, Inc. business. | 6 // Developed at SunSoft, a Sun Microsystems, Inc. business. |
7 // Permission to use, copy, modify, and distribute this | 7 // Permission to use, copy, modify, and distribute this |
8 // software is freely granted, provided that this notice | 8 // software is freely granted, provided that this notice |
9 // is preserved. | 9 // is preserved. |
10 // ==================================================== | 10 // ==================================================== |
(...skipping 825 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
836 if (MathAbs(x) <= KCOSH_OVERFLOW) { | 836 if (MathAbs(x) <= KCOSH_OVERFLOW) { |
837 var w = MathExp(0.5 * MathAbs(x)); | 837 var w = MathExp(0.5 * MathAbs(x)); |
838 var t = 0.5 * w; | 838 var t = 0.5 * w; |
839 return t * w; | 839 return t * w; |
840 } | 840 } |
841 if (NUMBER_IS_NAN(x)) return x; | 841 if (NUMBER_IS_NAN(x)) return x; |
842 // |x| > overflowthreshold. | 842 // |x| > overflowthreshold. |
843 return INFINITY; | 843 return INFINITY; |
844 } | 844 } |
845 | 845 |
846 // ES6 draft 09-27-13, section 20.2.2.33. | |
847 // Math.tanh(x) | |
848 // Method : | |
849 // x -x | |
850 // e - e | |
851 // 0. tanh(x) is defined to be ----------- | |
852 // x -x | |
853 // e + e | |
854 // 1. reduce x to non-negative by tanh(-x) = -tanh(x). | |
855 // 2. 0 <= x <= 2**-55 : tanh(x) := x*(one+x) | |
856 // -t | |
857 // 2**-55 < x <= 1 : tanh(x) := -----; t = expm1(-2x) | |
858 // t + 2 | |
859 // 2 | |
860 // 1 <= x <= 22.0 : tanh(x) := 1- ----- ; t = expm1(2x) | |
861 // t + 2 | |
862 // 22.0 < x <= INF : tanh(x) := 1. | |
863 // | |
864 // Special cases: | |
865 // tanh(NaN) is NaN; | |
866 // only tanh(0) = 0 is exact for finite argument. | |
867 // | |
868 | |
869 define TWO_M55 = 2.77555756156289135105e-17; // 2^-55, empty lower half | |
Raymond Toy
2015/10/16 17:07:25
A bit late, but this comment is incorrect. Since
| |
870 | |
871 function MathTanh(x) { | |
872 x = x * 1; // Convert to number. | |
873 // x is Infinity or NaN | |
874 if (!NUMBER_IS_FINITE(x)) { | |
875 if (x > 0) return 1; | |
876 if (x < 0) return -1; | |
877 return x; | |
878 } | |
879 | |
880 var ax = MathAbs(x); | |
881 var z; | |
882 // |x| < 22 | |
883 if (ax < 22) { | |
884 if (ax < TWO_M55) { | |
885 // |x| < 2^-55, tanh(small) = small. | |
886 return x; | |
887 } | |
888 if (ax >= 1) { | |
889 // |x| >= 1 | |
890 var t = MathExpm1(2 * ax); | |
891 z = 1 - 2 / (t + 2); | |
892 } else { | |
893 var t = MathExpm1(-2 * ax); | |
894 z = -t / (t + 2); | |
895 } | |
896 } else { | |
897 // |x| > 22, return +/- 1 | |
898 z = 1; | |
899 } | |
900 return (x >= 0) ? z : -z; | |
901 } | |
902 | |
846 // ES6 draft 09-27-13, section 20.2.2.21. | 903 // ES6 draft 09-27-13, section 20.2.2.21. |
847 // Return the base 10 logarithm of x | 904 // Return the base 10 logarithm of x |
848 // | 905 // |
849 // Method : | 906 // Method : |
850 // Let log10_2hi = leading 40 bits of log10(2) and | 907 // Let log10_2hi = leading 40 bits of log10(2) and |
851 // log10_2lo = log10(2) - log10_2hi, | 908 // log10_2lo = log10(2) - log10_2hi, |
852 // ivln10 = 1/log(10) rounded. | 909 // ivln10 = 1/log(10) rounded. |
853 // Then | 910 // Then |
854 // n = ilogb(x), | 911 // n = ilogb(x), |
855 // if(n<0) n = n+1; | 912 // if(n<0) n = n+1; |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1022 } | 1079 } |
1023 | 1080 |
1024 //------------------------------------------------------------------- | 1081 //------------------------------------------------------------------- |
1025 | 1082 |
1026 utils.InstallFunctions(GlobalMath, DONT_ENUM, [ | 1083 utils.InstallFunctions(GlobalMath, DONT_ENUM, [ |
1027 "cos", MathCos, | 1084 "cos", MathCos, |
1028 "sin", MathSin, | 1085 "sin", MathSin, |
1029 "tan", MathTan, | 1086 "tan", MathTan, |
1030 "sinh", MathSinh, | 1087 "sinh", MathSinh, |
1031 "cosh", MathCosh, | 1088 "cosh", MathCosh, |
1089 "tanh", MathTanh, | |
1032 "log10", MathLog10, | 1090 "log10", MathLog10, |
1033 "log2", MathLog2, | 1091 "log2", MathLog2, |
1034 "log1p", MathLog1p, | 1092 "log1p", MathLog1p, |
1035 "expm1", MathExpm1 | 1093 "expm1", MathExpm1 |
1036 ]); | 1094 ]); |
1037 | 1095 |
1038 %SetForceInlineFlag(MathSin); | 1096 %SetForceInlineFlag(MathSin); |
1039 %SetForceInlineFlag(MathCos); | 1097 %SetForceInlineFlag(MathCos); |
1040 | 1098 |
1041 }) | 1099 }) |
OLD | NEW |