DEV Community

Novatus
Novatus

Posted on Edited on

Based floating-point number in Erlang, a small intro

Assumptions:

This is a short expansion on that article. Adding little more complexity to your knowledge ! It's fine even if you dont understand it just practice the examples below and that's all you need.

Now lets say you want to write Dyadic rationals (also called binary rationals) in Erlang. The way to do it is by writing floating-point numbers. It is a type of data (data type) in Erlang or more technically saying they are "IEEE 754 binary64 (double-precision) floating-point values".

The syntax to write it is as follows:

"base # digits . digits #e exponent"

It is syntax for base-prefixed floating-point number (a "based float") with a decimal exponent.

Structure of the number

A number of this form has four parts:

  1. base: A decimal integer from 2 through 36 specifying the numeral base for the integer and fractional digit parts (for example, 2 for binary or 16 for hexadecimal).
  2. digits.digits: The integer part before . and the fractional part after ., written using digits valid for base (0-9 and letters A-Z / a-z up to base - 1).
  3. #e (or #E): The separator that introduces the exponent part.
  4. exponent: A decimal (base-10) signed or unsigned integer (for example, +6, -3, or 4). that scales the value by $\text{base}^{\text{exponent}}$.

How the Value Is Evaluated

For a number B#I.F#eE, the mathematical value before rounding to IEEE 754 binary64 is:

$$\left(I + \frac{F}{B^k}\right) \times B^E$$

where:

  • $B$ is base (in decimal),

  • $I$ is the value of the integer digits before . in base $B$,

  • $F$ is the value of the fractional digits after . in base $B$,

  • $k$ is the number of fractional digits after .,

  • $E$ is the decimal integer exponent.

An Example (16#ff.fe#e+6)

  • Base ($B$): 16 (hexadecimal).

  • Integer part (ff): $15 \times 16^1 + 15 \times 16^0 = 255$.

  • Fractional part (.fe, $k = 2$ hex digits):

    $$\frac{15 \times 16^1 + 14 \times 16^0}{16^2} = \frac{254}{256} = 0.9921875$$

  • Significand before scaling: $255 + \frac{254}{256} = 255.9921875$.

  • Exponent scaling (#e+6): Multiplies by $16^{+6}$ (decimal exponent 6), yielding:

    $$255.9921875 \times 16^6 = 4{,}294{,}950{,}912.0$$

Few more examples

Number Base (B) Integer Part (I) Fractional Part (F, k digits) Decimal Exponent (E) Formula: (I+F/Bk)×BE Resulting Erlang float Value
16#ff.fe#e+6 16 ff ($255$) fe ($254$, $k=2$) +6 ($6$) $(255 + 254 / 16^2) \times 16^6$ 4.294950912e9 ($4{,}294{,}950{,}912.0$)
2#1.1#e+0 2 1 ($1$) 1 ($1$, $k=1$) +0 ($0$) $(1 + 1 / 2^1) \times 2^0$ 1.5
2#101.01#e+2 2 101 ($5$) 01 ($1$, $k=2$) +2 ($2$) $(5 + 1 / 2^2) \times 2^2$ 21.0 ($5.25 \times 4$)
16#1.8#e-1 16 1 ($1$) 8 ($8$, $k=1$) -1 ($-1$) $(1 + 8 / 16^1) \times 16^{-1}$ 0.09375 ($1.5 / 16$)
8#10.4#e+1 8 10 ($8$) 4 ($4$, $k=1$) +1 ($1$) $(8 + 4 / 8^1) \times 8^1$ 68.0 ($8.5 \times 8$)
36#a.0#e+0 36 a ($10$) 0 ($0$, $k=1$) +0 ($0$) $(10 + 0 / 36^1) \times 36^0$ 10.0

Practice in your terminal

1> 16#ff.fe#e+6. 
4294836224.0 
2> 2#101.01#e+2. 
21.0 
3>
Enter fullscreen mode Exit fullscreen mode

Top comments (0)