A little mirror class for introspecting the bit-patterns of JS numbers using typed arrays.
Example:
Math.PI.introspect().mantissa; // "1001001000011111101101010100010001000010110100011000"
Math.E.introspect().exponent; // "10000000000"
(9.0).introspect().toBitString(); // "0100000000100010000000000000000000000000000000000000000000000000"
(-Math.PI).introspect().negative; // trueInteractively explore at http://dherman.github.com/float.js.
Internally, this puts the number in a single-element Float64Array typed array and gets its buffer to read the value’s raw bits:
(new Float64Array([n])).bufferThen it converts that buffer to a string of "1"s and "0"s by viewing the buffer as a Uint8Array and mapping each viewed byte to a binary string with .toString(2). Finally, it creates properties like exponent and mantissa by using [] array indexing, .substring, and .slice to extract the relevant bits.
Copyright © 2012 Dave Herman
Licensed under the MIT License.