| 6671 | // RawType: the raw floating-point type (either float or double) |
| 6672 | template <typename RawType> |
| 6673 | class FloatingPoint { |
| 6674 | public: |
| 6675 | // Defines the unsigned integer type that has the same size as the |
| 6676 | // floating point number. |
| 6677 | typedef typename TypeWithSize<sizeof(RawType)>::UInt Bits; |
| 6678 | |
| 6679 | // Constants. |
| 6680 | |
| 6681 | // # of bits in a number. |
| 6682 | static const size_t kBitCount = 8*sizeof(RawType); |
| 6683 | |
| 6684 | // # of fraction bits in a number. |
| 6685 | static const size_t kFractionBitCount = |
| 6686 | std::numeric_limits<RawType>::digits - 1; |
| 6687 | |
| 6688 | // # of exponent bits in a number. |
| 6689 | static const size_t kExponentBitCount = kBitCount - 1 - kFractionBitCount; |
| 6690 | |
| 6691 | // The mask for the sign bit. |
| 6692 | static const Bits kSignBitMask = static_cast<Bits>(1) << (kBitCount - 1); |
| 6693 | |
| 6694 | // The mask for the fraction bits. |
| 6695 | static const Bits kFractionBitMask = |
| 6696 | ~static_cast<Bits>(0) >> (kExponentBitCount + 1); |
| 6697 | |
| 6698 | // The mask for the exponent bits. |
| 6699 | static const Bits kExponentBitMask = ~(kSignBitMask | kFractionBitMask); |
| 6700 | |
| 6701 | // How many ULP's (Units in the Last Place) we want to tolerate when |
| 6702 | // comparing two numbers. The larger the value, the more error we |
| 6703 | // allow. A 0 value means that two numbers must be exactly the same |
| 6704 | // to be considered equal. |
| 6705 | // |
| 6706 | // The maximum error of a single floating-point operation is 0.5 |
| 6707 | // units in the last place. On Intel CPU's, all floating-point |
| 6708 | // calculations are done with 80-bit precision, while double has 64 |
| 6709 | // bits. Therefore, 4 should be enough for ordinary use. |
| 6710 | // |
| 6711 | // See the following article for more details on ULP: |
| 6712 | // http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/ |
| 6713 | static const size_t kMaxUlps = 4; |
| 6714 | |
| 6715 | // Constructs a FloatingPoint from a raw floating-point number. |
| 6716 | // |
| 6717 | // On an Intel CPU, passing a non-normalized NAN (Not a Number) |
| 6718 | // around may change its bits, although the new value is guaranteed |
| 6719 | // to be also a NAN. Therefore, don't expect this constructor to |
| 6720 | // preserve the bits in x when x is a NAN. |
| 6721 | explicit FloatingPoint(const RawType& x) { u_.value_ = x; } |
| 6722 | |
| 6723 | // Static methods |
| 6724 | |
| 6725 | // Reinterprets a bit pattern as a floating-point number. |
| 6726 | // |
| 6727 | // This function is needed to test the AlmostEquals() method. |
| 6728 | static RawType ReinterpretBits(const Bits bits) { |
| 6729 | FloatingPoint fp(0); |
| 6730 | fp.u_.bits_ = bits; |
nothing calls this directly
no outgoing calls
no test coverage detected