Exemple #1
0
    def readFloat64(self):
        """
        Read 64-bits from the stream as a float value encoded according to IEEE 754 binary64.

        :returns: Read float value.
        :raises PythonRuntimeException: If the reading goes behind the stream.
        """

        return convertUInt64ToFloat(self.readBits(64))
Exemple #2
0
    def testConvertFloatToUInt16(self):
        # plus zero
        float16ValuePlusZero = self._createFloat16Value(0, 0, 0)  # +0.0
        self.assertEqual(float16ValuePlusZero, convertFloatToUInt16(0.0))

        # minus zero
        float16ValueMinusZero = self._createFloat16Value(1, 0, 0)  # -0.0
        self.assertEqual(float16ValueMinusZero, convertFloatToUInt16(-0.0))

        # plus infinity
        float64ValuePlusInfinity = self._createFloat64Value(0, 0x7FF,
                                                            0)  # +INF
        float16ValuePlusInfinity = self._createFloat16Value(0, 0x1F, 0)  # +INF
        convertedFloat = convertUInt64ToFloat(float64ValuePlusInfinity)
        self.assertEqual(float16ValuePlusInfinity,
                         convertFloatToUInt16(convertedFloat))

        # minus infinity
        float64ValueMinusInfinity = self._createFloat64Value(1, 0x7FF,
                                                             0)  # -INF
        float16ValueMinusInfinity = self._createFloat16Value(1, 0x1F,
                                                             0)  # -INF
        convertedFloat = convertUInt64ToFloat(float64ValueMinusInfinity)
        self.assertEqual(float16ValueMinusInfinity,
                         convertFloatToUInt16(convertedFloat))

        # quiet NaN
        float64ValueQuietNan = self._createFloat64Value(
            0, 0x7FF, 0xFFC0000000000)  # +NaN
        float16ValueQuietNan = self._createFloat16Value(0, 0x1F, 0x3FF)  # +NaN
        convertedFloat = convertUInt64ToFloat(float64ValueQuietNan)
        self.assertEqual(float16ValueQuietNan,
                         convertFloatToUInt16(convertedFloat))

        # signaling NaN
        float64ValueSignalingNan = self._createFloat64Value(
            1, 0x7FF, 0xFFC0000000000)  # -NaN
        float16ValueSignalingNan = self._createFloat16Value(1, 0x1F,
                                                            0x3FF)  # -NaN
        convertedFloat = convertUInt64ToFloat(float64ValueSignalingNan)
        self.assertEqual(float16ValueSignalingNan,
                         convertFloatToUInt16(convertedFloat))

        # normal numbers
        float16ValueOne = self._createFloat16Value(0, 15, 0)  # 1.0
        self.assertEqual(float16ValueOne, convertFloatToUInt16(1.0))

        float64ValueOnePlus = self._createFloat64Value(
            0, 0x3FF, 0x40000000000)  # 1.0 + 2^-10
        float16ValueOnePlus = self._createFloat16Value(0, 15,
                                                       0x01)  # 1.0 + 2^-10
        convertedFloat = convertUInt64ToFloat(float64ValueOnePlus)
        self.assertEqual(float16ValueOnePlus,
                         convertFloatToUInt16(convertedFloat))

        float16ValueMax = self._createFloat16Value(
            0, 30, 0x3FF)  # 2^15 (1 + 2^-1 + ... + 2^-10)
        self.assertEqual(float16ValueMax, convertFloatToUInt16(65504.0))

        # normal numbers converted to zero
        float64ValueUnderflow = self._createFloat64Value(0, 998, 0)  # 2^-25
        convertedFloat = convertUInt64ToFloat(float64ValueUnderflow)
        self.assertEqual(float16ValuePlusZero,
                         convertFloatToUInt16(convertedFloat))

        # normal numbers converted to subnormal numbers
        float64ValueMinUnderflow = self._createFloat64Value(
            0, 999, 1)  # 2^-24 (1 + 2^-52)
        float16ValueMinSubnormal = self._createFloat16Value(0, 0, 1)  # 2^-24
        convertedFloat = convertUInt64ToFloat(float64ValueMinUnderflow)
        self.assertEqual(float16ValueMinSubnormal,
                         convertFloatToUInt16(convertedFloat))

        # normal numbers converted to subnormal numbers with rounding
        float64ValueMinUnderflowRounding = self._createFloat64Value(
            0, 1000, 0x4000000000000)  # 2^-23 (1 + 2^-2)
        float16ValueMinSubnormalRounding = self._createFloat16Value(
            0, 0, 0x3)  # 2^-14 (2^-9 + 2^-10)
        convertedFloat = convertUInt64ToFloat(float64ValueMinUnderflowRounding)
        self.assertEqual(float16ValueMinSubnormalRounding,
                         convertFloatToUInt16(convertedFloat))

        # normal numbers converted to infinity
        float64ValueOverflow = self._createFloat64Value(0, 1040, 0)  # 2^17
        convertedFloat = convertUInt64ToFloat(float64ValueOverflow)
        self.assertEqual(float16ValuePlusInfinity,
                         convertFloatToUInt16(convertedFloat))

        # normal numbers converted with rounding
        float64ValueRounding = self._createFloat64Value(
            0, 1023, 0x8040000000000)  # 1 + 2^-1 + 2^-11
        float16ValueRounding = self._createFloat16Value(
            0, 15, 0x201)  # 1 + 2^-1 + 2^-10
        convertedFloat = convertUInt64ToFloat(float64ValueRounding)
        self.assertEqual(float16ValueRounding,
                         convertFloatToUInt16(convertedFloat))

        # subnormal numbers
        float64ValueMin32Subnormal = self._createFloat64Value(
            0, 874, 0)  # 2^-126 (2^-23)
        convertedFloat = convertUInt64ToFloat(float64ValueMin32Subnormal)
        self.assertEqual(float16ValuePlusZero,
                         convertFloatToUInt16(convertedFloat))

        float64ValueMax32Subnormal = self._createFloat64Value(
            0, 896, 0xFFFFFC0000000)  # 2^-126 (2^-1 + ... + 2^-23)
        convertedFloat = convertUInt64ToFloat(float64ValueMax32Subnormal)
        self.assertEqual(float16ValuePlusZero,
                         convertFloatToUInt16(convertedFloat))
Exemple #3
0
 def testConvertUInt64ToFloat(self):
     for dataRow in self.TEST_FLOAT64_DATA:
         float64Value = self._createFloat64Value(dataRow[0], dataRow[1],
                                                 dataRow[2])
         convertedFloat = convertUInt64ToFloat(float64Value)
         self.assertEqual(dataRow[3], convertedFloat)