Skip to content

Instantly share code, notes, and snippets.

@robertsdionne
Last active July 30, 2022 06:08
Show Gist options
  • Save robertsdionne/eaf924293b66a24bfbb6a174576fa0c3 to your computer and use it in GitHub Desktop.
Save robertsdionne/eaf924293b66a24bfbb6a174576fa0c3 to your computer and use it in GitHub Desktop.
base32hex float literals for convenient representation of standard binary prefixes like kibi-, mebi-, gibi-, tebi-, etc.
As "hexadecimal" derives from "hexa" for 6 and "decimal" for 10,
"duotrigesimal" can be drived for "2" and "30" similarly, for base32. See: https://math.stackexchange.com/a/4263272
Base32hex is a character set that extends the hexadecimal digits with additional letters of the alphabet until
it reaches 32 values, [0-9a-vA-V]. See: https://en.wikipedia.org/wiki/Base32#base32hex
In the English alphabet, "W" is the next letter after the base32hex alphabet, so it would work as the delimiter for
the base prefix 0w (analogous to 0x for hexadecimal), and also the top of the letter "W" has 3 points, and the
bottom has 2 points, signifying 32.
Two digits of base32 can represent 1024 values, so base32 can conveniently represent standard binary prefixes
for information units, such as kibi-, mebi-, gibi-, tebi-bytes, etc.:
* 0w1_00 is 1 KiB
* 0w1_00_00 is 1 MiB
* 0w1_00_00_00 is 1 GiB
* 0w1_00_00_00_00 is 1 TiB
...
See: https://en.wikipedia.org/wiki/ISO/IEC_80000#Part_13:_Information_science_and_technology
Here is the syntax for base32 floating point literals, based on hexadecimal floating point literals defined
here: https://go.dev/ref/spec#Floating-point_literals
duotrigesimal_float_lit = "0" ( "w" | "W" ) duotrigesimal_mantissa duotrigesimal_exponent .
duotrigesimal_mantissa = [ "_" ] duotrigesimal_digits "." [ duotrigesimal_digits ] |
[ "_" ] duotrigesimal_digits |
"." duotrigesimal_digits .
duotrigesimal_exponent = ( "x" | "X") [ "+" | "-" ] decimal_digits
duotrigesimal_digits = duotrigesimal_digit { [ "_" ] duotrigesimal_digit } .
duotrigesimal_digit = "0" ... "9" | "A" ... "V" | "a" ... "v"
decimal_digits = decimal_digit { [ "_" ] decimal_digit } .
decimal_digit = "0" ... "9"
For example, the positive and negative values of Phi, (1±sqrt(5))/2 are:
* 0w1.jornjebv9ax0
* -0w1.u6tsrivqagx-1
Similarly, 1 KiB could be represented as any of these:
* 0w1_00_00x-10
* 0w1_00x0
* 0w1.00x10
* 0w0.00_1x20
Note, the exponents represent powers of two.
Note, I realized the 'p/P' exponent delimiter collides with the base32hex alphabet,
so I've replaced them with 'x/X' for exponent.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment