Estou analizando o Protocol Buffer do Google
(http://code.google.com/p/protobuf/) e vi um código aparentemente
bizarro:
<code>
//
// ver http://www.google.com/codesearch/p?hl=en#WTeibokF6gE/trunk/src/google/protobuf/stubs/strutil.cc&q=strutil.cc&l=57
//
inline bool IsNaN(double value) {
// NaN is never equal to anything, even itself.
return value != value;
}
</code>
Isso me parece bem paradoxal. Qual a explicação para isso?
Rodrigo Strauss
http://www.1bit.com.br
PS: como não temos o lameless filter do Slashdot, aqui vai: 2009 first post!!!
Um texto legal sobre ponto flutuante:
"
Special Values
IEEE reserves exponent field values of all 0s and all 1s to denote special values in the floating-point scheme.
Zero
As mentioned above, zero is not directly representable in the straight format, due to the assumption of a leading 1 (we'd need to specify a true zero mantissa to yield a value of zero). Zero is a special value denoted with an exponent field of zero and a fraction field of zero. Note that -0 and +0 are distinct values, though they both compare as equal.
Denormalized
If the exponent is all 0s, but the fraction is non-zero (else it would be interpreted as zero), then the value is a denormalized number, which does not have an assumed leading 1 before the binary point. Thus, this represents a number (-1)s × 0.f × 2-126, where s is the sign bit and f is the fraction. For double precision, denormalized numbers are of the form (-1)s × 0.f × 2-1022. From this you can interpret zero as a special type of denormalized number.
Infinity
The values +infinity and -infinity are denoted with an exponent of all 1s and a fraction of all 0s. The sign bit distinguishes between negative infinity and positive infinity. Being able to denote infinity as a specific value is useful because it allows operations to continue past overflow situations. Operations with infinite values are well defined in IEEE floating point.
Not A Number
The value NaN (Not a Number) is used to represent a value that does not represent a real number. NaN's are represented by a bit pattern with an exponent of all 1s and a non-zero fraction. There are two categories of NaN: QNaN (Quiet NaN) and SNaN (Signalling NaN).
A QNaN is a NaN with the most significant fraction bit set. QNaN's propagate freely through most arithmetic operations. These values pop out of an operation when the result is not mathematically defined.
An SNaN is a NaN with the most significant fraction bit clear. It is used to signal an exception when used in operations. SNaN's can be handy to assign to uninitialized variables to trap premature usage.
Semantically, QNaN's denote indeterminate operations, while SNaN's denote invalid operations.
"
(http://steve.hollasch.net/cgindex/coding/ieeefloat.html)
|
Virgilio Alexandre Fornazin High performance and realtime systems development
Rua Brigadeiro Vicente Faria Lima, 268 Bela Vista Leme-SP CEP 13611-485 Phone: +55 19 3571-5573 Cell: +55 19 8111-4053 +55 11 8357 1491 |
2009/1/1 Rodrigo Strauss <rod...@1bit.com.br>##############################################################################
Eu sei que existe Wikipedia, mas me parece uma boa pergunta de ano
novo. Eu sei que é primeiro de janeiro, mas todo dia é dia de escovar
bits. Ah, feliz ano novo a todos.
Estou analizando o Protocol Buffer do Google
(http://code.google.com/p/protobuf/) e vi um código aparentemente
bizarro:
<code>
//
// ver http://www.google.com/codesearch/p?hl=en#WTeibokF6gE/trunk/src/google/protobuf/stubs/strutil.cc&q=strutil.cc&l=57
//
inline bool IsNaN(double value) {
// NaN is never equal to anything, even itself.
return value != value;
}
</code>
Isso me parece bem paradoxal. Qual a explicação para isso?
Rodrigo Strauss
http://www.1bit.com.br
PS: como não temos o lameless filter do Slashdot, aqui vai: 2009 first post!!!
A explicação do kumpera é bem "esclarecedora".
Mas acho meio estranho esse codigo, parece rapido mas não deveria ser assim. Se existir um outro caso que o numero não seja iqual a ele mesmo, vai dar erro.
Mas acho que NaN não deveriam ocorrer em codigos quem estão funcionando certo :p.
Mas claro, sempre existe alguma cituação estranha :D.
Sobre usar ou não o ponto flutuante, acho que é uma questão de saber
usar a ferramenta correta para a aplicação em vista. O erro mais comum
é usar ponto flutuante para aplicações financeira$.