1818import com .google .common .collect .ImmutableSet ;
1919import com .google .common .collect .Lists ;
2020import java .math .BigDecimal ;
21+ import java .math .BigInteger ;
2122import java .util .List ;
2223import java .util .Random ;
2324import org .junit .Before ;
@@ -830,12 +831,16 @@ private List<Float> getListOfFloats() {
830831 Float .MAX_VALUE ,
831832 Float .POSITIVE_INFINITY ,
832833 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 );
835839
836840 for (int i = 1 ; i < 20 ; i ++) {
837841 for (int j = 1 ; j < 20 ; j ++) {
838842 flts .add ((float ) (i * Math .pow (10 , j )));
843+ flts .add ((float ) (-i * Math .pow (10 , j )));
839844 }
840845 }
841846
@@ -859,12 +864,16 @@ private List<Double> getListOfDoubles() {
859864 Double .MAX_VALUE ,
860865 Double .POSITIVE_INFINITY ,
861866 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 );
864872
865873 for (int i = 1 ; i < 20 ; i ++) {
866874 for (int j = 1 ; j < 20 ; j ++) {
867875 dbls .add (i * Math .pow (10 , j ));
876+ dbls .add (-i * Math .pow (10 , j ));
868877 }
869878 }
870879
@@ -1003,4 +1012,34 @@ public void failOnInvalidString() {
10031012 fpmgr .makeNumber ("a" , singlePrecType );
10041013 assert_ ().fail ();
10051014 }
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+ }
10061045}
0 commit comments