@@ -66,26 +66,53 @@ public void LongToFix64AndBack() {
6666
6767 [ Test ]
6868 public void DoubleToFix64AndBack ( ) {
69- var sources = new [ ] { int . MinValue , - 1.0 , - 0.0 , 0.0 , 1.0 , int . MaxValue } ;
70- var expecteds = new [ ] { int . MinValue , - 1.0 , 0.0 , 0.0 , 1.0 , int . MaxValue } ;
71- for ( int i = 0 ; i < sources . Length ; ++ i ) {
72- var expected = expecteds [ i ] ;
73- var f = ( Fix64 ) sources [ i ] ;
74- var actual = ( double ) f ;
75- Assert . AreEqual ( expected , actual ) ;
69+ var sources = new [ ] {
70+ ( double ) int . MinValue ,
71+ - ( double ) Math . PI ,
72+ - ( double ) Math . E ,
73+ - 1.0 ,
74+ - 0.0 ,
75+ 0.0 ,
76+ 1.0 ,
77+ ( double ) Math . PI ,
78+ ( double ) Math . E ,
79+ ( double ) int . MaxValue
80+ } ;
81+
82+ foreach ( var value in sources ) {
83+ AreEqualWithinPrecision ( value , ( double ) ( Fix64 ) value ) ;
7684 }
7785 }
7886
87+ static void AreEqualWithinPrecision ( decimal value1 , decimal value2 ) {
88+ Assert . IsTrue ( Math . Abs ( value2 - value1 ) < Fix64 . Precision ) ;
89+ }
90+
91+ static void AreEqualWithinPrecision ( double value1 , double value2 ) {
92+ Assert . IsTrue ( Math . Abs ( value2 - value1 ) < ( double ) Fix64 . Precision ) ;
93+ }
94+
7995 [ Test ]
8096 public void DecimalToFix64AndBack ( ) {
81- var sources = new [ ] { int . MinValue , - 123456789.123456789m , - 1.0m , - 0.0m , 0.0m , 1.0m , 123456789.123456789m , int . MaxValue } ;
82- var expecteds = new [ ] { int . MinValue , - 123456789.123456789m , - 1.0m , 0.0m , 0.0m , 1.0m , 123456789.123456789m , int . MaxValue } ;
83- for ( int i = 0 ; i < sources . Length ; ++ i ) {
84- var expected = expecteds [ i ] ;
85- var f = ( Fix64 ) sources [ i ] ;
86- var actual = ( decimal ) f ;
87- actual = decimal . Round ( actual , 9 ) ; // we're supposed to have 9 significant decimal places
88- Assert . AreEqual ( expected , actual ) ;
97+
98+ Assert . AreEqual ( Fix64 . MaxValue , ( Fix64 ) ( decimal ) Fix64 . MaxValue ) ;
99+ Assert . AreEqual ( Fix64 . MinValue , ( Fix64 ) ( decimal ) Fix64 . MinValue ) ;
100+
101+ var sources = new [ ] {
102+ int . MinValue ,
103+ - ( decimal ) Math . PI ,
104+ - ( decimal ) Math . E ,
105+ - 1.0m ,
106+ - 0.0m ,
107+ 0.0m ,
108+ 1.0m ,
109+ ( decimal ) Math . PI ,
110+ ( decimal ) Math . E ,
111+ int . MaxValue
112+ } ;
113+
114+ foreach ( var value in sources ) {
115+ AreEqualWithinPrecision ( value , ( decimal ) ( Fix64 ) value ) ;
89116 }
90117 }
91118
@@ -325,7 +352,7 @@ public void Modulus() {
325352
326353 [ Test ]
327354 public void SinBenchmark ( ) {
328- var deltas = new List < decimal > ( ) ;
355+ var deltas = new List < double > ( ) ;
329356
330357 var swf = new Stopwatch ( ) ;
331358 var swd = new Stopwatch ( ) ;
@@ -334,18 +361,18 @@ public void SinBenchmark() {
334361 for ( var angle = 0.0 ; angle <= 2 * Math . PI ; angle += 0.000004 ) {
335362 var f = ( Fix64 ) angle ;
336363 swf . Start ( ) ;
337- var actualF = Fix64 . FastSin ( f ) ;
364+ var actualF = Fix64 . Sin ( f ) ;
338365 swf . Stop ( ) ;
339- var actual = ( decimal ) actualF ;
366+ var actual = ( double ) actualF ;
340367 swd . Start ( ) ;
341368 var expectedD = Math . Sin ( angle ) ;
342369 swd . Stop ( ) ;
343- var expected = ( decimal ) expectedD ;
370+ var expected = ( double ) expectedD ;
344371 var delta = Math . Abs ( expected - actual ) ;
345372 deltas . Add ( delta ) ;
346373 }
347- Console . WriteLine ( "Max error: {0} ({1} times precision)" , deltas . Max ( ) , deltas . Max ( ) / Fix64 . Precision ) ;
348- Console . WriteLine ( "Average precision: {0} ({1} times precision)" , deltas . Average ( ) , deltas . Average ( ) / Fix64 . Precision ) ;
374+ Console . WriteLine ( "Max error: {0} ({1} times precision)" , deltas . Max ( ) , deltas . Max ( ) / ( double ) Fix64 . Precision ) ;
375+ Console . WriteLine ( "Average precision: {0} ({1} times precision)" , deltas . Average ( ) , deltas . Average ( ) / ( double ) Fix64 . Precision ) ;
349376 Console . WriteLine ( "Fix64.Sin time = {0}ms, Math.Sin time = {1}ms" , swf . ElapsedMilliseconds , swd . ElapsedMilliseconds ) ;
350377 }
351378
@@ -373,12 +400,19 @@ public void Sin() {
373400 }
374401
375402 foreach ( var val in m_testCases ) {
376- var f = ( Fix64 ) val ;
403+ var f = Fix64 . FromRaw ( val ) ;
377404 var actualF = Fix64 . Sin ( f ) ;
378405 var expected = ( decimal ) Math . Sin ( ( double ) f ) ;
379406 var delta = Math . Abs ( expected - ( decimal ) actualF ) ;
380- Assert . LessOrEqual ( delta , 0.01 , string . Format ( "Sin({0}): expected {1} but got {2}" , f , expected , actualF ) ) ;
407+ Assert . LessOrEqual ( delta , 0.003 , string . Format ( "Sin({0}): expected {1} but got {2}" , f , expected , actualF ) ) ;
381408 }
409+
410+ Console . WriteLine ( "Max delta = {0}" , m_testCases . Max ( val => {
411+ var f = Fix64 . FromRaw ( val ) ;
412+ var actualF = Fix64 . Sin ( f ) ;
413+ var expected = ( decimal ) Math . Sin ( ( double ) f ) ;
414+ return Math . Abs ( expected - ( decimal ) actualF ) ;
415+ } ) ) ;
382416 }
383417
384418 [ Test ]
@@ -392,7 +426,7 @@ public void FastSin() {
392426 }
393427
394428 foreach ( var val in m_testCases ) {
395- var f = ( Fix64 ) val ;
429+ var f = Fix64 . FromRaw ( val ) ;
396430 var actualF = Fix64 . FastSin ( f ) ;
397431 var expected = ( decimal ) Math . Sin ( ( double ) f ) ;
398432 var delta = Math . Abs ( expected - ( decimal ) actualF ) ;
@@ -424,11 +458,11 @@ public void Cos() {
424458 }
425459
426460 foreach ( var val in m_testCases ) {
427- var f = ( Fix64 ) val ;
461+ var f = Fix64 . FromRaw ( val ) ;
428462 var actualF = Fix64 . Cos ( f ) ;
429463 var expected = ( decimal ) Math . Cos ( ( double ) f ) ;
430464 var delta = Math . Abs ( expected - ( decimal ) actualF ) ;
431- Assert . LessOrEqual ( delta , 0.01 , string . Format ( "Cos({0}): expected {1} but got {2}" , f , expected , actualF ) ) ;
465+ Assert . LessOrEqual ( delta , 0.004 , string . Format ( "Cos({0}): expected {1} but got {2}" , f , expected , actualF ) ) ;
432466 }
433467 }
434468
@@ -443,7 +477,7 @@ public void FastCos() {
443477 }
444478
445479 foreach ( var val in m_testCases ) {
446- var f = ( Fix64 ) val ;
480+ var f = Fix64 . FromRaw ( val ) ;
447481 var actualF = Fix64 . FastCos ( f ) ;
448482 var expected = ( decimal ) Math . Cos ( ( double ) f ) ;
449483 var delta = Math . Abs ( expected - ( decimal ) actualF ) ;
@@ -519,7 +553,7 @@ public void Atan2() {
519553 }
520554
521555
522- [ Test ]
556+ // [Test]
523557 public void Atan2Benchmark ( ) {
524558 var deltas = new List < decimal > ( ) ;
525559
@@ -574,10 +608,11 @@ public void Equals() {
574608
575609 [ Test ]
576610 public void EqualityAndInequalityOperators ( ) {
577- foreach ( var op1 in m_testCases ) {
578- foreach ( var op2 in m_testCases ) {
579- var d1 = ( decimal ) op1 ;
580- var d2 = ( decimal ) op2 ;
611+ var sources = m_testCases . Select ( Fix64 . FromRaw ) . ToList ( ) ;
612+ foreach ( var op1 in sources ) {
613+ foreach ( var op2 in sources ) {
614+ var d1 = ( double ) op1 ;
615+ var d2 = ( double ) op2 ;
581616 Assert . True ( ( op1 == op2 ) == ( d1 == d2 ) ) ;
582617 Assert . True ( ( op1 != op2 ) == ( d1 != d2 ) ) ;
583618 Assert . False ( ( op1 == op2 ) && ( op1 != op2 ) ) ;
@@ -591,11 +626,10 @@ public void CompareTo() {
591626 var numsDecimal = nums . Select ( t => ( decimal ) t ) . ToArray ( ) ;
592627 Array . Sort ( nums ) ;
593628 Array . Sort ( numsDecimal ) ;
594- var sortedNums = nums . Select ( t => ( decimal ) t ) . ToArray ( ) ;
595- Assert . True ( sortedNums . SequenceEqual ( numsDecimal ) ) ;
629+ Assert . True ( nums . Select ( t => ( decimal ) t ) . SequenceEqual ( numsDecimal ) ) ;
596630 }
597631
598- [ Test ]
632+ // [Test]
599633 public void GenerateLuts ( ) {
600634 Fix64 . GenerateSinLut ( ) ;
601635 Fix64 . GenerateTanLut ( ) ;
0 commit comments