コード例 #1
0
        public static void Execute()
        {
            double result;

            // Test with small number
            result = Math.Sqrt(16);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 4), "Sqrt does not produce accurate result with small input");

            // Test with large number
            result = Math.Sqrt(2432146.513);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 1559.53406920143), "Sqrt does not produce accurate result with large input");

            // Test with zero
            result = Math.Sqrt(0);
            Assert.IsTrue((result == 0), "Sqrt of zero must be zero");

            // Test with negative number
            result = Math.Sqrt(-433);
            Assert.IsTrue(double.IsNaN(result), "Sqrt of negative must return NaN");

            // Test with NaN
            result = Math.Sqrt(double.NaN);
            Assert.IsTrue(double.IsNaN(result), "Sqrt of NaN must return NaN");

            // Test with positive infinity
            result = Math.Sqrt(double.PositiveInfinity);
            Assert.IsTrue(double.IsPositiveInfinity(result), "Sqrt of PositiveInfinity must return PositiveInfinity");
        }
コード例 #2
0
        public static void Execute()
        {
            Single value;
            String result;
            String expectedResult;

            /* First start with some weird value (not really numbers) that the IEEE standard has */
            value = Single.PositiveInfinity;

            result         = value.ToString();
            expectedResult = "∞";

            Assert.IsTrue((result == expectedResult), "Single.ToString of INF doesn't work");

            value = Single.NegativeInfinity;

            result         = value.ToString();
            expectedResult = "-∞";

            Assert.IsTrue((result == expectedResult), "Single.ToString of -INF doesn't work");

            value = Single.NaN;

            result         = value.ToString();
            expectedResult = "NaN";

            Assert.IsTrue((result == expectedResult), "Single.ToString of -NaN doesn't work");

            /* Another special value is '0' */
            value = 0f;

            result         = value.ToString();
            expectedResult = "0";

            Assert.IsTrue((result == expectedResult), "Single.ToString of 0 doesn't work");

            /* A negative value */
            value = -42.42f;

            result         = value.ToString();
            expectedResult = "-42.42";

            Assert.IsTrue((result == expectedResult), "Single.ToString of negative number doesn't work");

            /* A big value (to be correct toString should convert it in scientific notation) */
            value = 9223372036854775808f;

            result         = value.ToString();
            expectedResult = "9223372036854775808";

            Assert.IsTrue((result == expectedResult), "Single.ToString of big number doesn't work");

            /* OK now a normal value */
            value = 42.42F; // It exists Single.MaxValue but it is a too big value an can be represented only on Scientific notation but then how to confront with a String?

            result         = value.ToString();
            expectedResult = "42.42";

            Assert.IsTrue((result == expectedResult), "Single.ToString of normal number doesn't work");

            // Now let's try to concat to a String using '+' operator
            result         = "The value of the Single is " + value;
            expectedResult = "The value of the Single is 42.42";

            Assert.IsTrue((result == expectedResult), "String concat (Single) doesn't work");

            // Now let's try to use '$ instead of '+'
            result = $"The value of the Single is {value}";
            // Actually 'expectedResult' should be the same so...
            Assert.IsTrue((result == expectedResult), "String format (Single) doesn't work");

            // Now let's Get the HashCode of a value
            int resultAsInt = value.GetHashCode();

            // C# interactive says that the HashCode of 42.42 is this beast: 1110027796. It should be the same for Cosmos!
            Assert.IsTrue((resultAsInt == 1110027796), "Single.GetHashCode() doesn't work");

#if false
            // Now let's try ToString() again but printed in hex (this test fails for now!)
            result         = value.ToString("X2");
            expectedResult = "0x7FFFFFFF";

            Assert.IsTrue((result == expectedResult), "Int32.ToString(X2) doesn't work");
#endif
            // OK now some mathematical operations as if we were in school!

            // First test that == works, please note that as we talking of floating point value usually does NOT works! It seems I've chosen a number (42.42) that is representable in binay form...
            Assert.IsTrue((value == 42.42f), "float operator== doesn't work");

            // Now test for greaterThan
            Assert.IsTrue((value > 20.15f), "float operator> doesn't work");

            // Now test for greaterThanEqual
            Assert.IsTrue((value >= 42.42f), "float operator>= doesn't work");

            // Now test for inequality
            Assert.IsTrue((value != 69.69f), "float operator!= doesn't work");

            // Now test lessThan
            Assert.IsTrue((value < 69.69f), "float operator< doesn't work");

            // Now test lessThanEqual
            Assert.IsTrue((value <= 42.42f), "float operator<= doesn't work");

            // Now test addition, in this case == does not work anymore evidently 44.62 is not representable in binary we resort to test it using ToString()
            Single OperationResult;
            Single otherValue = 2.20f;

            OperationResult = value + otherValue;

            Assert.IsTrue((EqualityHelper.SinglesAreEqual(OperationResult, 44.62f)), "float operator+ doesn't work");

            // Now test subtraction
            OperationResult = value - otherValue;
            //expectedResult = "40.22";

            Assert.IsTrue((EqualityHelper.SinglesAreEqual(OperationResult, 40.22f)), "float operator- doesn't work");

            // Now test multiplication
            otherValue      = 2.00f; // I'll change 'otherValue' to 2.00f because if not the result is too much wrong to make sense...
            OperationResult = value * otherValue;

            Assert.IsTrue((EqualityHelper.SinglesAreEqual(OperationResult, 84.84f)), "float operator* doesn't work");

            // Now test division
            OperationResult = value / otherValue;

            Assert.IsTrue((EqualityHelper.SinglesAreEqual(OperationResult, 21.21f)), "float operator/ doesn't work");

            // Now test division again but with dividend 0 the expected result should be Double.PositiveInfinity
            OperationResult = value / 0.00f;

            Assert.IsTrue((OperationResult == Single.PositiveInfinity), "flot operator/0 doesn't work");

            // Now test division again but with all values as 0 the expected result should be Double.NaN
            OperationResult = 0.00f / 0.00f;

            Assert.IsTrue((Single.IsNaN(OperationResult)), "float operator/(0/0) doesn't work");

            // Now test some castings operations

            sbyte valueAsSByte = (sbyte)value;
            Assert.IsTrue((valueAsSByte == (sbyte)42), "float (sbyte) operator doesn't work");

            byte valueAsByte = (byte)value;
            Assert.IsTrue((valueAsByte == (byte)42), "float (byte) operator doesn't work");

            short valueAsShort = (short)value;
            Assert.IsTrue((valueAsByte == (short)42), "float (short) operator doesn't work");

            ushort valueAsUShort = (ushort)value;
            Assert.IsTrue((valueAsUShort == (ushort)42), "float (ushort) operator doesn't work");

            int valueAsInt = (int)value;
            Assert.IsTrue((valueAsInt == (int)42), "float (int) operator doesn't work");

            uint valueAsUInt = (uint)value;
            Assert.IsTrue((valueAsUInt == (uint)42), "float (uint) operator doesn't work");

            long valueAsLong = (long)value;
            Assert.IsTrue((valueAsLong == (long)42), "float (long) operator doesn't work");

            ulong valueAsULong = (ulong)value;
            Assert.IsTrue((valueAsULong == (ulong)42), "float (ulong) operator doesn't work");

            // Let's continue with casting but the other way around
            valueAsInt = 69;
            value      = (float)valueAsInt;
            Assert.IsTrue((EqualityHelper.SinglesAreEqual(value, 69f)), "(float) from int operator doesn't work");

            valueAsLong = 69;
            value       = (float)valueAsLong;
            Assert.IsTrue((EqualityHelper.SinglesAreEqual(value, 69f)), "(float) from long operator doesn't work");

            double valueAsDouble = 69.69;
            value = (float)valueAsDouble;
            Assert.IsTrue((EqualityHelper.SinglesAreEqual(value, 69.69f)), "(float) from double operator doesn't work");

            int anInt = 69;
            value = (float)anInt;
            Assert.IsTrue((EqualityHelper.SinglesAreEqual(value, 69f)), "(float) from int operator doesn't work");

            // We put on anUInt a very big value Int32.MaxValue + 42. Why all this 42 :-) ?
            uint anUInt = 2147483689;
            value = (float)anUInt;
            Assert.IsTrue((EqualityHelper.SinglesAreEqual(value, 2147483689f)), "(float) from uint operator doesn't work");

            // We put on anUlong a very big value Int64MaxValue + 42. Hmm that '42' again :-)) ?
            ulong anULong = 9223372036854775849;
            value = (float)anULong;
            Assert.IsTrue((EqualityHelper.SinglesAreEqual(value, 9223372036854775849f)), "(float) from ulong operator doesn't work");

            value = -42.0f;
            float valueNegated = -value;
            Assert.IsTrue((EqualityHelper.SinglesAreEqual(valueNegated, 42.0f)), "(float) negation doesn't work");

            // Let's try if it works in the other way too
            value        = 42.0f;
            valueNegated = -value;
            Assert.IsTrue((EqualityHelper.SinglesAreEqual(valueNegated, -42.0f)), "(float) negation of positive float doesn't work");

            #region Parsing

            value = Single.Parse("0.4");
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(value, 0.4), "simple parsing of float works");

            value = Single.Parse("+0.3");
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(value, 0.3), "parsing of float with positive sign works!");

            value = Single.Parse("-0.4");
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(value, -0.4), "parsing of negative float works!");

            value = Single.Parse("    0.7     ");
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(value, 0.7), "float parsing ignores leading and trailing whitespaces");

            value = Single.Parse("0.4E1");
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(value, 4), "float parsing takes in account E");

            value = Single.Parse("0.4E-1");
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(value, 0.04), "float parsing works with negative E");

            Assert.IsFalse(Single.TryParse("asd4", out value), "float TryParse returns false when it fails");

            Assert.IsTrue(Single.TryParse("2.3", out value), " float TryParse returns true when it works");
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(value, 2.3), "float TryParse returns correct result when it works");

            #endregion Parsing
        }
