18
18
import com .google .common .collect .ImmutableSet ;
19
19
import com .google .common .collect .Lists ;
20
20
import java .math .BigDecimal ;
21
+ import java .math .BigInteger ;
21
22
import java .util .List ;
22
23
import java .util .Random ;
23
24
import org .junit .Before ;
@@ -830,12 +831,16 @@ private List<Float> getListOfFloats() {
830
831
Float .MAX_VALUE ,
831
832
Float .POSITIVE_INFINITY ,
832
833
Float .NEGATIVE_INFINITY ,
833
- 0.0f // , -0.0f // MathSat5 fails for NEGATIVE_ZERO
834
- );
834
+ 0.0f , // , -0.0f // MathSat5 fails for NEGATIVE_ZERO
835
+ 1f ,
836
+ -1f ,
837
+ 2f ,
838
+ -2f );
835
839
836
840
for (int i = 1 ; i < 20 ; i ++) {
837
841
for (int j = 1 ; j < 20 ; j ++) {
838
842
flts .add ((float ) (i * Math .pow (10 , j )));
843
+ flts .add ((float ) (-i * Math .pow (10 , j )));
839
844
}
840
845
}
841
846
@@ -859,12 +864,16 @@ private List<Double> getListOfDoubles() {
859
864
Double .MAX_VALUE ,
860
865
Double .POSITIVE_INFINITY ,
861
866
Double .NEGATIVE_INFINITY ,
862
- 0.0 // , -0.0 // MathSat5 fails for NEGATIVE_ZERO
863
- );
867
+ 0.0 , // , -0.0 // MathSat5 fails for NEGATIVE_ZERO
868
+ 1d ,
869
+ -1d ,
870
+ 2d ,
871
+ -2d );
864
872
865
873
for (int i = 1 ; i < 20 ; i ++) {
866
874
for (int j = 1 ; j < 20 ; j ++) {
867
875
dbls .add (i * Math .pow (10 , j ));
876
+ dbls .add (-i * Math .pow (10 , j ));
868
877
}
869
878
}
870
879
@@ -1003,4 +1012,34 @@ public void failOnInvalidString() {
1003
1012
fpmgr .makeNumber ("a" , singlePrecType );
1004
1013
assert_ ().fail ();
1005
1014
}
1015
+
1016
+ @ Test
1017
+ public void fpFrom32BitPattern () throws SolverException , InterruptedException {
1018
+ for (float f : getListOfFloats ()) {
1019
+ int bits = Float .floatToRawIntBits (f );
1020
+ int exponent = (bits >>> 23 ) & 0xFF ;
1021
+ int mantissa = bits & 0x7FFFFF ;
1022
+ boolean sign = bits < 0 ; // equal to: (bits >>> 31) & 0x1
1023
+ final FloatingPointFormula fpFromBv =
1024
+ fpmgr .makeNumber (
1025
+ BigInteger .valueOf (exponent ), BigInteger .valueOf (mantissa ), sign , singlePrecType );
1026
+ final FloatingPointFormula fp = fpmgr .makeNumber (f , singlePrecType );
1027
+ assertThatFormula (fpmgr .assignment (fpFromBv , fp )).isTautological ();
1028
+ }
1029
+ }
1030
+
1031
+ @ Test
1032
+ public void fpFrom64BitPattern () throws SolverException , InterruptedException {
1033
+ for (double d : getListOfDoubles ()) {
1034
+ long bits = Double .doubleToRawLongBits (d );
1035
+ long exponent = (bits >>> 52 ) & 0x7FF ;
1036
+ long mantissa = bits & 0xFFFFFFFFFFFFFL ;
1037
+ boolean sign = bits < 0 ; // equal to: (doubleBits >>> 63) & 1;
1038
+ final FloatingPointFormula fpFromBv =
1039
+ fpmgr .makeNumber (
1040
+ BigInteger .valueOf (exponent ), BigInteger .valueOf (mantissa ), sign , doublePrecType );
1041
+ final FloatingPointFormula fp = fpmgr .makeNumber (d , doublePrecType );
1042
+ assertThatFormula (fpmgr .assignment (fpFromBv , fp )).isTautological ();
1043
+ }
1044
+ }
1006
1045
}
0 commit comments