GCC NaN's
-
Warum bekomme ich bei folgendem Programm gerade diese Ausgabe?
#include <climits> // Für CHAR_BIT #include <limits> // Für std::numeric_limits #include <iostream> // Für die #include <bitset> // Ausgabe int main() { typedef float float_type; typedef unsigned int int_type; // Um strict-aliasing-Regeln nicht zu verletzen union FloatType { float_type data; int_type bits; FloatType (float_type d): data(d) {} }; typedef std::bitset<sizeof(float_type) * CHAR_BIT> float_bitset; FloatType signalingNaN = std::numeric_limits<float_type>::signaling_NaN(); FloatType quietNaN = std::numeric_limits<float_type>::quiet_NaN(); constexpr std::size_t bitCount = sizeof(float_type) * CHAR_BIT; constexpr std::size_t fractionBitCount = std::numeric_limits<float_type>::digits - 1; constexpr std::size_t exponentBitCount = bitCount - fractionBitCount - 1; unsigned long long fractionMask = ~static_cast<int_type>(0) >> (exponentBitCount + 1); std::cout << "Mantisse: " << "\n"; std::cout << " Number of bits: " << fractionBitCount << "\n"; std::cout << " Signaling NaN: " << std::bitset<fractionBitCount>(signalingNaN.bits & fractionMask) << "\n"; std::cout << " Quiet NaN: " << std::bitset<fractionBitCount>(quietNaN.bits & fractionMask) << "\n"; return 0; }Ausgabe:
Mantisse: Number of bits: 23 Signaling NaN: 11000000000000000000000 Quiet NaN: 10000000000000000000000Eigentlich sollte doch bei Signaling NaN 10000000000... stehen, und bei Quiet NaN 1xxxxxxxxx (http://de.wikipedia.org/wiki/NaN) ? Oder ist der GCC nicht IEEE-konform (mit -fsignaling-nans -frounding-math kompiliert.).
Oder habe ich einen Denkfehler?
-
http://en.wikipedia.org/wiki/NaN#Encoding
The 2008 revision of the IEEE 754 standard (IEEE 754-2008) makes formal recommendations for the interpretation of the signaled/quiet bit.
For binary formats, the standard follows the interpretation as an 'is_quiet' flag. I.e. the signaled/quiet bit is non-zero if the NaN is quiet, and zero if the NaN is signaling. The standard still allows implementations to use several bits to distinguish signaling and quiet NaNs.
Die englische Wikipedia sagt, dass das Mantisse-Bit mit der höchsten Wertigkeit laut IEEE 754-2008 eine 1 sein sollte, wenn es sich um eine "quiet NAN" handelt. Für eine "signaling NAN" sollte dieses Bit eine 0 sein. Demzufolge hält sich der gcc an die Spezifikation.
-
Wieso? Das höchste mantissenbit ist doch beides mal 1, oder?
-
pyhax schrieb:
Wieso? Das höchste mantissenbit ist doch beides mal 1, oder?
Ja, hab gar nicht auf deine Ausgabe geachtet. Ich habe das Programm bei mir unter Fedora 15 mit gcc 4.6.1 kompiliert und dort bekomme ich folgende Ausgabe:
Mantisse:
Number of bits: 23
Signaling NaN: 01000000000000000000000
Quiet NaN: 10000000000000000000000Bleibt die Frage, welche gcc Version du verwendest, bei mir funktioniert es ja offensichtlich richtig.
-
sqrt(2) schrieb:
pyhax schrieb:
Wieso? Das höchste mantissenbit ist doch beides mal 1, oder?
Ja, hab gar nicht auf deine Ausgabe geachtet. Ich habe das Programm bei mir unter Fedora 15 mit gcc 4.6.1 kompiliert und dort bekomme ich folgende Ausgabe:
Mantisse:
Number of bits: 23
Signaling NaN: 01000000000000000000000
Quiet NaN: 10000000000000000000000Bleibt die Frage, welche gcc Version du verwendest, bei mir funktioniert es ja offensichtlich richtig.
Ah, wenn ich -O1 einschalte, funktioniert es bei mir auch.
Mantisse: Number of bits: 23 Signaling NaN: 01000000000000000000000 Quiet NaN: 10000000000000000000000gcc (GCC) 4.6.2 20120120