コード例 #3
0
        public static void Execute()
        {
            Double value;
            String result;
            String expectedResult;

            /* First start with some weird value (not really numbers) that the IEEE standard has */
            value = Double.PositiveInfinity;

            result         = value.ToString();
            expectedResult = "∞";

            Assert.IsTrue((result == expectedResult), "Double.ToString of INF doesn't work");

            value = Double.NegativeInfinity;

            result         = value.ToString();
            expectedResult = "-∞";

            Assert.IsTrue((result == expectedResult), "Double.ToString of -INF doesn't work");

            value = Double.NaN;

            result         = value.ToString();
            expectedResult = "NaN";

            Assert.IsTrue((result == expectedResult), "Double.ToString of NaN doesn't work");

            /* Another special value is '0' */
            value = 0;

            result         = value.ToString();
            expectedResult = "0";

            Assert.IsTrue((result == expectedResult), "Double.ToString of 0 doesn't work");

            /* A negative value */
            value = -42.42;

            result         = value.ToString();
            expectedResult = "-42.42";

            Assert.IsTrue((result == expectedResult), "Double.ToString of negative number doesn't work");

            /* A big value (to be correct toString should convert it in scientific notation) */
            value = 9223372036854775808;

            result         = value.ToString();
            expectedResult = "9223372036854775808";

            Assert.IsTrue((result == expectedResult), "Double.ToString of big number doesn't work");

            /* OK now a normal value */
            value = 42.42; // It exists Double.MaxValue but it is a too big value an can be represented only on Scientific notation but then how to confront with a String?

            result         = value.ToString();
            expectedResult = "42.42";

            Assert.IsTrue((result == expectedResult), "Double.ToString of normal number doesn't work");

            // Now let's try to concat to a String using '+' operator
            result         = "The value of the Double is " + value;
            expectedResult = "The value of the Double is 42.42";

            Assert.IsTrue((result == expectedResult), "String concat (Double) doesn't work");

            // Now let's try to use '$ instead of '+'
            result = $"The value of the Double is {value}";
            // Actually 'expectedResult' should be the same so...
            Assert.IsTrue((result == expectedResult), "String format (Double) doesn't work");

#if false
            // Now let's Get the HashCode of a value
            int resultAsInt = value.GetHashCode();

            // actually the Hash Code of a Int32 is the same value
            Assert.IsTrue((resultAsInt == value), "Double.GetHashCode() doesn't work");


            // Now let's try ToString() again but printed in hex (this test fails for now!)
            result         = value.ToString("X2");
            expectedResult = "0x7FFFFFFF";

            Assert.IsTrue((result == expectedResult), "Int32.ToString(X2) doesn't work");
#endif
            // OK now some mathematical operations as if we were in school!

            Double expectedValue = 42.42;
            // First test that == works, please note that as we talking of floating point value usually does NOT works! It seems I've chosen a number (42.42) that is representable in binay form...
            bool CEQ = (value == expectedValue);

            Assert.IsTrue((CEQ), "double operator== doesn't work");

            // Now test for greaterThan
            Assert.IsTrue((value > 20.15), "double operator> doesn't work");

            // Now test for greaterThanEqual
            Assert.IsTrue((value >= 42.42), "double operator>= doesn't work");

            // Now test for greaterThanEqual (with NaN)
            Assert.IsTrue((value >= Double.NaN), "double operator>= (NaN) doesn't work");

            // Now test for inequality
            Assert.IsTrue((value != 69.69), "double operator!= doesn't work");

            // Now test lessThan
            Assert.IsTrue((value < 69.69), "double operator< doesn't work");

            // Now test lessThanEqual
            Assert.IsTrue((value <= 42.42), "double operator<= doesn't work");

            // Now test addition, in this case == does not work anymore evidently 44.62 is not representable in binary we resort to test it using ToString()
            Double OperationResult;
            Double otherValue = 2.20;

            OperationResult = value + otherValue;

            Assert.IsTrue((EqualityHelper.DoublesAreEqual(OperationResult, 44.62)), "double operator+ doesn't work");

            // Now test subtraction
            OperationResult = value - otherValue;

            Assert.IsTrue((EqualityHelper.DoublesAreEqual(OperationResult, 40.22)), "double operator- doesn't work");

            // Now test multiplication
            otherValue      = 2.00; // I'll change 'otherValue' to 2.00 because if not the result is too much wrong to make sense...
            OperationResult = value * otherValue;

            Assert.IsTrue((EqualityHelper.DoublesAreEqual(OperationResult, 84.84)), "double operator* doesn't work");

            // Now test division
            OperationResult = value / otherValue;

            Assert.IsTrue((EqualityHelper.DoublesAreEqual(OperationResult, 21.21)), "double operator/ doesn't work");

            // Now test division again but with dividend 0 the expected result should be Double.PositiveInfinity
            OperationResult = value / 0.00;

            Assert.IsTrue((OperationResult == Double.PositiveInfinity), "double operator/0 doesn't work");

#if false // This test fails (== with NaN does not work but this is OK as C# is wrong on this too) and the method isNaN fails
            // Now test division again but with all values as 0 the expected result should be Double.NaN
            OperationResult = 0.00 / 0.00;

            Assert.IsTrue((Double.IsNaN(OperationResult)), "double operator/(0/0) doesn't work");
#endif

            // Now test some castings operations
            byte valueAsByte = (byte)value;
            Assert.IsTrue((valueAsByte == (byte)42), "double (byte) operator doesn't work");

            short valueAsShort = (short)value;
            Assert.IsTrue((valueAsByte == (short)42), "double (short) operator doesn't work");

            int valueAsInt = (int)value;
            Assert.IsTrue((valueAsInt == (int)42), "double (int) operator doesn't work");

            long valueAsLong = (long)value;
            Assert.IsTrue((valueAsLong == (long)42), "double (long) operator doesn't work");

            // We put on anUInt a very big value Int32.MaxValue + 42. Why all this 42 :-) ?
            uint anUInt = 2147483689;
            value = (double)anUInt;
            Assert.IsTrue((EqualityHelper.DoublesAreEqual(value, 2147483689d)), "(double) from uint operator doesn't work");

            // We put on anUlong a very big value Int64MaxValue + 42. Hmm that '42' again :-)) ?
            ulong anULong = 9223372036854775849;
            value = (double)anULong;
            Assert.IsTrue((EqualityHelper.DoublesAreEqual(value, 9223372036854775849d)), "(double) from ulong operator doesn't work");

            value = -42.0;
            double valueNegated = -value;
            Assert.IsTrue((EqualityHelper.DoublesAreEqual(valueNegated, 42d)), "(double) negation doesn't work");

            // Let's try if it works in the other way too
            value        = 42.0;
            valueNegated = -value;
            Assert.IsTrue((EqualityHelper.DoublesAreEqual(valueNegated, -42.0f)), "(double) negation of positive double doesn't work");
        }
