@@ -796,6 +796,29 @@ public static void Max_Decimal()
796
796
public static void Max_Double_NotNetFramework ( double x , double y , double expectedResult )
797
797
{
798
798
AssertExtensions . Equal ( expectedResult , Math . Max ( x , y ) , 0.0 ) ;
799
+
800
+ if ( double . IsNaN ( x ) )
801
+ {
802
+ // Toggle the sign of the NaN to validate both +NaN and -NaN behave the same.
803
+ // Negate should work as well but the JIT may constant fold or do other tricks
804
+ // and normalize to a single NaN form so we do bitwise tricks to ensure we test
805
+ // the right thing.
806
+
807
+ ulong bits = BitConverter . DoubleToUInt64Bits ( x ) ;
808
+ bits ^= BitConverter . DoubleToUInt64Bits ( - 0.0 ) ;
809
+ x = BitConverter . UInt64BitsToDouble ( bits ) ;
810
+
811
+ AssertExtensions . Equal ( expectedResult , Math . Max ( x , y ) , 0.0 ) ;
812
+ }
813
+
814
+ if ( double . IsNaN ( y ) )
815
+ {
816
+ ulong bits = BitConverter . DoubleToUInt64Bits ( y ) ;
817
+ bits ^= BitConverter . DoubleToUInt64Bits ( - 0.0 ) ;
818
+ y = BitConverter . UInt64BitsToDouble ( bits ) ;
819
+
820
+ AssertExtensions . Equal ( expectedResult , Math . Max ( x , y ) , 0.0 ) ;
821
+ }
799
822
}
800
823
801
824
[ Fact ]
@@ -854,6 +877,29 @@ public static void Max_SByte()
854
877
public static void Max_Single_NotNetFramework ( float x , float y , float expectedResult )
855
878
{
856
879
AssertExtensions . Equal ( expectedResult , Math . Max ( x , y ) , 0.0f ) ;
880
+
881
+ if ( float . IsNaN ( x ) )
882
+ {
883
+ // Toggle the sign of the NaN to validate both +NaN and -NaN behave the same.
884
+ // Negate should work as well but the JIT may constant fold or do other tricks
885
+ // and normalize to a single NaN form so we do bitwise tricks to ensure we test
886
+ // the right thing.
887
+
888
+ uint bits = BitConverter . SingleToUInt32Bits ( x ) ;
889
+ bits ^= BitConverter . SingleToUInt32Bits ( - 0.0f ) ;
890
+ x = BitConverter . UInt32BitsToSingle ( bits ) ;
891
+
892
+ AssertExtensions . Equal ( expectedResult , Math . Max ( x , y ) , 0.0f ) ;
893
+ }
894
+
895
+ if ( float . IsNaN ( y ) )
896
+ {
897
+ uint bits = BitConverter . SingleToUInt32Bits ( y ) ;
898
+ bits ^= BitConverter . SingleToUInt32Bits ( - 0.0f ) ;
899
+ y = BitConverter . UInt32BitsToSingle ( bits ) ;
900
+
901
+ AssertExtensions . Equal ( expectedResult , Math . Max ( x , y ) , 0.0f ) ;
902
+ }
857
903
}
858
904
859
905
[ Fact ]
@@ -919,6 +965,29 @@ public static void Min_Decimal()
919
965
public static void Min_Double_NotNetFramework ( double x , double y , double expectedResult )
920
966
{
921
967
AssertExtensions . Equal ( expectedResult , Math . Min ( x , y ) , 0.0 ) ;
968
+
969
+ if ( double . IsNaN ( x ) )
970
+ {
971
+ // Toggle the sign of the NaN to validate both +NaN and -NaN behave the same.
972
+ // Negate should work as well but the JIT may constant fold or do other tricks
973
+ // and normalize to a single NaN form so we do bitwise tricks to ensure we test
974
+ // the right thing.
975
+
976
+ ulong bits = BitConverter . DoubleToUInt64Bits ( x ) ;
977
+ bits ^= BitConverter . DoubleToUInt64Bits ( - 0.0 ) ;
978
+ x = BitConverter . UInt64BitsToDouble ( bits ) ;
979
+
980
+ AssertExtensions . Equal ( expectedResult , Math . Min ( x , y ) , 0.0 ) ;
981
+ }
982
+
983
+ if ( double . IsNaN ( y ) )
984
+ {
985
+ ulong bits = BitConverter . DoubleToUInt64Bits ( y ) ;
986
+ bits ^= BitConverter . DoubleToUInt64Bits ( - 0.0 ) ;
987
+ y = BitConverter . UInt64BitsToDouble ( bits ) ;
988
+
989
+ AssertExtensions . Equal ( expectedResult , Math . Min ( x , y ) , 0.0 ) ;
990
+ }
922
991
}
923
992
924
993
[ Fact ]
@@ -977,6 +1046,29 @@ public static void Min_SByte()
977
1046
public static void Min_Single_NotNetFramework ( float x , float y , float expectedResult )
978
1047
{
979
1048
AssertExtensions . Equal ( expectedResult , Math . Min ( x , y ) , 0.0f ) ;
1049
+
1050
+ if ( float . IsNaN ( x ) )
1051
+ {
1052
+ // Toggle the sign of the NaN to validate both +NaN and -NaN behave the same.
1053
+ // Negate should work as well but the JIT may constant fold or do other tricks
1054
+ // and normalize to a single NaN form so we do bitwise tricks to ensure we test
1055
+ // the right thing.
1056
+
1057
+ uint bits = BitConverter . SingleToUInt32Bits ( x ) ;
1058
+ bits ^= BitConverter . SingleToUInt32Bits ( - 0.0f ) ;
1059
+ x = BitConverter . UInt32BitsToSingle ( bits ) ;
1060
+
1061
+ AssertExtensions . Equal ( expectedResult , Math . Min ( x , y ) , 0.0f ) ;
1062
+ }
1063
+
1064
+ if ( float . IsNaN ( y ) )
1065
+ {
1066
+ uint bits = BitConverter . SingleToUInt32Bits ( y ) ;
1067
+ bits ^= BitConverter . SingleToUInt32Bits ( - 0.0f ) ;
1068
+ y = BitConverter . UInt32BitsToSingle ( bits ) ;
1069
+
1070
+ AssertExtensions . Equal ( expectedResult , Math . Min ( x , y ) , 0.0f ) ;
1071
+ }
980
1072
}
981
1073
982
1074
[ Fact ]
@@ -2566,6 +2658,29 @@ public static void Log2(double value, double expectedResult, double allowedVaria
2566
2658
public static void MaxMagnitude ( double x , double y , double expectedResult )
2567
2659
{
2568
2660
AssertExtensions . Equal ( expectedResult , Math . MaxMagnitude ( x , y ) , 0.0 ) ;
2661
+
2662
+ if ( double . IsNaN ( x ) )
2663
+ {
2664
+ // Toggle the sign of the NaN to validate both +NaN and -NaN behave the same.
2665
+ // Negate should work as well but the JIT may constant fold or do other tricks
2666
+ // and normalize to a single NaN form so we do bitwise tricks to ensure we test
2667
+ // the right thing.
2668
+
2669
+ ulong bits = BitConverter . DoubleToUInt64Bits ( x ) ;
2670
+ bits ^= BitConverter . DoubleToUInt64Bits ( - 0.0 ) ;
2671
+ x = BitConverter . UInt64BitsToDouble ( bits ) ;
2672
+
2673
+ AssertExtensions . Equal ( expectedResult , Math . MaxMagnitude ( x , y ) , 0.0 ) ;
2674
+ }
2675
+
2676
+ if ( double . IsNaN ( y ) )
2677
+ {
2678
+ ulong bits = BitConverter . DoubleToUInt64Bits ( y ) ;
2679
+ bits ^= BitConverter . DoubleToUInt64Bits ( - 0.0 ) ;
2680
+ y = BitConverter . UInt64BitsToDouble ( bits ) ;
2681
+
2682
+ AssertExtensions . Equal ( expectedResult , Math . MaxMagnitude ( x , y ) , 0.0 ) ;
2683
+ }
2569
2684
}
2570
2685
2571
2686
[ Theory ]
@@ -2589,6 +2704,29 @@ public static void MaxMagnitude(double x, double y, double expectedResult)
2589
2704
public static void MinMagnitude ( double x , double y , double expectedResult )
2590
2705
{
2591
2706
AssertExtensions . Equal ( expectedResult , Math . MinMagnitude ( x , y ) , 0.0 ) ;
2707
+
2708
+ if ( double . IsNaN ( x ) )
2709
+ {
2710
+ // Toggle the sign of the NaN to validate both +NaN and -NaN behave the same.
2711
+ // Negate should work as well but the JIT may constant fold or do other tricks
2712
+ // and normalize to a single NaN form so we do bitwise tricks to ensure we test
2713
+ // the right thing.
2714
+
2715
+ ulong bits = BitConverter . DoubleToUInt64Bits ( x ) ;
2716
+ bits ^= BitConverter . DoubleToUInt64Bits ( - 0.0 ) ;
2717
+ x = BitConverter . UInt64BitsToDouble ( bits ) ;
2718
+
2719
+ AssertExtensions . Equal ( expectedResult , Math . MinMagnitude ( x , y ) , 0.0 ) ;
2720
+ }
2721
+
2722
+ if ( double . IsNaN ( y ) )
2723
+ {
2724
+ ulong bits = BitConverter . DoubleToUInt64Bits ( y ) ;
2725
+ bits ^= BitConverter . DoubleToUInt64Bits ( - 0.0 ) ;
2726
+ y = BitConverter . UInt64BitsToDouble ( bits ) ;
2727
+
2728
+ AssertExtensions . Equal ( expectedResult , Math . MinMagnitude ( x , y ) , 0.0 ) ;
2729
+ }
2592
2730
}
2593
2731
2594
2732
[ Theory ]
@@ -2657,7 +2795,7 @@ public static void MinMagnitude(double x, double y, double expectedResult)
2657
2795
[ InlineData ( 9.267056966972586 , 2 , 37.06822786789034 , CrossPlatformMachineEpsilon * 100 ) ]
2658
2796
[ InlineData ( 0.5617597462207241 , 5 , 17.97631187906317 , CrossPlatformMachineEpsilon * 100 ) ]
2659
2797
[ InlineData ( 0.7741522965913037 , 6 , 49.545746981843436 , CrossPlatformMachineEpsilon * 100 ) ]
2660
- [ InlineData ( - 0.6787637026394024 , 7 , - 86.88175393784351 , CrossPlatformMachineEpsilon * 100 ) ]
2798
+ [ InlineData ( - 0.6787637026394024 , 7 , - 86.88175393784351 , CrossPlatformMachineEpsilon * 100 ) ]
2661
2799
[ InlineData ( - 6.531673581913484 , 1 , - 13.063347163826968 , CrossPlatformMachineEpsilon * 100 ) ]
2662
2800
[ InlineData ( 9.267056966972586 , 2 , 37.06822786789034 , CrossPlatformMachineEpsilon * 100 ) ]
2663
2801
[ InlineData ( 0.5617597462207241 , 5 , 17.97631187906317 , CrossPlatformMachineEpsilon * 100 ) ]
0 commit comments