Skip to content

Commit 021e1cf

Browse files
committed
Put information in README regarding overflow, and add CONTRIBUTING
1 parent 0c486f3 commit 021e1cf

File tree

2 files changed

+91
-3
lines changed

2 files changed

+91
-3
lines changed

CONTRIBUTORS.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Style guide
2+
3+
This outlines recommended practices for contributors to this package.
4+
5+
## Naming
6+
7+
- Type parameters: use `F` for `F <: Fixed`, `N` for `N <: Normed`,
8+
and `X` for `X <: FixedPoint`. Use `f` for the number of fractional bits.
9+
- Use `Ti` for `Ti <: Integer`, `Tf` for `Tf <: AbstractFloat`, and `Tw`
10+
for `widen`ed types.

README.md

Lines changed: 81 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
[![codecov.io](http://codecov.io/github/JuliaMath/FixedPointNumbers.jl/coverage.svg?branch=master)](http://codecov.io/github/JuliaMath/FixedPointNumbers.jl?branch=master)
66

77
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
99
non-integral, number. In contrast with the more widely known
1010
floating-point numbers, with fixed-point numbers the decimal point
1111
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
3737
is interpreted as if the integer representation has been divided by
3838
`2^f`. Consequently, `Fixed{Int8,7}` numbers `x` satisfy
3939

40-
```
40+
```julia
4141
-1.0 = -128/128 x 127/128 0.992.
4242
```
4343

@@ -64,4 +64,82 @@ More generally, an arbitrary number of bits from any of the standard unsigned
6464
integer widths can be used for the fractional part. For example:
6565
`Normed{UInt32,16}`, `Normed{UInt64,3}`, `Normed{UInt128,7}`.
6666

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.8N0f8
75+
76+
julia> float(x) + x
77+
1.6f0
78+
79+
julia> x + x
80+
0.596N0f8
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.596N0f8
91+
```
92+
93+
Similarly,
94+
95+
```julia
96+
julia> x = eps(N0f8) # smallest nonzero `N0f8` number
97+
0.004N0f8
98+
99+
julia> x*x
100+
0.0N0f8
101+
```
102+
103+
which is rounding-induced [underflow]. Finally,
104+
105+
```julia
106+
julia> x = N4f12(15)
107+
15.0N4f12
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

Comments
 (0)