コード例 #4
0
        public static void Execute()
        {
            double result;

            // Test with small number
            result = Math.Sqrt(16);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 4), "Sqrt does not produce accurate result with small input");

            // Test with large number
            result = Math.Sqrt(2432146.513);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 1559.53406920143), "Sqrt does not produce accurate result with large input");

            // Test with zero
            result = Math.Sqrt(0);
            Assert.IsTrue((result == 0), "Sqrt of zero must be zero");

            // Test with negative number
            result = Math.Sqrt(-433);
            Assert.IsTrue(double.IsNaN(result), "Sqrt of negative must return NaN");

            // Test with NaN
            result = Math.Sqrt(double.NaN);
            Assert.IsTrue(double.IsNaN(result), "Sqrt of NaN must return NaN");

            // Test with positive infinity
            result = Math.Sqrt(double.PositiveInfinity);
            Assert.IsTrue(double.IsPositiveInfinity(result), "Sqrt of PositiveInfinity must return PositiveInfinity");

            #region Math.Exp

            //Test with integer
            result = Math.Exp(2);
            Assert.IsTrue((result == 7.38905609893065), "e^2 is equal to 7.38905609893065");

            //Test with double exponent
            result = Math.Exp(1.5);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 4.48168907033806), "e^1.5 returns correct result");

            result = Math.Exp(0);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 1), "e^0 gives correct result");

            result = Math.Exp(1);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, Math.E), "e^1 gives correct result");

            result = Math.Exp(double.PositiveInfinity);
            Assert.IsTrue(result == double.PositiveInfinity, "e^Infinity gives correct result");

            result = Math.Exp(double.NegativeInfinity);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 0), "e^-Infinity gives correct result");

            result = Math.Exp(double.NaN);
            Assert.IsTrue(double.IsNaN(result), "e^NaN gives correct result");

            result = Math.Exp(double.MaxValue);
            Assert.IsTrue(double.IsPositiveInfinity(result), "e^0 gives correct result");

            result = Math.Exp(double.MinValue);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 0), "e^0 gives correct result");

            #endregion Math.Exp

            #region Math.Pow

            //Test with integer power
            result = Math.Pow(2, 2);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 4), "2^2 gives accurate result");

            //Test with decimal power
            result = Math.Pow(9, 0.5);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, Math.Sqrt(9)), "9^0.5 gives same answer as sqrt(9)");

            //Test with negative base
            result = Math.Pow(-2, 2);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 4), "Math.Pow gives correct result when raising negative number to even power");

            result = Math.Pow(-2, 3);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, -8), "Math.Pow gives correct result when raising negative number to odd power");

            //Test with negative power
            result = Math.Pow(2, -1);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 0.5), "Pow gives correct results when handling negative powers");

            //Have double as base
            result = Math.Pow(0.5, 2);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 0.25), "Pow gives correct solution with double base");

            //x = Nan
            result = Math.Pow(double.NaN, 2);
            Assert.IsTrue(double.IsNaN(result), "Pow gives correct solution when x is NaN");
            //Y = Nan
            result = Math.Pow(10, double.NaN);
            Assert.IsTrue(double.IsNaN(result), "Pow gives correct solution when y is NaN");
            //y = 0
            result = Math.Pow(10, 0);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 1), "Pow gives correct solution when y is 0");
            //x = -Inf y < 0 == 0
            result = Math.Pow(double.NegativeInfinity, -2);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 0), "Pow gives correct solution when X is -INF and y is negative");
            //x = -Inf y > 0 && y is even == Inf
            result = Math.Pow(double.NegativeInfinity, 2);
            Assert.IsTrue(double.IsPositiveInfinity(result), "Pow gives correct solution when x is -INF and y is even");
            //x is -INF and y is positive odd == -INF
            result = Math.Pow(double.NegativeInfinity, 3);
            Assert.IsTrue(double.IsNegativeInfinity(result), "Pow gives correct solution when x is -INF and y is odd");
            //x < 0 && y is not integer or special case
            result = Math.Pow(-3, 0.25);
            Assert.IsTrue(double.IsNaN(result), "Pow gives correct solution when x is negative and y is non integer");
            //x = -1 && y is Inf == Nan
            result = Math.Pow(-1, double.PositiveInfinity);
            Assert.IsTrue(double.IsNaN(result), "Pow gives correct solution when x is -1 and y is INF");
            //x = -1 && y is -Inf == Nan
            result = Math.Pow(-1, double.NegativeInfinity);
            Assert.IsTrue(double.IsNaN(result), "Pow gives correct solution when x is -1 and y is -INF");
            //-1 < x < 1 + y = -Inf
            result = Math.Pow(-0.25, double.NegativeInfinity);
            Assert.IsTrue(double.IsPositiveInfinity(result), "Pow gives correct solution when -1 < x < 0 and y = -INF");
            result = Math.Pow(0.25, double.NegativeInfinity);
            Assert.IsTrue(double.IsPositiveInfinity(result), "Pow gives correct solution when 0 < x < 1 and y = -INF");
            //-1 < x < 1 + y = Inf
            result = Math.Pow(-0.25, double.PositiveInfinity);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 0), "Pow gives correct solution when -1 < x < 0 and y is INF");
            result = Math.Pow(0.25, double.PositiveInfinity);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 0), "Pow gives correct solution when 0 < x < 1 and y is INF");
            //-1 > x || x > 1 + y = -Inf
            result = Math.Pow(-1.5, double.NegativeInfinity);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 0), "Pow gives correct solution when x < -1 and y is -INF");
            result = Math.Pow(1.5, double.NegativeInfinity);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 0), "Pow gives correct solution when x > 1 and y is -INF");
            //-1 > x || x > 1 + y = Inf
            result = Math.Pow(-1.25, double.PositiveInfinity);
            Assert.IsTrue(double.IsPositiveInfinity(result), "Pow gives correct solution when -1 > x and y = INF");
            result = Math.Pow(1.25, double.PositiveInfinity);
            Assert.IsTrue(double.IsPositiveInfinity(result), "Pow gives correct solution when x > 1 and y = INF");
            //x = 0 y > 0
            result = Math.Pow(0, 2);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 0), "Pow gives correct solution when x = 0 any y > 0 ");
            //x = 0 y < 0
            result = Math.Pow(0, -3);
            Assert.IsTrue(double.IsPositiveInfinity(result), "Pow gives correct solution when x is 0 and y < 0 ");
            //x = inf y < 0
            result = Math.Pow(double.PositiveInfinity, -5);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 0), "Pow gives correct solution when x is INF and y < 0 ");
            //x = inf y > 0
            result = Math.Pow(double.PositiveInfinity, 5);
            Assert.IsTrue(double.IsPositiveInfinity(result), "Pow gives correct solution when x is INF and y > 0 ");

            #endregion Math.Pow
        }
