From c31cfcef316de035d3ee5a7bceff64438826fabe Mon Sep 17 00:00:00 2001 From: Qujak Date: Sun, 7 Jan 2018 20:06:02 +0100 Subject: [PATCH] Changed Math.Acos and added tests --- .../System/MathTest.cs | 19 +++++ .../Cosmos.System2_Plugs/System/MathImpl.cs | 70 ++++++++++++++++++- 2 files changed, 86 insertions(+), 3 deletions(-) diff --git a/Tests/Cosmos.Compiler.Tests.Bcl/System/MathTest.cs b/Tests/Cosmos.Compiler.Tests.Bcl/System/MathTest.cs index 2c92be2bc..e04c1d8eb 100644 --- a/Tests/Cosmos.Compiler.Tests.Bcl/System/MathTest.cs +++ b/Tests/Cosmos.Compiler.Tests.Bcl/System/MathTest.cs @@ -31,6 +31,25 @@ namespace Cosmos.Compiler.Tests.Bcl.System #endregion Math.Asin + #region Math.Acos + + result = Math.Acos(1.1); + Assert.IsTrue(double.IsNaN(result), "Math.Acos returns NaN for values larger than 1"); + + result = Math.Acos(-1.1); + Assert.IsTrue(double.IsNaN(result), "Math.Acos returns NaN for values smaller than -1"); + + result = Math.Acos(1); + Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 0), "Acos returns correct value for 1"); + + result = Math.Acos(-1); + Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, Math.PI), "Acos returns correct value for -1"); + + result = Math.Acos(0); + Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 1.5707963267949), "Acos returns correct value for 9"); + + #endregion Math.Acos + #region Math.Cos result = Math.Cos(4); diff --git a/source/Cosmos.System2_Plugs/System/MathImpl.cs b/source/Cosmos.System2_Plugs/System/MathImpl.cs index 47077a189..c77884abc 100644 --- a/source/Cosmos.System2_Plugs/System/MathImpl.cs +++ b/source/Cosmos.System2_Plugs/System/MathImpl.cs @@ -67,9 +67,73 @@ namespace Cosmos.System_Plugs.System public static double Acos(double x) { - if ((x > 1.0) || (x < -1.0)) - throw new ArgumentOutOfRangeException("Domain error"); - return (pio2 - Asin(x)); + double pio2_hi = 1.57079632679489655800e+00, /* 0x3FF921FB, 0x54442D18 */ + pio2_lo = 6.12323399573676603587e-17, /* 0x3C91A626, 0x33145C07 */ + pS0 = 1.66666666666666657415e-01, /* 0x3FC55555, 0x55555555 */ + pS1 = -3.25565818622400915405e-01, /* 0xBFD4D612, 0x03EB6F7D */ + pS2 = 2.01212532134862925881e-01, /* 0x3FC9C155, 0x0E884455 */ + pS3 = -4.00555345006794114027e-02, /* 0xBFA48228, 0xB5688F3B */ + pS4 = 7.91534994289814532176e-04, /* 0x3F49EFE0, 0x7501B288 */ + pS5 = 3.47933107596021167570e-05, /* 0x3F023DE1, 0x0DFDF709 */ + qS1 = -2.40339491173441421878e+00, /* 0xC0033A27, 0x1C8A2D4B */ + qS2 = 2.02094576023350569471e+00, /* 0x40002AE5, 0x9C598AC8 */ + qS3 = -6.88283971605453293030e-01, /* 0xBFE6066C, 0x1B8D0159 */ + qS4 = 7.70381505559019352791e-02; /* 0x3FB3B8C5, 0xB12E9282 */ + + double z, p, q, r, w, s, c, df; + int hx, ix; + hx = HighWord(x); + ix = hx & 0x7fffffff; + if (ix >= 0x3ff00000) + { /* |x| >= 1 */ + if (((ix - 0x3ff00000) | LowWord(x)) == 0) + { /* |x|==1 */ + if (hx > 0) + return 0.0; /* acos(1) = 0 */ + else + return Math.PI + 2.0 * pio2_lo; /* acos(-1)= pi */ + } + return (x - x) / (x - x); /* acos(|x|>1) is NaN */ + } + if (ix < 0x3fe00000) + { /* |x| < 0.5 */ + if (ix <= 0x3c600000) + return pio2_hi + pio2_lo;/*if|x|<2**-57*/ + z = x * x; + p = z * (pS0 + z * (pS1 + z * (pS2 + z * (pS3 + z * (pS4 + z * pS5))))); + q = 1 + z * (qS1 + z * (qS2 + z * (qS3 + z * qS4))); + r = p / q; + return pio2_hi - (x - (pio2_lo - x * r)); + } + else if (hx < 0) + { /* x < -0.5 */ + z = (1 + x) * 0.5; + p = z * (pS0 + z * (pS1 + z * (pS2 + z * (pS3 + z * (pS4 + z * pS5))))); + q = 1 + z * (qS1 + z * (qS2 + z * (qS3 + z * qS4))); + s = Math.Sqrt(z); + r = p / q; + w = r * s - pio2_lo; + return Math.PI - 2.0 * (s + w); + } + else + { /* x > 0.5 */ + z = (1 - x) * 0.5; + s = Math.Sqrt(z); + df = s; + //__LO(df) = 0; + Byte[] bdf = BitConverter.GetBytes(BitConverter.DoubleToInt64Bits(df)); + for (int i = 0; i < 4; i++) + { + bdf[i + (BitConverter.IsLittleEndian ? 4 : 0)] = 0; + } + df = BitConverter.ToDouble(bdf, 0); + c = (z - df * df) / (s + df); + p = z * (pS0 + z * (pS1 + z * (pS2 + z * (pS3 + z * (pS4 + z * pS5))))); + q = 1 + z * (qS1 + z * (qS2 + z * (qS3 + z * qS4))); + r = p / q; + w = r * s + c; + return 2.0 * (df + w); + } } #endregion Acos