# atom_to_floatNN

Definition: | sequence s = atom_to_float32(atom a1) -- or -- sequence s = atom_to_float64(atom a1) -- or -- sequence s = atom_to_float80(atom a1)
Description: | Convert an atom to a sequence (in fact a raw binary string) of 4/8/10 single-byte values. These bytes contain the representation of an IEEE floating-point number in 32/64/80-bit format.
pwa/p2js: | Supported, apart from atom_to_float80() - while JavaScript has DataView.prototype.setFloat32 and DataView.prototype.setFloat64, there is no DataView.prototype.setFloat80, as yet, and no mention of it in the ECMAScript specs. To be fair, there would be little point, and in fact there is very little point in 32-bit Phix supporting atom_to_float80(), since at least 11 bits plus some of the exponent are always missing/0, apart from being able to poke or write something almost right and in the correct format for some C api or similar, and nothing remotely like that applies to JavaScript running in a browser, at least nothing that I can think of. Note that while on desktop/Phix the result is technically a string, whereas under pwa/p2js it is explicitly a sequence.
Comments: | There are three very similar routines, atom_to_float32, atom_to_float64, and atom_to_float80, which deal with 32-bit/4-byte, 64-bit/8-byte, and 80-bit/10-byte conversions respectively. Note that while the result is technically a string, it is not suitable for display; instead it is intended for poking to memory or writing to a file. On 32-bit all atoms are stored as 64-bit IEEE floating-point numbers, so you can convert any atom to 64-bit format without losing any precision. On 64-bit all atoms are stored as 80-bit IEEE floating-point numbers, so you can convert any atom to 80-bit format without losing any precision. Converting an atom to a smaller format can lose precision or result in one of the special values inf or -inf (infinity or -infinity). It is also possible that upscaling can introduce apparent discrepancies, for instance the value 0.1 cannot be held exactly, and hence a 64-bit version of 0.1 upscaled to 80-bits will not match a 0.1 value that has been 80-bits since inception. Neither is exact but the latter will contain a futher eleven bits of the infinitely recurring bit pattern that the former does not, at least that is on 64-bit. Integer values will also be converted to floating-point format.
Example: |
    fn = open("numbers.dat", "wb")
    puts(fn, atom_to_float64(157.82)) -- write 8 bytes to a file
Implementation: | See builtins\VM\pFloatN.e (an autoinclude), or pwa\p2js.js, for details of the actual implementation.
See Also: | floatNN_to_atom