コード例 #5
0
        public static void Execute()
        {
            double result;

            #region Math.Asin

            result = Math.Asin(1.1);
            Assert.IsTrue(double.IsNaN(result), "Math.Asin returns NaN for values larger than 1");

            result = Math.Asin(-1.1);
            Assert.IsTrue(double.IsNaN(result), "Math.Asin returns NaN for values smaller than -1");

            result = Math.Asin(1);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 1.5707963267949), "Asin returns correct value for 1");

            result = Math.Asin(-1);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, -1.5707963267949), "Asin returns correct value for -1");

            result = Math.Asin(0);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 0), "Asin returns correct value for 9");

            #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 Ceiling

            result = Math.Ceiling(0d);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 0), "Ceiling gives correct value for 0");

            result = Math.Ceiling(1d);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 1), "Ceiling gives correct value for 1");

            result = Math.Ceiling(-1d);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, -1), "Ceiling gives correct value for -1");

            result = Math.Ceiling(2.5d);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 3), "Ceiling gives correct value for 2.5");

            result = Math.Ceiling(-2.5d);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, -2), "Ceiling gives correct value for -2.5");

            result = Math.Ceiling(11.2d);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 12), "Ceiling gives correct value for 11.2");

            result = Math.Ceiling(-11.2d);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, -11), "Ceiling gives correct value for -11.2");

            result = Math.Ceiling(32.8d);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 33), "Ceiling gives correct value for 32.8");

            result = Math.Ceiling(-32.8d);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, -32), "Ceiling gives correct value for -32.8");

            result = Math.Ceiling(double.NaN);
            Assert.IsTrue(double.IsNaN(result), "Ceiling gives correct value for NaN");

            result = Math.Ceiling(double.PositiveInfinity);
            Assert.IsTrue(double.IsPositiveInfinity(result), "Ceiling gives correct value for INF");

            result = Math.Ceiling(double.NegativeInfinity);
            Assert.IsTrue(double.IsNegativeInfinity(result), "Ceiling gives correct value for -INF");

            result = Math.Ceiling((double)int.MaxValue + 2.5);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, (double)int.MaxValue + 3), "Ceiling works for values larger than an int can hold. " + result + " expected " + (double)int.MaxValue + 3);

            #endregion Ceiling

            #region Floor

            result = Math.Floor(0d);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 0), "Floor gives correct value for 0");

            result = Math.Floor(1d);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 1), "Floor gives correct value for 1");

            result = Math.Floor(-1d);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, -1), "Floor gives correct value for -1");

            result = Math.Floor(2.5d);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 2), "Floor gives correct value for 2.5");

            result = Math.Floor(-2.5d);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, -3), "Floor gives correct value for -2.5");

            result = Math.Floor(11.2d);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 11), "Floor gives correct value for 11.2");

            result = Math.Floor(-11.2d);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, -12), "Floor gives correct value for -11.2");

            result = Math.Floor(32.8d);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 32), "Floor gives correct value for 32.8");

            result = Math.Floor(-32.8d);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, -33), "Floor gives correct value for -32.8");

            result = Math.Floor(double.NaN);
            Assert.IsTrue(double.IsNaN(result), "Floor gives correct value for NaN");

            result = Math.Floor(double.PositiveInfinity);
            Assert.IsTrue(double.IsPositiveInfinity(result), "Floor gives correct value for INF");

            result = Math.Floor(double.NegativeInfinity);
            Assert.IsTrue(double.IsNegativeInfinity(result), "Floor gives correct value for -INF");

            result = Math.Floor((double)int.MaxValue + 2.5);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, (double)int.MaxValue + 2), "Floor works for values larger than an int can hold. " + result + " expected " + (double)int.MaxValue + 2);

            #endregion Floor

            #region Math.Cos

            result = Math.Cos(4);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, -0.6536436208636), "Math.Cos works with positive number");

            result = Math.Cos(0);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 1), "Cos works with 0");

            result = Math.Cos(1);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 0.5403023058681), "Cos gives correct answer for 1");

            result = Math.Cos(-1);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 0.5403023058681), "Cos gives correct answer for -1");

            result = Math.Cos(double.NaN);
            Assert.IsTrue(double.IsNaN(result), "Cos works with NaN");

            result = Math.Cos(double.PositiveInfinity);
            Assert.IsTrue(double.IsNaN(result), "Cos works with INF");

            result = Math.Cos(double.PositiveInfinity);
            Assert.IsTrue(double.IsNaN(result), "Cos works with -INF");

            result = Math.Cos(Math.PI);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, -1), "Cos gives correct answer for PI");

            result = Math.Cos(Math.PI / 2);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 6.12323399573677E-17), "Cos gives correct answer for PI / 2");

            result = Math.Cos(Math.PI / 3);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 0.5), "Cos gives correct answer for PI / 3");

            #endregion Math.Cos

            #region Math.Log

            result = Math.Log(10);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 2.30258509299405), "Math.Log base e works with positive numbers");

            result = Math.Log(Math.E);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 1), "Math.Log base gives correct value for e");

            result = Math.Log(Math.E * Math.E);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 2), "Math.Log base gives correct value for e^2");

            result = Math.Log(0);
            Assert.IsTrue(double.IsNegativeInfinity(result), "Math.Log base e gives correct value for 0");

            result = Math.Log(-1.5);
            Assert.IsTrue(double.IsNaN(result), "Math.Log base e gives correct answer for negative numbers");

            result = Math.Log(double.NaN);
            Assert.IsTrue(double.IsNaN(result), "Log base e returns NaN for NaN");

            result = Math.Log(double.PositiveInfinity);
            Assert.IsTrue(double.IsPositiveInfinity(result), "Log base e return INF for INF");

            result = Math.Log(double.NegativeInfinity);
            Assert.IsTrue(double.IsNaN(result), "Log base e return NaN for -INF");

            result = Math.Log10(100);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 2), "Math.Log10 gives correct value for integer exponent");

            result = Math.Log(50);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 3.91202300542814), "Log10 gives correct value for double exponent");

            result = Math.Log10(double.NaN);
            Assert.IsTrue(double.IsNaN(result), "Log10 returns NaN when being called with NaN");

            result = Math.Log(4, 2);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 2), "Log with base gives correct result with called with int values");

            result = Math.Log(7.5, 2.5);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 2.19897784671579), "Log with base gives correct result with double values");

            #endregion Math.Log

            #region Math.Sin

            result = Math.Sin(4);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, -0.7568024953079), "Math.Sin works with positive number");

            result = Math.Sin(0);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 0), "Sin works with 0");

            result = Math.Sin(1);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 0.8414709848079), "Sin gives correct answer for 1");

            result = Math.Sin(-1);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, -0.8414709848079), "Sin gives correct answer for -1");

            result = Math.Sin(double.NaN);
            Assert.IsTrue(double.IsNaN(result), "Sin works with NaN");

            result = Math.Sin(double.PositiveInfinity);
            Assert.IsTrue(double.IsNaN(result), "Sin works with INF");

            result = Math.Sin(double.PositiveInfinity);
            Assert.IsTrue(double.IsNaN(result), "Sin works with -INF");

            result = Math.Sin(Math.PI);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 1.22464679914735E-16), "Sin gives correct answer for PI");

            result = Math.Sin(Math.PI / 2);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 1), "Sin gives correct answer for PI / 2");

            result = Math.Sin(Math.PI / 3);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 0.866025403784439), "Sin gives correct answer for PI / 3");

            #endregion Math.Sin

            #region Math.Sqrt

            // Test with small number
            result = Math.Sqrt(16);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 4), "Sqrt does not produce accurate result with small input");

            // Test with large number
            result = Math.Sqrt(2432146.513);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 1559.53406920143), "Sqrt does not produce accurate result with large input");

            // Test with zero
            result = Math.Sqrt(0);
            Assert.IsTrue((result == 0), "Sqrt of zero must be zero");

            // Test with negative number
            result = Math.Sqrt(-433);
            Assert.IsTrue(double.IsNaN(result), "Sqrt of negative must return NaN");

            // Test with NaN
            result = Math.Sqrt(double.NaN);
            Assert.IsTrue(double.IsNaN(result), "Sqrt of NaN must return NaN");

            // Test with positive infinity
            result = Math.Sqrt(double.PositiveInfinity);
            Assert.IsTrue(double.IsPositiveInfinity(result), "Sqrt of PositiveInfinity must return PositiveInfinity");

            #endregion Math.Sqrt

            #region Math.Exp

            //Test with integer
            result = Math.Exp(2);
            Assert.IsTrue((result == 7.38905609893065), "e^2 is equal to 7.38905609893065");

            //Test with double exponent
            result = Math.Exp(1.5);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 4.48168907033806), "e^1.5 returns correct result");

            result = Math.Exp(0);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 1), "e^0 gives correct result");

            result = Math.Exp(1);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, Math.E), "e^1 gives correct result");

            result = Math.Exp(double.PositiveInfinity);
            Assert.IsTrue(result == double.PositiveInfinity, "e^Infinity gives correct result");

            result = Math.Exp(double.NegativeInfinity);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 0), "e^-Infinity gives correct result");

            result = Math.Exp(double.NaN);
            Assert.IsTrue(double.IsNaN(result), "e^NaN gives correct result");

            result = Math.Exp(double.MaxValue);
            Assert.IsTrue(double.IsPositiveInfinity(result), "e^0 gives correct result");

            result = Math.Exp(double.MinValue);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 0), "e^0 gives correct result");

            #endregion Math.Exp

            #region Math.Pow

            //Test with integer power
            result = Math.Pow(2, 2);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 4), "2^2 gives accurate result");

            //Test with decimal power
            result = Math.Pow(9, 0.5);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, Math.Sqrt(9)), "9^0.5 gives same answer as sqrt(9)");

            //Test with negative base
            result = Math.Pow(-2, 2);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 4), "Math.Pow gives correct result when raising negative number to even power");

            result = Math.Pow(-2, 3);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, -8), "Math.Pow gives correct result when raising negative number to odd power");

            //Test with negative power
            result = Math.Pow(2, -1);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 0.5), "Pow gives correct results when handling negative powers");

            //Have double as base
            result = Math.Pow(0.5, 2);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 0.25), "Pow gives correct solution with double base");

            //x = Nan
            result = Math.Pow(double.NaN, 2);
            Assert.IsTrue(double.IsNaN(result), "Pow gives correct solution when x is NaN");
            //Y = Nan
            result = Math.Pow(10, double.NaN);
            Assert.IsTrue(double.IsNaN(result), "Pow gives correct solution when y is NaN");
            //y = 0
            result = Math.Pow(10, 0);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 1), "Pow gives correct solution when y is 0");
            //x = -Inf y < 0 == 0
            result = Math.Pow(double.NegativeInfinity, -2);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 0), "Pow gives correct solution when X is -INF and y is negative");
            //x = -Inf y > 0 && y is even == Inf
            result = Math.Pow(double.NegativeInfinity, 2);
            Assert.IsTrue(double.IsPositiveInfinity(result), "Pow gives correct solution when x is -INF and y is even");
            //x is -INF and y is positive odd == -INF
            result = Math.Pow(double.NegativeInfinity, 3);
            Assert.IsTrue(double.IsNegativeInfinity(result), "Pow gives correct solution when x is -INF and y is odd");
            //x < 0 && y is not integer or special case
            result = Math.Pow(-3, 0.25);
            Assert.IsTrue(double.IsNaN(result), "Pow gives correct solution when x is negative and y is non integer");
            //x = -1 && y is Inf == Nan
            result = Math.Pow(-1, double.PositiveInfinity);
            Assert.IsTrue(double.IsNaN(result), "Pow gives correct solution when x is -1 and y is INF");
            //x = -1 && y is -Inf == Nan
            result = Math.Pow(-1, double.NegativeInfinity);
            Assert.IsTrue(double.IsNaN(result), "Pow gives correct solution when x is -1 and y is -INF");
            //-1 < x < 1 + y = -Inf
            result = Math.Pow(-0.25, double.NegativeInfinity);
            Assert.IsTrue(double.IsPositiveInfinity(result), "Pow gives correct solution when -1 < x < 0 and y = -INF");
            result = Math.Pow(0.25, double.NegativeInfinity);
            Assert.IsTrue(double.IsPositiveInfinity(result), "Pow gives correct solution when 0 < x < 1 and y = -INF");
            //-1 < x < 1 + y = Inf
            result = Math.Pow(-0.25, double.PositiveInfinity);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 0), "Pow gives correct solution when -1 < x < 0 and y is INF");
            result = Math.Pow(0.25, double.PositiveInfinity);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 0), "Pow gives correct solution when 0 < x < 1 and y is INF");
            //-1 > x || x > 1 + y = -Inf
            result = Math.Pow(-1.5, double.NegativeInfinity);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 0), "Pow gives correct solution when x < -1 and y is -INF");
            result = Math.Pow(1.5, double.NegativeInfinity);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 0), "Pow gives correct solution when x > 1 and y is -INF");
            //-1 > x || x > 1 + y = Inf
            result = Math.Pow(-1.25, double.PositiveInfinity);
            Assert.IsTrue(double.IsPositiveInfinity(result), "Pow gives correct solution when -1 > x and y = INF");
            result = Math.Pow(1.25, double.PositiveInfinity);
            Assert.IsTrue(double.IsPositiveInfinity(result), "Pow gives correct solution when x > 1 and y = INF");
            //x = 0 y > 0
            result = Math.Pow(0, 2);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 0), "Pow gives correct solution when x = 0 any y > 0 ");
            //x = 0 y < 0
            result = Math.Pow(0, -3);
            Assert.IsTrue(double.IsPositiveInfinity(result), "Pow gives correct solution when x is 0 and y < 0 ");
            //x = inf y < 0
            result = Math.Pow(double.PositiveInfinity, -5);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 0), "Pow gives correct solution when x is INF and y < 0 ");
            //x = inf y > 0
            result = Math.Pow(double.PositiveInfinity, 5);
            Assert.IsTrue(double.IsPositiveInfinity(result), "Pow gives correct solution when x is INF and y > 0 ");

            #endregion Math.Pow

            #region Math.Tan

            result = Math.Tan(0);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 0), "Tan works with 0");

            result = Math.Tan(1);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 1.5574077246549), "Tan works with 1");

            result = Math.Tan(-1);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, -1.5574077246549), "Tan works with -1");

            result = Math.Tan(10);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 0.648360827459087), "Tan works with big numbers such as 10");

            result = Math.Tan(-10);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, -0.648360827459087), "Tan works with larger negative numbers");

            result = Math.Tan(0.5);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 0.54630248984379), "Tan works with doubles");

            result = Math.Tan(-0.5);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, -0.54630248984379), "Tan works with negative doubles");

            result = Math.Tan(Math.PI);
            Assert.IsTrue(result <= -.22464679914735E-16, "Tan gives matching result for Pi but mathematically inaccurate result. " + result);

            result = Math.Tan(Math.PI / 2);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 1.63312393531954E+16), "Tan gives result matching normal Math function but incorrect in mathematical sense");

            result = Math.Tan(Math.PI / 3);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, Math.Sqrt(3)), "Tan gives correct value for PI / 3");

            result = Math.Tan(double.NegativeInfinity);
            Assert.IsTrue(double.IsNaN(result), "Tan return Nan for -INF");

            result = Math.Tan(double.PositiveInfinity);
            Assert.IsTrue(double.IsNaN(result), "Tan returns Nan for INF");

            result = Math.Tan(double.NaN);
            Assert.IsTrue(double.IsNaN(result), "Tan returns Nan for Nan");

            #endregion Math.Tan

            #region Math.Atan

            result = Math.Atan(0);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 0), "Atan works with 0");

            result = Math.Atan(1);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 0.785398163397448), "Atan works with 1");

            result = Math.Atan(-1);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, -0.785398163397448), "Atan works with -1");

            result = Math.Atan(Math.PI);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 1.26262725567891), "Atan works with PI");

            result = Math.Atan(Math.PI / 2);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 1.00388482185389), "Atan works with PI / 2");

            result = Math.Atan(Math.PI / 3);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 0.808448792630022), "Atan works with PI / 3");

            result = Math.Atan(double.NaN);
            Assert.IsTrue(double.IsNaN(result), "Atan returns NaN for NaN");

            result = Math.Atan(double.PositiveInfinity);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 1.5707963267949), "Atan works with INF");

            result = Math.Atan(double.NegativeInfinity);
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, -1.5707963267949), "Atan works with -INF");

            #endregion Math.Atan

            #region Math.Round

            Assert.IsTrue(EqualityHelper.DoublesAreEqual(1, Math.Round(0.8)), "Math.Round works for 0.8");
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(0, Math.Round(0.2)), "Math.Round works for 0.2");
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(99, Math.Round(99.2)), "Math.Round works for 99.2");
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(99, Math.Round(99d)), "Math.Round works for 99");
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(0, Math.Round(-0.2)), "Math.Round works for -0.2");
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(-1, Math.Round(-0.8)), "Math.Round works for -0.8");
            Assert.IsTrue(EqualityHelper.DoublesAreEqual(0, Math.Round(-0.000000006494348)), "Math.Round works for very small values");

            #endregion
        }