diff --git a/libs/util/include/psemek/util/fixed_point.hpp b/libs/util/include/psemek/util/fixed_point.hpp index 20295b43..8827092a 100644 --- a/libs/util/include/psemek/util/fixed_point.hpp +++ b/libs/util/include/psemek/util/fixed_point.hpp @@ -20,7 +20,7 @@ namespace psemek::util * represented as an (I+F)-bit-wide standard signed integer type, and the * stored value is interpreted as representing the fraction (value / 2^F). * It is capable of representing values in the range from (- 2^(I+F-1)) / 2^F - * to (2^(I+F-1) - 1) / 2^F, with a fixed precision of 2^F. + * to (2^(I+F-1) - 1) / 2^F, with a fixed precision of 1 / 2^F. * * For example, * @@ -40,7 +40,9 @@ namespace psemek::util * separate question...). * * Multiplication and division are implemented by casting to a 2x wider integer type, - * to increase precision and prevent overflows. + * to increase precision and prevent overflows. Note that the result is still truncated + * to the original size, i.e. multiplying two 16.16 fixed-points gives a 16.16 fixed-point + * as well. * * Typical usage of this class would be to make a type alias like * @@ -345,6 +347,8 @@ namespace psemek::util if (p < 0) return fixed_point(1) / pow(x, -p); + // Binary exponentiation algorithm + fixed_point result(1); while (p > 0) {