|
1 | 1 | # FixedPoint
|
2 | 2 |
|
3 |
| -This library exports a 32-bit fixed-point type `Fixed32{f}`. |
| 3 | +This library exports fixed-point number types. |
| 4 | +A [fixed-point number][wikipedia] represents a fractional, or non-integral, number. |
| 5 | +In contrast with the more widely known floating-point numbers, fixed-point |
| 6 | +numbers have a fixed number of digits (bits) after the decimal (radix) point. |
| 7 | +They are effectively integers scaled by a constant factor. |
| 8 | + |
| 9 | +Fixed-point numbers can be used to perform arithmetic. Another practical |
| 10 | +application is to implicitly rescale integers without modifying the |
| 11 | +underlying representation. |
| 12 | + |
| 13 | +This library exports two categories of fixed-point types. Fixed-point types are |
| 14 | +used like any other number: they can be added, multiplied, raised to a power, |
| 15 | +etc. In many cases these operations result in conversion to floating-point types. |
| 16 | + |
| 17 | +## Fixed32 (signed fixed-point numbers) |
| 18 | + |
| 19 | +For signed integers, there is a 32-bit fixed-point type `Fixed32{f}`. |
4 | 20 | The parameter `f` is the number of fraction bits. There is also an abstract subtype of
|
5 | 21 | `Real` called `Fixed`.
|
6 | 22 |
|
7 | 23 | To use it, convert numbers to a `Fixed32` type, or call `Fixed32(x)`, which will default
|
8 |
| -to constructing a `Fixed32{16}`. Then use ordinary arithmetic. |
| 24 | +to constructing a `Fixed32{16}`. |
| 25 | + |
| 26 | +## Ufixed (unsigned fixed-point numbers) |
| 27 | + |
| 28 | +For unsigned integers, there is a family of subtypes of the abstract `Ufixed` type. |
| 29 | +These types, built with `f` fraction bits, map the closed interval [0.0,1.0] |
| 30 | +to the span of numbers with `f` bits. |
| 31 | +For example, the `Ufixed8` type is represented internally by a `Uint8`, and makes |
| 32 | +`0x00` equivalent to `0.0` and `0xff` to `1.0`. |
| 33 | +The types `Ufixed10`, `Ufixed12`, `Ufixed14`, and `Ufixed16` are all based on `Uint16` |
| 34 | +and reach the value `1.0` at 10, 12, 14, and 16 bits, respectively (`0x03ff`, `0x0fff`, |
| 35 | +`0x3fff`, and `0xffff`). |
| 36 | + |
| 37 | +To construct such a number, use `convert(Ufixed12, 1.3)` or the literal syntax `0x14ccuf12`. |
| 38 | +The latter syntax means to construct a `Ufixed12` (it ends in `uf12`) from the `Uint16` value |
| 39 | +`0x14cc`. |
| 40 | + |
| 41 | +[wikipedia]: http://en.wikipedia.org/wiki/Fixed-point_arithmetic |
0 commit comments