5
5
[ ![ codecov.io] ( http://codecov.io/github/JuliaMath/FixedPointNumbers.jl/coverage.svg?branch=master )] ( http://codecov.io/github/JuliaMath/FixedPointNumbers.jl?branch=master )
6
6
7
7
This library implements fixed-point number types. A
8
- [ fixed-point number] [ wikipedia ] represents a fractional, or
8
+ [ fixed-point number] represents a fractional, or
9
9
non-integral, number. In contrast with the more widely known
10
10
floating-point numbers, with fixed-point numbers the decimal point
11
11
doesn't "float": fixed-point numbers are effectively integers that are
@@ -37,7 +37,7 @@ except the sign bit) for the fractional part. The value of the number
37
37
is interpreted as if the integer representation has been divided by
38
38
` 2^f ` . Consequently, ` Fixed{Int8,7} ` numbers ` x ` satisfy
39
39
40
- ```
40
+ ``` julia
41
41
- 1.0 = - 128 / 128 ≤ x ≤ 127 / 128 ≈ 0.992 .
42
42
```
43
43
@@ -64,4 +64,82 @@ More generally, an arbitrary number of bits from any of the standard unsigned
64
64
integer widths can be used for the fractional part. For example:
65
65
` Normed{UInt32,16} ` , ` Normed{UInt64,3} ` , ` Normed{UInt128,7} ` .
66
66
67
- [ wikipedia ] : http://en.wikipedia.org/wiki/Fixed-point_arithmetic
67
+ # Computation with Fixed and Normed numbers
68
+
69
+ You can perform mathematical operations with ` FixedPoint ` numbers, but keep in mind
70
+ that they are vulnerable to both [ rounding] and [ overflow] . For example:
71
+
72
+ ``` julia
73
+ julia> x = N0f8 (0.8 )
74
+ 0.8 N0f8
75
+
76
+ julia> float (x) + x
77
+ 1.6f0
78
+
79
+ julia> x + x
80
+ 0.596 N0f8
81
+ ```
82
+
83
+ This is a consequence of the rules that govern overflow in integer arithmetic:
84
+
85
+ ``` julia
86
+ julia> y = reinterpret (x) # `reinterpret(x::FixedPoint)` reinterprets as the underlying "raw" type
87
+ 0xcc
88
+
89
+ julia> reinterpret (N0f8, y + y) # add two UInt8s and then reinterpret as N0f8
90
+ 0.596 N0f8
91
+ ```
92
+
93
+ Similarly,
94
+
95
+ ``` julia
96
+ julia> x = eps (N0f8) # smallest nonzero `N0f8` number
97
+ 0.004 N0f8
98
+
99
+ julia> x* x
100
+ 0.0 N0f8
101
+ ```
102
+
103
+ which is rounding-induced [ underflow] . Finally,
104
+
105
+ ``` julia
106
+ julia> x = N4f12 (15 )
107
+ 15.0 N4f12
108
+
109
+ julia> x* x
110
+ ERROR: ArgumentError: Normed{UInt16,12 } is a 16 - bit type representing 65536 values from 0.0 to 16.0037 ; cannot represent 225.0
111
+ Stacktrace:
112
+ [1 ] throw_converterror (:: Type{Normed{UInt16,12}} , :: Float32 ) at / home/ tim/ . julia/ dev/ FixedPointNumbers/ src/ FixedPointNumbers. jl: 251
113
+ [2 ] _convert at / home/ tim/ . julia/ dev/ FixedPointNumbers/ src/ normed. jl: 77 [inlined]
114
+ [3 ] FixedPoint at / home/ tim/ . julia/ dev/ FixedPointNumbers/ src/ FixedPointNumbers. jl: 51 [inlined]
115
+ [4 ] convert at ./ number. jl: 7 [inlined]
116
+ [5 ] * (:: Normed{UInt16,12} , :: Normed{UInt16,12} ) at / home/ tim/ . julia/ dev/ FixedPointNumbers/ src/ normed. jl: 254
117
+ [6 ] top- level scope at REPL[16 ]: 1
118
+ ```
119
+
120
+ In some circumstances, it may make most sense to think of ` FixedPoint ` numbers as * storage types*
121
+ rather than computational types. You can call ` float(x) ` to convert ` x ` to a floating-point equivalent that is reasonably
122
+ safe for computation; in the type domain, ` floattype(T::Type) ` returns the corresponding type.
123
+ Note that in some cases ` floattype(T) ` differs from ` float ` 's behavior on the corresponding "raw" type:
124
+
125
+ ``` julia
126
+ julia> float (UInt8)
127
+ Float64
128
+
129
+ julia> floattype (N0f8)
130
+ Float32
131
+ ```
132
+
133
+ Because of the role of FixedPointNumbers in domains such as image-processing, this package tries to limit the expansion of the
134
+ number of bits needed to store results.
135
+
136
+
137
+ ## Contributors
138
+
139
+ Please see the [ CONTRIBUTORS.md] ( CONTRIBUTORS.md ) for information about improving this package.
140
+
141
+
142
+ [ fixed-point number ] : http://en.wikipedia.org/wiki/Fixed-point_arithmetic
143
+ [ overflow ] : https://en.wikipedia.org/wiki/Integer_overflow
144
+ [ rounding ] : https://en.wikipedia.org/wiki/Round-off_error
145
+ [ underflow ] : https://en.wikipedia.org/wiki/Arithmetic_underflow
0 commit comments