Created: Sun Feb 3 09:39:23 CET 2019

Last modiﬁed: Sun Feb 3 23:50:31 CET 2019

*I’m learning about ﬂoating-point computations.*

I wrote both μ and ν without accelerators for the sake of simplicity.

```
(define (μ k a)
(define (helper a n)
(if (null? a)
0
(+ (/ (car a) (expt k n))
(helper (cdr a) (+ n 1)))))
(helper a 1))
```

In case you’ve ever wondered how to encode `1/10`

in binary.

```
(μ 2 '(0 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 1))
; => 0.10000000149011612
```

Regarding **IEEE 754** standard for representing ﬂoating point numbers,
we’d have an exponent of `-4`

, encoded as `123`

, and the mantissa `0.6`

.

```
(define (ν k a)
(define (helper a n)
(if (null? a)
0
(+ (* (car a) (expt k n))
(helper (cdr a) (- n 1)))))
(helper a (- (length a) 1)))
```

ν does the same as μ with naturals instead of positive real numbers.
For example, `(ν 2 '(1 1 0)) => 6`

, `(ν 10 '(1 1 0)) => 110`

and
`(ν 16 '(15 15)) => 255`

.

We can use those two functions to evaluate any encoded fp to a numerical constant.

We can test this with the number 0.085, which can be encoded as follows.

```
Sign Exponent Mantissa
0 123 (-4) 0.36
0 01111011 01011100001010001111011
```

We’ll just read the steps in reverse order.

- apply
`μ 2`

to the mantissa and add`1`

, save as`a`

- substract
`127`

to the exponent, save as`b`

- calculate
`2`

, save as^{b}`c`

- multiply both
`a`

and`c`

```
(let ((exponent '(0 1 1 1 1 0 1 1))
(mantissa '(0 1 0 1 1 1 0 0 0 0 1 0 1 0 0 0 1 1 1 1 0 1 1)))
(exact->inexact
(* (expt 2 (- (ν 2 exponent) 127))
(+ 1 (μ 2 mantissa)))))
; => 0.08500000089406967
```

Of course, you’d have to multiply the whole result by `-1`

had the *sign
bit* been set.

The same process can be applied with the double precision form using a
bias of
1023
instead of `127`

.

```
(let ((exponent '(0 1 1 1 1 1 1 1 0 1 1))
(mantissa '(1 0 0 1 1 0 0 1 1 0 0 1 1
0 0 1 1 0 0 1 1 0 0 1 1 0
0 1 1 0 0 1 1 0 0 1 1 0 0
1 1 0 0 1 1 0 0 1 1 0 1 0)))
(exact->inexact
(* (expt 2 (- (ν 2 exponent) 1023))
(+ 1 (μ 2 mantissa)))))
; => 0.1
```

**Rounding errors**

*How come* `0.1`

*has no exact representation ?*

The number we approximate `0.1`

with is of the form, `x/y`

where `y`

is a
power of two.

We have 10^{-1} = xy^{-1}. It follows that y = 10x
(*cross product*) ≡ 2·2·…·2 = 10x. Because this equation has no
solution whatsoever, we know that we can never represent 0.1 in a base-2
system. Had the denominator been a power of two, we would have got
ourselves an exact representation.