Fixed point type
To use this package, run the following command in your project's root directory:
Put the following dependency into your project's dependences section:
Defines a fixed point type.
A fixed point number is a number with a fixed number of decimal places. The number of decimal places never varies, unlike floating point types, where the number of decimal places varies depending on the value.
Fixed point values are used wherever fractions are needed, but floating point values are undesirable or impractical, eg currencies.
Fixed point values are precise (no rounding issues) and are integral in behaviour (division and modulo work the same as they do for integers).
jaypha.fixed into your project. Instantiate with the desired scale.
auto v = Fixed!2(23); // Creates a value with 2 decimal places.
Fixed implements all the arithmetic, comparison and assignment operators, as well as casting to long and double types.
In addition the following methods/proerties are defined
pure nothrow auto conv(uint newScale)()
Converts to a different number of decimal places.
@property string asString()
Convert to a string. Includes the full number of decimal places.
Minimum and maximum possible values respectively for the implementation. Declared as static immutables.
fix3 are defined as aliases of
fix3 value = 21.44; value += 12; assert(value.asString == "33.440");
Distributed under the Boost License.
Perhaps change the string -> fixed algorithm to avoid using floating point. Not sure if it is worth the effort.
I am not certian how modulo is supposed to behave for fixed point. At the moment it returns the remainder after subtracting a whole number of dividors from the divisee. If this is incorrect and anyone knows the real behaviour, please inform me. Thanks.
- Registered by Jason den Dulk
- 1.0.2 released 8 years ago
- Copyright 2014, Jaypha