From 60e26f496d40da0c1b701b40dd422fe1b261476f Mon Sep 17 00:00:00 2001 From: Simon Etter Date: Thu, 10 Nov 2022 17:45:39 +0800 Subject: [PATCH 1/2] Fix misleading statement regarding sign bit The original formulation of the README made me think that the bit pattern of `Fixed{IntN,f}` numbers is `[sign bit] [Int(N-1) number]`. I hope this alternative formulation makes it clearer that `Fixed{T,f}` numbers are based on 2's complement just like the underlying integer type `T`. --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 35f9e202..7fa3c2d8 100644 --- a/README.md +++ b/README.md @@ -32,11 +32,11 @@ For `T<:Signed` (a signed integer), there is a fixed-point type `Normed{T, f}` type. However, there are slight differences in behavior that go beyond signed/unsigned distinctions. -The `Fixed{T,f}` types use 1 bit for sign, and `f` bits to represent -the fraction. For example, `Fixed{Int8,7}` uses 7 bits (all bits -except the sign bit) for the fractional part. The value of the number -is interpreted as if the integer representation has been divided by -`2^f`. Consequently, `Fixed{Int8,7}` numbers `x` satisfy +The `Fixed{T,f}` types take the integer type `T` and simply shift the "decimal" +point (or more accurately, the [radix or binary +point](https://en.wikipedia.org/wiki/Decimal_separator#Radix_point)) `f` bits to +the left, i.e. they represent real numbers of the form `x::T / 2^f`. +Consequently, `Fixed{Int8,7}` numbers `x` satisfy ```julia -1.0 = -128/128 ≤ x ≤ 127/128 ≈ 0.992. From 3ea0ce47e06455402539693c980c096b06cece02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20H=C3=A4cker?= Date: Fri, 16 Oct 2026 10:49:28 +0200 Subject: [PATCH 2/2] Describe `Fixed` by its value instead of a sign bit "1 bit for sign, `f` bits for the fraction" suggests a sign-magnitude layout with a negative zero and a symmetric range. Define the value as the raw integer divided by `2^f` instead, and show that raw `-1` is the negative number closest to zero. Name the Q notation variant behind the `QXfY` aliases, because it explains why `X+Y` is one less than the number of bits. --- README.md | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 7fa3c2d8..003f9113 100644 --- a/README.md +++ b/README.md @@ -32,17 +32,28 @@ For `T<:Signed` (a signed integer), there is a fixed-point type `Normed{T, f}` type. However, there are slight differences in behavior that go beyond signed/unsigned distinctions. -The `Fixed{T,f}` types take the integer type `T` and simply shift the "decimal" -point (or more accurately, the [radix or binary -point](https://en.wikipedia.org/wiki/Decimal_separator#Radix_point)) `f` bits to -the left, i.e. they represent real numbers of the form `x::T / 2^f`. -Consequently, `Fixed{Int8,7}` numbers `x` satisfy +A `Fixed{T,f}` number is an integer `i::T` divided by `2^f`. Its range and its +overflow behavior therefore follow those of `T`. For example, `Fixed{Int8,7}` +numbers `x` satisfy ```julia -1.0 = -128/128 ≤ x ≤ 127/128 ≈ 0.992. ``` -because the range of `Int8` is from -128 to 127. +because the range of `Int8` is from -128 to 127. Like `Int8`, `Fixed{Int8,7}` +has a single zero, and the raw value `-1` is the negative number closest to zero: + +```julia +julia> reinterpret(Q0f7, Int8(-1)) +-0.008Q0f7 +``` + +The `QXfY` aliases follow the Texas Instruments variant of the [Q notation]: +`Y` is the number of fraction bits, and `X+Y+1` equals the number of bits of +`T`. For example, `Q0f7` is aliased to `Fixed{Int8,7}` and `Q3f12` to +`Fixed{Int16,12}`. Unlike for the `NXfY` aliases below, `X+Y` is one less than +the number of bits, so `Fixed{Int8,0}`, which represents the same values as +`Int8`, is called `Q7f0`. In contrast, the `Normed{T,f}`, with `f` fraction bits, map the closed interval [0.0,1.0] to the span of numbers with `f` bits. For example, @@ -141,6 +152,7 @@ Please see [CONTRIBUTING.md](CONTRIBUTING.md) for information about improving th [fixed-point number]: http://en.wikipedia.org/wiki/Fixed-point_arithmetic [overflow]: https://en.wikipedia.org/wiki/Integer_overflow +[Q notation]: https://en.wikipedia.org/wiki/Q_(number_format) [rounding]: https://en.wikipedia.org/wiki/Round-off_error [underflow]: https://en.wikipedia.org/wiki/Arithmetic_underflow