[LoongArch64] Part-5:add loongarch support in some files for LoongArch64. (#21769)
[mono-project.git] / mcs / class / corlib / Test / System / MathTest.cs
blob13d03806046fdc39af7b252198ee36ff90a8393b
1 // MathTest.cs
2 //
3 // Jon Guymon (guymon@slackworks.com)
4 // Pedro Martínez Juliá (yoros@wanadoo.es)
5 //
6 // (C) 2002 Jon Guymon
7 // Copyright (C) 2003 Pedro Martínez Juliá <yoros@wanadoo.es>
8 // Copyright (C) 2004 Novell (http://www.novell.com)
9 //
11 using System;
12 using NUnit.Framework;
14 namespace MonoTests.System
16 [TestFixture]
17 public class MathTest
19 private static double double_epsilon = 2.2204460492503131e-16; /* DBL_EPSILON = 2^-52 */
21 static double x = 0.1234;
22 static double y = 12.345;
24 [Test]
25 public void TestDecimalAbs ()
27 decimal a = -9.0M;
29 Assert.IsTrue (9.0M == Math.Abs (a), "#1");
30 Assert.IsTrue (Decimal.MaxValue == Math.Abs (Decimal.MaxValue), "#2");
31 Assert.IsTrue (Decimal.MaxValue == Math.Abs (Decimal.MinValue), "#3");
32 Assert.IsTrue (Decimal.Zero == Math.Abs (Decimal.Zero), "#4");
33 Assert.IsTrue (Decimal.One == Math.Abs (Decimal.One), "#5");
34 Assert.IsTrue (Decimal.One == Math.Abs (Decimal.MinusOne), "#6");
37 [Test]
38 public void TestDoubleAbs ()
40 double a = -9.0D;
42 Assert.IsTrue (9.0D == Math.Abs (a), "#1");
43 Assert.IsTrue (0.0D == Math.Abs (0.0D), "#2");
44 Assert.IsTrue (Double.MaxValue == Math.Abs (Double.MaxValue), "#3");
45 Assert.IsTrue (Double.MaxValue == Math.Abs (Double.MinValue), "#4");
46 Assert.IsTrue (Double.IsPositiveInfinity (Math.Abs (Double.PositiveInfinity)), "#5");
47 Assert.IsTrue (Double.IsPositiveInfinity (Math.Abs (Double.NegativeInfinity)), "#6");
48 Assert.IsTrue (Double.IsNaN (Math.Abs (Double.NaN)), "#7");
51 [Test]
52 public void TestFloatAbs ()
54 float a = -9.0F;
56 Assert.IsTrue (9.0F == Math.Abs (a), "#1");
57 Assert.IsTrue (0.0F == Math.Abs (0.0F), "#2");
58 Assert.IsTrue (Single.MaxValue == Math.Abs (Single.MaxValue), "#3");
59 Assert.IsTrue (Single.MaxValue == Math.Abs (Single.MinValue), "#4");
60 Assert.IsTrue (Single.PositiveInfinity == Math.Abs (Single.PositiveInfinity), "#5");
61 Assert.IsTrue (Single.PositiveInfinity == Math.Abs (Single.NegativeInfinity), "#6");
62 Assert.IsTrue (Single.IsNaN (Math.Abs (Single.NaN)), "#7");
65 [Test]
66 public void TestLongAbs ()
68 long a = -9L;
69 long b = Int64.MinValue;
71 Assert.IsTrue (9L == Math.Abs (a), "#1");
72 try {
73 Math.Abs (b);
74 Assert.Fail ("#2");
75 } catch (Exception e) {
76 Assert.AreEqual (typeof (OverflowException), e.GetType (), "#3");
78 Assert.IsTrue (Int64.MaxValue == Math.Abs (Int64.MaxValue), "#4");
81 [Test]
82 public void TestIntAbs ()
84 int a = -9;
85 int b = Int32.MinValue;
87 Assert.IsTrue (9 == Math.Abs (a), "#1");
88 try {
89 Math.Abs (b);
90 Assert.Fail ("#2");
91 } catch (Exception e) {
92 Assert.AreEqual (typeof (OverflowException), e.GetType (), "#3");
94 Assert.IsTrue (Int32.MaxValue == Math.Abs (Int32.MaxValue), "#4");
97 [Test]
98 public void TestSbyteAbs ()
100 sbyte a = -9;
101 sbyte b = SByte.MinValue;
103 Assert.IsTrue (9 == Math.Abs (a), "#1");
104 try {
105 Math.Abs (b);
106 Assert.Fail ("#2");
107 } catch (Exception e) {
108 Assert.AreEqual (typeof (OverflowException), e.GetType (), "#3");
110 Assert.IsTrue (SByte.MaxValue == Math.Abs (SByte.MaxValue), "#4");
113 [Test]
114 public void TestShortAbs ()
116 short a = -9;
117 short b = Int16.MinValue;
119 Assert.IsTrue (9 == Math.Abs (a), "#1");
120 try {
121 Math.Abs (b);
122 Assert.Fail ("#2");
123 } catch (Exception e) {
124 Assert.AreEqual (typeof (OverflowException), e.GetType (), "#3");
126 Assert.IsTrue (Int16.MaxValue == Math.Abs (Int16.MaxValue), "#4");
129 [Test]
130 public void TestAcos ()
132 double a = Math.Acos (x);
133 double b = 1.4470809809523457;
135 bool regularTest = (Math.Abs (a - b) <= double_epsilon);
136 if (!regularTest){
138 // On MacOS X libc acos (0.1234) returns
139 // 1.4470809809523455 (hex 0x3ff7273e62fda9ab) instead
140 // of 1.4470809809523457 (hex 0x3ff7273e62fda9ac)
142 // For now, let it go
144 if (a == 1.4470809809523455)
145 regularTest = true;
148 Assert.IsTrue (regularTest, a.ToString ("G99") + " != " + b.ToString ("G99"));
150 Assert.IsTrue (double.IsNaN (Math.Acos (-1.01D)));
151 Assert.IsTrue (double.IsNaN (Math.Acos (1.01D)));
152 Assert.IsTrue (double.IsNaN (Math.Acos (Double.MinValue)));
153 Assert.IsTrue (double.IsNaN (Math.Acos (Double.MaxValue)));
154 Assert.IsTrue (double.IsNaN (Math.Acos (Double.NegativeInfinity)));
155 Assert.IsTrue (double.IsNaN (Math.Acos (Double.PositiveInfinity)));
158 [Test]
159 public void TestAsin ()
161 double a = Math.Asin (x);
162 double b = 0.12371534584255098;
164 Assert.IsTrue ((Math.Abs (a - b) <= double_epsilon), a.ToString ("G99")
165 + " != " + b.ToString ("G99"));
166 Assert.IsTrue (double.IsNaN (Math.Asin (-1.01D)));
167 Assert.IsTrue (double.IsNaN (Math.Asin (1.01D)));
168 Assert.IsTrue (double.IsNaN (Math.Asin (Double.MinValue)));
169 Assert.IsTrue (double.IsNaN (Math.Asin (Double.MaxValue)));
170 Assert.IsTrue (double.IsNaN (Math.Asin (Double.NegativeInfinity)));
171 Assert.IsTrue (double.IsNaN (Math.Asin (Double.PositiveInfinity)));
174 [Test]
175 public void TestAtan ()
177 double a = Math.Atan (x);
178 double b = 0.12277930094473837;
179 double c = 1.5707963267948966;
180 double d = -1.5707963267948966;
182 Assert.IsTrue ((Math.Abs (a - b) <= double_epsilon), "#1: " + a.ToString ("G99")
183 + " != " + b.ToString ("G99"));
184 Assert.IsTrue (double.IsNaN (Math.Atan (double.NaN)), "should return NaN");
185 Assert.IsTrue (Math.Abs ((double) Math.Atan (double.PositiveInfinity) - c) <= 0.0000000000000001,
186 "#2: " + Math.Atan (double.PositiveInfinity).ToString ("G99") + " != " + c.ToString ("G99"));
187 Assert.IsTrue (Math.Abs ((double) Math.Atan (double.NegativeInfinity) - d) <= 0.0000000000000001,
188 "#3: " + Math.Atan (double.NegativeInfinity).ToString ("G99") + " != " + d.ToString ("G99"));
191 [Test]
192 public void TestAtan2 ()
194 double a = Math.Atan2 (x, y);
195 double b = 0.0099956168687207747;
197 Assert.IsTrue ((Math.Abs (a - b) <= double_epsilon), a.ToString ("G99")
198 + " != " + b.ToString ("G99"));
199 Assert.IsTrue (double.IsNaN (Math.Acos (-2D)));
200 Assert.IsTrue (double.IsNaN (Math.Acos (2D)));
203 // The following test is for methods that are in ECMA but they are
204 // not implemented in MS.NET. I leave them commented.
206 public void TestBigMul () {
207 int a = int.MaxValue;
208 int b = int.MaxValue;
210 Assert(((long)a * (long)b) == Math.BigMul(a,b));
214 [Test]
215 public void TestCos ()
217 double a = Math.Cos (x);
218 double b = 0.99239587670489104;
220 Assert.IsTrue ((Math.Abs (a - b) <= double_epsilon), a.ToString ("G99")
221 + " != " + b.ToString ("G99"));
222 Assert.IsTrue (double.IsNaN (Math.Cos (Double.NaN)));
223 Assert.IsTrue (double.IsNaN (Math.Cos (Double.NegativeInfinity)));
224 Assert.IsTrue (double.IsNaN (Math.Cos (Double.PositiveInfinity)));
227 [Test]
228 public void TestCosh ()
230 double a = Math.Cosh (x);
231 double b = 1.0076234465130722;
233 Assert.IsTrue ((Math.Abs (a - b) <= double_epsilon), a.ToString ("G99")
234 + " != " + b.ToString ("G99"));
235 Assert.IsTrue (Math.Cosh (double.NegativeInfinity) == double.PositiveInfinity);
236 Assert.IsTrue (Math.Cosh (double.PositiveInfinity) == double.PositiveInfinity);
237 Assert.IsTrue (double.IsNaN (Math.Cosh (double.NaN)));
240 // The following test is for methods that are in ECMA but they are
241 // not implemented in MS.NET. I leave them commented.
243 public void TestIntDivRem () {
244 int a = 5;
245 int b = 2;
246 int div = 0, rem = 0;
248 div = Math.DivRem (a, b, out rem);
250 Assert.IsTrue (rem == 1);
251 Assert.IsTrue (div == 2);
254 public void TestLongDivRem () {
255 long a = 5;
256 long b = 2;
257 long div = 0, rem = 0;
259 div = Math.DivRem (a, b, out rem);
261 Assert.IsTrue (rem == 1);
262 Assert.IsTrue (div == 2);
266 [Test]
267 public void TestSin ()
269 double a = Math.Sin (x);
270 double b = 0.12308705821137626;
272 Assert.IsTrue ((Math.Abs (a - b) <= double_epsilon), a.ToString ("G99")
273 + " != " + b.ToString ("G99"));
274 Assert.IsTrue (double.IsNaN (Math.Sin (Double.NaN)));
275 Assert.IsTrue (double.IsNaN (Math.Sin (Double.NegativeInfinity)));
276 Assert.IsTrue (double.IsNaN (Math.Sin (Double.PositiveInfinity)));
279 [Test]
280 public void TestSinh ()
282 double a = Math.Sinh (x);
283 double b = 0.12371341868561381;
285 Assert.IsTrue (Math.Abs (a - b) <= 0.0000000000000001, a.ToString ("G99")
286 + " != " + b.ToString ("G99"));
287 Assert.IsTrue (double.IsNaN (Math.Sinh (Double.NaN)), "SinH of Nan should be a Nan");
288 Assert.IsTrue (double.IsNegativeInfinity (Math.Sinh (Double.NegativeInfinity)), "SinH of -inf should be -inf");
289 Assert.IsTrue (double.IsPositiveInfinity (Math.Sinh (Double.PositiveInfinity)), "SinH of +inf should be +inf");
292 [Test]
293 public void TestTan ()
295 double a = Math.Tan (x);
296 double b = 0.12403019913793806;
298 Assert.IsTrue ((Math.Abs (a - b) <= double_epsilon), a.ToString ("G99")
299 + " != " + b.ToString ("G99"));
300 Assert.IsTrue (Double.IsNaN (Math.Tan (Double.NaN)));
301 Assert.IsTrue (Double.IsNaN (Math.Tan (Double.PositiveInfinity)));
302 Assert.IsTrue (Double.IsNaN (Math.Tan (Double.NegativeInfinity)));
305 [Test]
306 public void TestTanh ()
308 double a = Math.Tanh (x);
309 double b = 0.12277743150353424;
311 Assert.IsTrue ((Math.Abs (a - b) <= double_epsilon), a.ToString ("G99")
312 + " != " + b.ToString ("G99"));
313 Assert.IsTrue (Double.IsNaN (Math.Tanh (Double.NaN)),
314 "Tanh(NaN) should be NaN");
315 Assert.IsTrue (1 == Math.Tanh (Double.PositiveInfinity),
316 "Tanh(+Infinity) should be 1");
317 Assert.IsTrue (-1 == Math.Tanh (Double.NegativeInfinity),
318 "Tanh(-Infinity) should be -1");
321 [Test]
322 public void TestSqrt ()
324 double a = Math.Sqrt (x);
325 double b = 0.35128336140500593;
327 Assert.IsTrue ((Math.Abs (a - b) <= double_epsilon), a.ToString ("G99")
328 + " != " + b.ToString ("G99"));
329 Assert.IsTrue (Double.IsNaN (Math.Sqrt (Double.NaN)));
330 Assert.IsTrue (Double.IsPositiveInfinity (Math.Sqrt (Double.PositiveInfinity)));
331 Assert.IsTrue (Double.IsNaN (Math.Sqrt (Double.NegativeInfinity)));
334 [Test]
335 public void TestExp ()
337 double a = Math.Exp (x);
338 double b = 1.1313368651986859;
340 Assert.IsTrue ((Math.Abs (a - b) <= double_epsilon), a.ToString ("G99")
341 + " != " + b.ToString ("G99"));
342 Assert.IsTrue (double.IsNaN (Math.Exp (double.NaN)));
343 Assert.IsTrue (Math.Exp (double.NegativeInfinity) == 0);
344 Assert.IsTrue (Math.Exp (double.PositiveInfinity) == double.PositiveInfinity);
347 [Test]
348 public void TestCeiling ()
350 int iTest = 1;
351 try {
352 double a = Math.Ceiling (1.5);
353 double b = 2;
355 iTest++;
356 Assert.IsTrue ((Math.Abs (a - b) <= double_epsilon), a.ToString ("G99")
357 + " != " + b.ToString ("G99"));
358 iTest++;
359 Assert.IsTrue (Math.Ceiling (double.NegativeInfinity) == double.NegativeInfinity);
360 iTest++;
361 Assert.IsTrue (Math.Ceiling (double.PositiveInfinity) == double.PositiveInfinity);
362 iTest++;
363 Assert.IsTrue (double.IsNaN (Math.Ceiling (double.NaN)));
365 iTest++;
366 Assert.IsTrue (Double.MaxValue == Math.Ceiling (Double.MaxValue));
368 iTest++;
369 Assert.IsTrue (Double.MinValue == Math.Ceiling (Double.MinValue));
370 } catch (Exception e) {
371 Assert.Fail ("Unexpected Exception at iTest=" + iTest + ": " + e);
375 [Test]
376 public void TestDecimalCeiling()
378 decimal a = Math.Ceiling(1.5M);
379 decimal b = 2M;
381 Assert.IsTrue (a == b, "#1");
382 Assert.IsTrue (Decimal.MaxValue == Math.Ceiling(Decimal.MaxValue), "#2");
383 Assert.IsTrue (Decimal.MinValue == Math.Ceiling(Decimal.MinValue), "#3");
386 [Test]
387 public void TestFloor ()
389 double a = Math.Floor (1.5);
390 double b = 1;
392 Assert.IsTrue ((Math.Abs (a - b) <= double_epsilon), a.ToString ("G99")
393 + " != " + b.ToString ("G99"));
394 Assert.IsTrue (Math.Floor (double.NegativeInfinity) == double.NegativeInfinity);
395 Assert.IsTrue (Math.Floor (double.PositiveInfinity) == double.PositiveInfinity);
396 Assert.IsTrue (double.IsNaN (Math.Floor (double.NaN)));
398 Assert.IsTrue (Double.MaxValue == Math.Floor (Double.MaxValue));
400 Assert.IsTrue (Double.MinValue == Math.Floor (Double.MinValue));
403 [Test]
404 public void TestIEEERemainder ()
406 double a = Math.IEEERemainder (y, x);
407 double b = 0.0050000000000010592;
409 Assert.IsTrue ((Math.Abs (a - b) <= double_epsilon), a.ToString ("G99")
410 + " != " + b.ToString ("G99"));
412 Assert.IsTrue (double.IsNaN (Math.IEEERemainder (y, 0)), "Positive 0");
414 // http://www.obtuse.com/resources/negative_zero.html
415 double n0 = BitConverter.Int64BitsToDouble (Int64.MinValue);
416 Assert.IsTrue (double.IsNaN (Math.IEEERemainder (n0, 0)), "Negative 0");
418 // the "zero" remainder of negative number is negative
419 long result = BitConverter.DoubleToInt64Bits (Math.IEEERemainder (-1, 1));
420 Assert.AreEqual (Int64.MinValue, result, "Negative Dividend");
423 [Test]
424 public void TestLog ()
426 double a = Math.Log (y);
427 double b = 2.513251122797143;
429 Assert.IsTrue ((Math.Abs (a - b) <= double_epsilon), a.ToString ("G99")
430 + " != " + b.ToString ("G99"));
431 Assert.IsTrue (double.IsNaN (Math.Log (-1)));
432 Assert.IsTrue (double.IsNaN (Math.Log (double.NaN)));
434 // MS docs say this should be PositiveInfinity
435 Assert.IsTrue (Math.Log (0) == double.NegativeInfinity);
436 Assert.IsTrue (Math.Log (double.PositiveInfinity) == double.PositiveInfinity);
439 [Test]
440 public void TestLog2 ()
442 double a = Math.Log (x, y);
443 double b = -0.83251695325303621;
445 Assert.IsTrue ((Math.Abs (a - b) <= 1e-14), a + " != " + b
446 + " because diff is " + Math.Abs (a - b));
447 Assert.IsTrue (double.IsNaN (Math.Log (-1, y)));
448 Assert.IsTrue (double.IsNaN (Math.Log (double.NaN, y)));
449 Assert.IsTrue (double.IsNaN (Math.Log (x, double.NaN)));
450 Assert.IsTrue (double.IsNaN (Math.Log (double.NegativeInfinity, y)));
451 Assert.IsTrue (double.IsNaN (Math.Log (x, double.NegativeInfinity)));
452 Assert.IsTrue (double.IsNaN (Math.Log (double.PositiveInfinity, double.PositiveInfinity)));
453 Assert.IsTrue (double.IsNaN (Math.Log (2, 1)));
455 // MS docs say this should be PositiveInfinity
456 Assert.IsTrue (Math.Log (0, y) == double.NegativeInfinity);
457 Assert.IsTrue (Math.Log (double.PositiveInfinity, y) == double.PositiveInfinity);
459 Assert.IsTrue (Double.IsNaN (Math.Log (x, double.PositiveInfinity)));
462 [Test]
463 public void TestLog10 ()
465 double a = Math.Log10 (x);
466 double b = -0.90868484030277719;
468 Assert.IsTrue ((Math.Abs (a - b) <= double_epsilon), a.ToString ("G99")
469 + " != " + b.ToString ("G99"));
470 Assert.IsTrue (double.IsNaN (Math.Log10 (-1)));
471 Assert.IsTrue (double.IsNaN (Math.Log10 (double.NaN)));
473 // MS docs say this should be PositiveInfinity
474 Assert.IsTrue (Math.Log10 (0) == double.NegativeInfinity);
475 Assert.IsTrue (Math.Log10 (double.PositiveInfinity) == double.PositiveInfinity);
479 [Test]
480 public void TestPow ()
482 double precision;
483 #if MONODROID
484 // It fails on Nexus 9 with
486 // 1.3636094460602122 != 1.3636094460602119
488 // when using double_epsilon. Precision differs between different ARM CPUs, so we
489 // will just use a more conservative value
490 precision = double_epsilon * 10;
491 #else
492 precision = double_epsilon;
493 #endif
495 /* documentation cases : https://msdn.microsoft.com/en-us/library/system.math.pow%28v=vs.110%29.aspx */
497 /* x or y = NaN -> NaN */
498 Assert.IsTrue (double.IsNaN (Math.Pow ( double.NaN, double.NaN)), "#1");
499 Assert.IsTrue (double.IsNaN (Math.Pow ( double.NaN, double.NegativeInfinity)), "#2");
500 Assert.IsTrue (double.IsNaN (Math.Pow ( double.NaN, -2)), "#2");
501 Assert.IsTrue (double.IsNaN (Math.Pow ( double.NaN, -1)), "#3");
502 Assert.IsFalse (double.IsNaN (Math.Pow ( double.NaN, 0)), "#4");
503 Assert.IsTrue (double.IsNaN (Math.Pow ( double.NaN, 1)), "#5");
504 Assert.IsTrue (double.IsNaN (Math.Pow ( double.NaN, 2)), "#6");
505 Assert.IsTrue (double.IsNaN (Math.Pow ( double.NaN, double.PositiveInfinity)), "#7");
506 Assert.IsTrue (double.IsNaN (Math.Pow (double.NegativeInfinity, double.NaN)), "#8");
507 Assert.IsTrue (double.IsNaN (Math.Pow ( -2, double.NaN)), "#9");
508 Assert.IsTrue (double.IsNaN (Math.Pow ( -1, double.NaN)), "#10");
509 Assert.IsTrue (double.IsNaN (Math.Pow ( 0, double.NaN)), "#11");
510 #if !WASM
511 /* WASM returns NaN */
512 Assert.IsFalse (double.IsNaN (Math.Pow ( 1, double.NaN)), "#12");
513 #endif
514 Assert.IsTrue (double.IsNaN (Math.Pow ( 2, double.NaN)), "#13");
515 Assert.IsTrue (double.IsNaN (Math.Pow (double.PositiveInfinity, double.NaN)), "#14");
517 /* x = Any value except NaN; y = 0 -> 1 */
518 Assert.AreEqual ((double) 1, Math.Pow (2, 0), "#15");
520 /* x = NegativeInfinity; y < 0 -> 0 */
521 Assert.AreEqual ((double) 0, Math.Pow (double.NegativeInfinity, -2), "#16");
523 /* x = NegativeInfinity; y is a positive odd integer -> NegativeInfinity */
524 Assert.AreEqual (double.NegativeInfinity, Math.Pow (double.NegativeInfinity, 3), "#17");
526 /* x = NegativeInfinity; y is positive but not an odd integer -> PositiveInfinity */
527 Assert.AreEqual (double.PositiveInfinity, Math.Pow (double.NegativeInfinity, 2), "#18");
529 /* x < 0 but not NegativeInfinity; y is not an integer, NegativeInfinity, or PositiveInfinity -> NaN */
530 Assert.IsTrue (double.IsNaN (Math.Pow (-1, 2.5)), "#19");
532 /* x = -1; y = NegativeInfinity or PositiveInfinity -> NaN */
533 #if !WASM
534 /* WASM returns NaN */
535 Assert.IsFalse (double.IsNaN (Math.Pow (-1, double.PositiveInfinity)), "#20");
536 Assert.IsFalse (double.IsNaN (Math.Pow (-1, double.NegativeInfinity)), "#21");
537 #endif
539 /* -1 < x < 1; y = NegativeInfinity -> PositiveInfinity */
540 Assert.AreEqual (double.PositiveInfinity, Math.Pow (-0.5, double.NegativeInfinity), "#22");
541 Assert.AreEqual (double.PositiveInfinity, Math.Pow (+0.5, double.NegativeInfinity), "#23");
543 /* -1 < x < 1; y = PositiveInfinity -> 0 */
544 Assert.AreEqual ((double) 0, Math.Pow (-0.5, double.PositiveInfinity), "#24");
545 Assert.AreEqual ((double) 0, Math.Pow (+0.5, double.PositiveInfinity), "#25");
547 /* x < -1 or x > 1; y = NegativeInfinity -> 0 */
548 Assert.AreEqual ((double) 0, Math.Pow (-2, double.NegativeInfinity), "#26");
549 Assert.AreEqual ((double) 0, Math.Pow (+2, double.NegativeInfinity), "#27");
551 /* x < -1 or x > 1; y = PositiveInfinity -> PositiveInfinity */
552 Assert.AreEqual (double.PositiveInfinity, Math.Pow (-2, double.PositiveInfinity), "#28");
553 Assert.AreEqual (double.PositiveInfinity, Math.Pow (+2, double.PositiveInfinity), "#29");
555 /* x = 0; y < 0 -> PositiveInfinity */
556 Assert.AreEqual (double.PositiveInfinity, Math.Pow (0, -2), "#30");
558 /* x = 0; y > 0 -> PositiveInfinity */
559 Assert.AreEqual ((double) 0, Math.Pow (0, +2), "#31");
561 /* x = 1; y is any value except NaN -> 1 */
562 #if !WASM
563 Assert.AreEqual ((double) 1, Math.Pow (1, double.NegativeInfinity), "#32");
564 #endif
565 Assert.AreEqual ((double) 1, Math.Pow (1, -2), "#33");
566 Assert.AreEqual ((double) 1, Math.Pow (1, 0), "#34");
567 Assert.AreEqual ((double) 1, Math.Pow (1, +2), "#35");
568 #if !WASM
569 Assert.AreEqual ((double) 1, Math.Pow (1, double.PositiveInfinity), "#36");
570 #endif
572 /* x = PositiveInfinity; y < 0 -> 0 */
573 Assert.AreEqual ((double) 0, Math.Pow (double.PositiveInfinity, -1), "#37");
574 Assert.AreEqual ((double) 0, Math.Pow (double.PositiveInfinity, -2), "#38");
576 /* x = PositiveInfinity; y > 0 -> PositiveInfinity */
577 Assert.AreEqual (double.PositiveInfinity, Math.Pow (double.PositiveInfinity, 1), "#39");
578 Assert.AreEqual (double.PositiveInfinity, Math.Pow (double.PositiveInfinity, 2), "#40");
580 /* other cases */
582 double a = Math.Pow (y, x);
583 double b = 1.363609446060212;
585 Assert.IsTrue (Math.Abs (a - b) <= precision, "#41 " + a.ToString ("G99") + " != " + b.ToString ("G99") + " +/- " + precision.ToString ("G99"));
586 Assert.AreEqual (double.NegativeInfinity, Math.Pow (double.NegativeInfinity, 1), "#42");
587 Assert.AreEqual (double.PositiveInfinity, Math.Pow (double.NegativeInfinity, 2), "#43");
589 Assert.AreEqual (Math.Pow (double.PositiveInfinity, double.NegativeInfinity), (double) 0, "#44");
591 Assert.AreEqual ((double) 1, Math.Pow (-1, Double.MaxValue), "#45");
592 Assert.AreEqual ((double) 1, Math.Pow (-1, Double.MinValue), "#46");
593 Assert.AreEqual ((double) 0, Math.Pow (Double.MinValue, Double.MinValue), "#47");
594 Assert.AreEqual (double.PositiveInfinity, Math.Pow (Double.MinValue, Double.MaxValue), "#48");
596 double infinity = double.PositiveInfinity;
597 Assert.AreEqual ((double) 0, Math.Pow ( 0.5, infinity), "#49");
598 Assert.AreEqual ( infinity, Math.Pow ( 0.5, -infinity), "#50");
599 Assert.AreEqual ( infinity, Math.Pow ( 2, infinity), "#51");
600 Assert.AreEqual ((double) 0, Math.Pow ( 2, -infinity), "#52");
601 Assert.AreEqual ((double) 1, Math.Pow ( infinity, 0), "#53");
602 Assert.AreEqual ((double) 1, Math.Pow (-infinity, 0), "#54");
605 [Test]
606 public void TestByteMax ()
608 byte a = 1;
609 byte b = 2;
611 Assert.IsTrue (b == Math.Max (a, b), "#1");
612 Assert.IsTrue (b == Math.Max (b, a), "#2");
615 [Test]
616 public void TestDecimalMax ()
618 decimal a = 1.5M;
619 decimal b = 2.5M;
621 Assert.IsTrue (b == Math.Max (a, b), "#1");
622 Assert.IsTrue (b == Math.Max (b, a), "#2");
625 [Test]
626 public void TestDoubleMax ()
628 double a = 1.5D;
629 double b = 2.5D;
631 Assert.IsTrue (b == Math.Max (a, b), "#1");
632 Assert.IsTrue (b == Math.Max (b, a), "#2");
634 Assert.IsTrue (Double.IsNaN (Math.Max (Double.NaN, Double.NaN)), "#3");
635 Assert.IsTrue (Double.IsNaN (Math.Max (Double.NaN, a)), "#4");
636 Assert.IsTrue (Double.IsNaN (Math.Max (b, Double.NaN)), "#5");
639 [Test]
640 public void TestFloatMax ()
642 float a = 1.5F;
643 float b = 2.5F;
645 Assert.IsTrue (b == Math.Max (a, b), "#1");
646 Assert.IsTrue (b == Math.Max (b, a), "#2");
647 Assert.IsTrue (Single.IsNaN (Math.Max (Single.NaN, Single.NaN)), "#3");
648 Assert.IsTrue (Single.IsNaN (Math.Max (Single.NaN, a)), "#4");
649 Assert.IsTrue (Single.IsNaN (Math.Max (b, Single.NaN)), "#5");
652 [Test]
653 public void TestIntMax ()
655 int a = 1;
656 int b = 2;
657 int c = 100;
658 int d = -2147483647;
660 Assert.AreEqual (b, Math.Max (a, b), "#1");
661 Assert.AreEqual (b, Math.Max (b, a), "#2");
662 Assert.AreEqual (c, Math.Max (c, d), "#3");
663 Assert.AreEqual (c, Math.Max (d, c), "#4");
666 [Test]
667 public void TestLongMax ()
669 long a = 1L;
670 long b = 2L;
672 Assert.IsTrue (b == Math.Max (a, b), "#1");
673 Assert.IsTrue (b == Math.Max (b, a), "#2");
676 [Test]
677 public void TestSbyteMax ()
679 sbyte a = 1;
680 sbyte b = 2;
682 Assert.IsTrue (b == Math.Max (a, b), "#1");
683 Assert.IsTrue (b == Math.Max (b, a), "#2");
686 [Test]
687 public void TestShortMax ()
689 short a = 1;
690 short b = 2;
692 Assert.IsTrue (b == Math.Max (a, b), "#1");
693 Assert.IsTrue (b == Math.Max (b, a), "#2");
696 [Test]
697 public void TestUintMax ()
699 uint a = 1U;
700 uint b = 2U;
702 Assert.IsTrue (b == Math.Max (a, b), "#1");
703 Assert.IsTrue (b == Math.Max (b, a), "#2");
706 [Test]
707 public void TestUlongMax ()
709 ulong a = 1UL;
710 ulong b = 2UL;
712 Assert.IsTrue (b == Math.Max (a, b), "#1");
713 Assert.IsTrue (b == Math.Max (b, a), "#2");
716 [Test]
717 public void TestUshortMax ()
719 ushort a = 1;
720 ushort b = 2;
722 Assert.IsTrue (b == Math.Max (a, b), "#1");
723 Assert.IsTrue (b == Math.Max (b, a), "#2");
726 [Test]
727 public void TestByteMin ()
729 byte a = 1;
730 byte b = 2;
732 Assert.IsTrue (a == Math.Min (a, b), "#1");
733 Assert.IsTrue (a == Math.Min (b, a), "#2");
736 [Test]
737 public void TestDecimalMin ()
739 decimal a = 1.5M;
740 decimal b = 2.5M;
742 Assert.IsTrue (a == Math.Min (a, b), "#1");
743 Assert.IsTrue (a == Math.Min (b, a), "#2");
746 [Test]
747 public void TestDoubleMin ()
749 double a = 1.5D;
750 double b = 2.5D;
752 Assert.IsTrue (a == Math.Min (a, b), "#1");
753 Assert.IsTrue (a == Math.Min (b, a), "#2");
754 Assert.IsTrue (Double.IsNaN (Math.Min (Double.NaN, Double.NaN)), "#3");
755 Assert.IsTrue (Double.IsNaN (Math.Min (Double.NaN, a)), "#4");
756 Assert.IsTrue (Double.IsNaN (Math.Min (b, Double.NaN)), "#5");
759 [Test]
760 public void TestFloatMin ()
762 float a = 1.5F;
763 float b = 2.5F;
765 Assert.IsTrue (a == Math.Min (a, b), "#1");
766 Assert.IsTrue (a == Math.Min (b, a), "#2");
767 Assert.IsTrue (Single.IsNaN (Math.Min (Single.NaN, Single.NaN)), "#3");
768 Assert.IsTrue (Single.IsNaN (Math.Min (Single.NaN, a)), "#4");
769 Assert.IsTrue (Single.IsNaN (Math.Min (b, Single.NaN)), "#5");
772 [Test]
773 public void TestIntMin ()
775 int a = 1;
776 int b = 2;
778 Assert.IsTrue (a == Math.Min (a, b), "#1");
779 Assert.IsTrue (a == Math.Min (b, a), "#2");
782 [Test]
783 public void TestLongMin ()
785 long a = 1L;
786 long b = 2L;
788 Assert.IsTrue (a == Math.Min (a, b), "#1");
789 Assert.IsTrue (a == Math.Min (b, a), "#2");
792 [Test]
793 public void TestSbyteMin ()
795 sbyte a = 1;
796 sbyte b = 2;
798 Assert.IsTrue (a == Math.Min (a, b), "#1");
799 Assert.IsTrue (a == Math.Min (b, a), "#2");
802 [Test]
803 public void TestShortMin ()
805 short a = 1;
806 short b = 2;
808 Assert.IsTrue (a == Math.Min (a, b), "#1");
809 Assert.IsTrue (a == Math.Min (b, a), "#2");
812 [Test]
813 public void TestUintMin ()
815 uint a = 1U;
816 uint b = 2U;
818 Assert.IsTrue (a == Math.Min (a, b), "#1");
819 Assert.IsTrue (a == Math.Min (b, a), "#2");
822 [Test]
823 public void TestUlongMin ()
825 ulong a = 1UL;
826 ulong b = 2UL;
828 Assert.IsTrue (a == Math.Min (a, b), "#1");
829 Assert.IsTrue (a == Math.Min (b, a), "#2");
832 [Test]
833 public void TestUshortMin ()
835 ushort a = 1;
836 ushort b = 2;
838 Assert.IsTrue (a == Math.Min (a, b), "#1");
839 Assert.IsTrue (a == Math.Min (b, a), "#2");
842 [Test]
843 public void TestDecimalRound ()
845 decimal a = 1.5M;
846 decimal b = 2.5M;
848 Assert.IsTrue (Math.Round (a) == 2, "#1");
849 Assert.IsTrue (Math.Round (b) == 2, "#2");
850 Assert.IsTrue (Decimal.MaxValue == Math.Round (Decimal.MaxValue), "#3");
851 Assert.IsTrue (Decimal.MinValue == Math.Round (Decimal.MinValue), "#4");
854 [Test]
855 public void TestDecimalRound2 ()
857 decimal a = 3.45M;
858 decimal b = 3.46M;
860 Assert.AreEqual (3.4M, Math.Round (a, 1), "#1");
861 Assert.AreEqual (3.5M, Math.Round (b, 1), "#2");
864 [Test]
865 public void TestDoubleRound ()
867 double a = 1.5D;
868 double b = 2.5D;
869 double c = 0.5000000000000001; // https://github.com/mono/mono/issues/9989
871 Assert.AreEqual (2D, Math.Round (a), "#1");
872 Assert.AreEqual (2D, Math.Round (b), "#2");
873 Assert.AreEqual (1D, Math.Round (c), "#3");
874 Assert.IsTrue (Double.MaxValue == Math.Round (Double.MaxValue), "#4");
875 Assert.IsTrue (Double.MinValue == Math.Round (Double.MinValue), "#5");
878 [Test]
879 public void TestDoubleTruncate ()
881 double a = 1.2D;
882 double b = 2.8D;
883 double c = 0D;
885 Assert.AreEqual (1D, Math.Truncate (a), "#1");
886 Assert.AreEqual (2D, Math.Truncate (b), "#2");
888 Assert.AreEqual (-1D, Math.Truncate (a * -1D), "#3");
889 Assert.AreEqual (-2D, Math.Truncate (b * -1D), "#4");
891 Assert.AreEqual (0D, Math.Truncate (c), "#5");
893 Assert.IsTrue (Double.MaxValue == Math.Truncate (Double.MaxValue), "#6");
894 Assert.IsTrue (Double.MinValue == Math.Truncate (Double.MinValue), "#7");
897 [Test]
898 public void TestDecimalTruncate ()
900 decimal a = 1.2M;
901 decimal b = 2.8M;
902 decimal c = 0M;
904 Assert.AreEqual (1M, Math.Truncate (a), "#1");
905 Assert.AreEqual (2M, Math.Truncate (b), "#2");
907 Assert.AreEqual (-1M, Math.Truncate (a * -1M), "#3");
908 Assert.AreEqual (-2M, Math.Truncate (b * -1M), "#4");
910 Assert.AreEqual (0M, Math.Truncate (c), "#5");
912 Assert.IsTrue (Decimal.MaxValue == Math.Truncate (Decimal.MaxValue), "#6");
913 Assert.IsTrue (Decimal.MinValue == Math.Truncate (Decimal.MinValue), "#7");
916 [Test]
917 public void TestDoubleRound2 ()
919 double a = 3.45D;
920 double b = 3.46D;
922 Assert.AreEqual (3.4D, Math.Round (a, 1), "#1");
923 Assert.AreEqual (3.5D, Math.Round (b, 1), "#2");
924 Assert.AreEqual (-0.1, Math.Round (-0.123456789, 1), "#3");
927 [Test]
928 public void TestDoubleRound3 ()
930 Assert.AreEqual (1D, Math.Round (1D, 0, MidpointRounding.ToEven), "#1");
931 Assert.AreEqual (1D, Math.Round (1D, 0, MidpointRounding.AwayFromZero), "#2");
933 Assert.AreEqual (-1D, Math.Round (-1D, 0, MidpointRounding.ToEven), "#3");
934 Assert.AreEqual (-1D, Math.Round (-1D, 0, MidpointRounding.AwayFromZero), "#4");
936 Assert.AreEqual (1D, Math.Round (1D, 1, MidpointRounding.ToEven), "#5");
937 Assert.AreEqual (1D, Math.Round (1D, 1, MidpointRounding.AwayFromZero), "#6");
939 Assert.AreEqual (-1D, Math.Round (-1D, 1, MidpointRounding.ToEven), "#7");
940 Assert.AreEqual (-1D, Math.Round (-1D, 1, MidpointRounding.AwayFromZero), "#8");
942 Assert.AreEqual (1D, Math.Round (1.2345D, 0, MidpointRounding.ToEven), "#9");
943 Assert.AreEqual (1D, Math.Round (1.2345D, 0, MidpointRounding.AwayFromZero), "#A");
945 Assert.AreEqual (-1D, Math.Round (-1.2345D, 0, MidpointRounding.ToEven), "#B");
946 Assert.AreEqual (-1D, Math.Round (-1.2345D, 0, MidpointRounding.AwayFromZero), "#C");
948 Assert.AreEqual (1.2D, Math.Round (1.2345D, 1, MidpointRounding.ToEven), "#D");
949 Assert.AreEqual (1.2D, Math.Round (1.2345D, 1, MidpointRounding.AwayFromZero), "#E");
951 Assert.AreEqual (-1.2D, Math.Round (-1.2345D, 1, MidpointRounding.ToEven), "#F");
952 Assert.AreEqual (-1.2D, Math.Round (-1.2345D, 1, MidpointRounding.AwayFromZero), "#10");
954 Assert.AreEqual (1.23D, Math.Round (1.2345D, 2, MidpointRounding.ToEven), "#11");
955 Assert.AreEqual (1.23D, Math.Round (1.2345D, 2, MidpointRounding.AwayFromZero), "#12");
957 Assert.AreEqual (-1.23D, Math.Round (-1.2345D, 2, MidpointRounding.ToEven), "#13");
958 Assert.AreEqual (-1.23D, Math.Round (-1.2345D, 2, MidpointRounding.AwayFromZero), "#14");
960 Assert.AreEqual (1.234D, Math.Round (1.2345D, 3, MidpointRounding.ToEven), "#15");
961 Assert.AreEqual (1.235D, Math.Round (1.2345D, 3, MidpointRounding.AwayFromZero), "#16");
963 Assert.AreEqual (-1.234D, Math.Round (-1.2345D, 3, MidpointRounding.ToEven), "#17");
964 Assert.AreEqual (-1.235D, Math.Round (-1.2345D, 3, MidpointRounding.AwayFromZero), "#18");
966 Assert.AreEqual (1.2345D, Math.Round (1.2345D, 4, MidpointRounding.ToEven), "#19");
967 Assert.AreEqual (1.2345D, Math.Round (1.2345D, 4, MidpointRounding.AwayFromZero), "#1A");
969 Assert.AreEqual (-1.2345D, Math.Round (-1.2345D, 4, MidpointRounding.ToEven), "#1B");
970 Assert.AreEqual (-1.2345D, Math.Round (-1.2345D, 4, MidpointRounding.AwayFromZero), "#1C");
972 Assert.AreEqual (2D, Math.Round (1.5432D, 0, MidpointRounding.ToEven), "#1D");
973 Assert.AreEqual (2D, Math.Round (1.5432D, 0, MidpointRounding.AwayFromZero), "#1E");
975 Assert.AreEqual (-2D, Math.Round (-1.5432D, 0, MidpointRounding.ToEven), "#1F");
976 Assert.AreEqual (-2D, Math.Round (-1.5432D, 0, MidpointRounding.AwayFromZero), "#20");
978 Assert.AreEqual (1.5D, Math.Round (1.5432D, 1, MidpointRounding.ToEven), "#21");
979 Assert.AreEqual (1.5D, Math.Round (1.5432D, 1, MidpointRounding.AwayFromZero), "#22");
981 Assert.AreEqual (-1.5D, Math.Round (-1.5432D, 1, MidpointRounding.ToEven), "#23");
982 Assert.AreEqual (-1.5D, Math.Round (-1.5432D, 1, MidpointRounding.AwayFromZero), "#24");
984 Assert.AreEqual (1.54D, Math.Round (1.5432D, 2, MidpointRounding.ToEven), "#25");
985 Assert.AreEqual (1.54D, Math.Round (1.5432D, 2, MidpointRounding.AwayFromZero), "#26");
987 Assert.AreEqual (-1.54D, Math.Round (-1.5432D, 2, MidpointRounding.ToEven), "#27");
988 Assert.AreEqual (-1.54D, Math.Round (-1.5432D, 2, MidpointRounding.AwayFromZero), "#28");
990 Assert.AreEqual (1.543D, Math.Round (1.5432D, 3, MidpointRounding.ToEven), "#29");
991 Assert.AreEqual (1.543D, Math.Round (1.5432D, 3, MidpointRounding.AwayFromZero), "#2A");
993 Assert.AreEqual (-1.543D, Math.Round (-1.5432D, 3, MidpointRounding.ToEven), "#2B");
994 Assert.AreEqual (-1.543D, Math.Round (-1.5432D, 3, MidpointRounding.AwayFromZero), "#2C");
996 Assert.AreEqual (1.5432D, Math.Round (1.5432D, 4, MidpointRounding.ToEven), "#2D");
997 Assert.AreEqual (1.5432D, Math.Round (1.5432D, 4, MidpointRounding.AwayFromZero), "#2E");
999 Assert.AreEqual (-1.5432D, Math.Round (-1.5432D, 4, MidpointRounding.ToEven), "#2F");
1000 Assert.AreEqual (-1.5432D, Math.Round (-1.5432D, 4, MidpointRounding.AwayFromZero), "#30");
1002 Assert.AreEqual (63988D, Math.Round (63987.83593942D, 0, MidpointRounding.ToEven), "#31");
1003 Assert.AreEqual (63988D, Math.Round (63987.83593942D, 0, MidpointRounding.AwayFromZero), "#32");
1005 Assert.AreEqual (-63988D, Math.Round (-63987.83593942D, 0, MidpointRounding.ToEven), "#33");
1006 Assert.AreEqual (-63988D, Math.Round (-63987.83593942D, 0, MidpointRounding.AwayFromZero), "#34");
1008 Assert.AreEqual (63987.83594D, Math.Round (63987.83593942D, 5, MidpointRounding.ToEven), "#35");
1009 Assert.AreEqual (63987.83594D, Math.Round (63987.83593942D, 5, MidpointRounding.AwayFromZero), "#36");
1011 Assert.AreEqual (-63987.83594D, Math.Round (-63987.83593942D, 5, MidpointRounding.ToEven), "#37");
1012 Assert.AreEqual (-63987.83594D, Math.Round (-63987.83593942D, 5, MidpointRounding.AwayFromZero), "#38");
1014 Assert.AreEqual (63987.83593942D, Math.Round (63987.83593942D, 8, MidpointRounding.ToEven), "#39");
1015 Assert.AreEqual (63987.83593942D, Math.Round (63987.83593942D, 8, MidpointRounding.AwayFromZero), "#3A");
1017 Assert.AreEqual (-63987.83593942D, Math.Round (-63987.83593942D, 8, MidpointRounding.ToEven), "#3B");
1018 Assert.AreEqual (-63987.83593942D, Math.Round (-63987.83593942D, 8, MidpointRounding.AwayFromZero), "#3C");
1020 Assert.AreEqual (1, Math.Round (0.5, 0, MidpointRounding.AwayFromZero));
1023 [Test]
1024 public void TestDecimalSign ()
1026 decimal a = -5M;
1027 decimal b = 5M;
1029 Assert.IsTrue (Math.Sign (a) == -1, "#1");
1030 Assert.IsTrue (Math.Sign (b) == 1, "#2");
1031 Assert.IsTrue (Math.Sign (0M) == 0, "#3");
1034 [Test]
1035 public void TestDoubleSign ()
1037 double a = -5D;
1038 double b = 5D;
1040 Assert.IsTrue (Math.Sign (a) == -1, "#1");
1041 Assert.IsTrue (Math.Sign (b) == 1, "#2");
1042 Assert.IsTrue (Math.Sign (0D) == 0, "#3");
1045 [Test]
1046 public void TestFloatSign ()
1048 float a = -5F;
1049 float b = 5F;
1051 Assert.IsTrue (Math.Sign (a) == -1, "#1");
1052 Assert.IsTrue (Math.Sign (b) == 1, "#2");
1053 Assert.IsTrue (Math.Sign (0F) == 0, "#3");
1056 [Test]
1057 public void TestIntSign ()
1059 int a = -5;
1060 int b = 5;
1062 Assert.IsTrue (Math.Sign (a) == -1, "#1");
1063 Assert.IsTrue (Math.Sign (b) == 1, "#2");
1064 Assert.IsTrue (Math.Sign (0) == 0, "#3");
1067 [Test]
1068 public void TestLongSign ()
1070 long a = -5L;
1071 long b = 5L;
1073 Assert.IsTrue (Math.Sign (a) == -1, "#1");
1074 Assert.IsTrue (Math.Sign (b) == 1, "#2");
1075 Assert.IsTrue (Math.Sign (0L) == 0, "#3");
1078 [Test]
1079 public void TestSbyteSign ()
1081 sbyte a = -5;
1082 sbyte b = 5;
1084 Assert.IsTrue (Math.Sign (a) == -1, "#1");
1085 Assert.IsTrue (Math.Sign (b) == 1, "#2");
1086 Assert.IsTrue (Math.Sign (0) == 0, "#3");
1089 [Test]
1090 public void TestShortSign ()
1092 short a = -5;
1093 short b = 5;
1095 Assert.IsTrue (Math.Sign (a) == -1, "#1");
1096 Assert.IsTrue (Math.Sign (b) == 1, "#2");
1097 Assert.IsTrue (Math.Sign (0) == 0, "#3");