You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
_ 1111111110000000000000000000001 = ffc0 0001 = qNaN (on x86 and ARM processors)
203
+
_ 1111111100000000000000000000001 = ff80 0001 = sNaN (on x86 and ARM processors)
204
+
```
205
+
206
+
```cpp
207
+
(8 bits) (23 bits)
208
+
sign exponent fraction
209
+
0000 ...00 = -0
210
+
1000 ...00 = +0
211
+
0 FF 0 ...00 = +infinity
212
+
1 FF 0 ...00 = -infinity
213
+
_ FF 1 ...01 = qNaN
214
+
_ FF 0 ...01 = sNaN
215
+
```
216
+
217
+
Encodings of qNaN and sNaN are not specified in IEEE 754 and implemented
218
+
differently on different processors. Luckily, both x86 and ARM family use the
219
+
"most significant bit of fraction" to indicate quiteness.
220
+
221
+
### More on NaN
222
+
223
+
If we look carefully into the IEEE 754-2008 spec, in the _page35, 6.2.1_, it
224
+
actually defined anything with exponent `FF` and not infinity (i.e. with
225
+
trailing bit of fraction being `0`), a NaN!
226
+
227
+
> All binary NaN bit strings have all the bits of the biased exponent field E set to 1 (see 3.4). A quiet NaN bit string should be encoded with the first bit (d1) of the trailing significand field T being 1. A signaling NaN bit string should be encoded with the first bit of the trailing significand field being 0.
228
+
229
+
That means, we actually have `2 ** 24 - 2` of NaNs in a 32-bits floats!
230
+
The `24` came from the `1` sign bit plus `23` fractions and the `2` came from
231
+
the `+/- inf`.
232
+
233
+
The contingious 22 bits inside the fraction looks quite a waste, and there
234
+
would be 51 bits of them in the `double`! We will see how to made them useful
235
+
in later episodes (spoiler: they are known as _NaN payload_).
236
+
237
+
It's also worth nothing that It's weird to use the MSB instead of sign bit for
238
+
NaN quiteness/signalness:
239
+
240
+
> It seems strange to me that the bit which signifies whether or not the NaN is signaling is the top bit of the mantissa rather than the sign bit; perhaps something about how floating point pipelines are implemented makes it less natural to use the sign bit to decide whether or not to raise a signal.
0 commit comments