#decimal #pure #integer #abs #numbers #precision #operations

nightly decimate

Simple decimal library in pure Rust

4 releases

Uses old Rust 2015

0.2.2 Aug 14, 2017
0.2.1 Jul 31, 2017
0.2.0 Jul 30, 2017
0.1.0 Feb 24, 2016

#4 in #abs

MIT license

26KB
606 lines

Simple implementation of decimal floating point numbers in pure rust.

Numbers are represented as significand and exponent. Significand is stored in an integer that is specified as type parameter, so basic operations should be reasonably fast.

When a limited integer type is used for significand then nearly all operations may cause integer overflows. Overflows are handled in the same way as with standard integer types: debug builds panic, release builds silently ignore them.

The main use case for this library is fast, low-overhead operations with financial data when precision and magnitude of numbers are well known beforehand and there is need for inexact conversions into ints and standard floating types for faster number-crunching.

This library implements non-standard precision handling. Decimals behave mostly as fixed-point numbers. Unary operations - and abs don't alter precision (number of decimal digits in the fractional part). Operations +, -, /, % don't change precision either if precision of both operands is the same. If it's not, then highest one is used (e.g. 12.3 + 11.111 == 23.411).

Multiplication is the only exception: precision of result is sum of precisions of operands (e.g. 123.4 * 0.2 == 24.68).

Dependencies

~275–630KB
~13K SLoC