4 ms·
> better notation for literals [...] something like 1eb-1 (for 0.5) and 1eb-2 (for 0.25) There are floating point hex literals. These can be written as 0x1p-1
by jacobolus 5y ago
> better notation for literals [...] something like 1eb-1 (for 0.5) and 1eb-2 (for 0.25)
There are floating point hex literals. These can be written as 0x1p-1 == 0.5 and 0x1p-2 == 0.25.
You can use them in C/C++, Java, Julia, Swift, ..., but they are not supported everywhere.
https://observablehq.com/@jrus/hexfloat https://observablehq.com/@jrus/hexfloat
- Aardwolf 5y agoC++ hex floats are an interesting combination of 3 numeric bases in one! the mantissa is written in base 16 the exponent is written in base 10 the exponent itself is a power of 2 (not of 16 or 2), so that's base 2 One can only wonder how that came to be. I think they chose base 10 for the exponent to allow using the 'f' suffix to denote float (as opposed to double)
- adgjlsfhk1 5y agoJulia is missing 32 bit and 16 bit hex floats unfortunately.
- simonbyrne 5y agoYou can just wrap the literal in a conversion function, eg Float32(0x1p52), which should get constant propagated at compile time.
- adgjlsfhk1 5y agoI know, it just isn't as nice to read.