Skip to main content

Use decimals, not doubles

Floating point numbers, i.e. float and double, are the result of a trade-off between numerical precision and computation time. While great in many applications, we opted not to support them in Nancy to prevent their approximation errors from propagating through our algorithms.

Still, you will often want to write literals as a starting point in your code. To do this, you can use decimals by just appending an m to the right.

var a = new RateLatencyServiceCurve(4, 0.5);    // double, will not compile
var a = new RateLatencyServiceCurve(4, 0.5m); // decimal, ok

A decimal is a precise representation which will be translated, without any loss of accuracy, into a Rational with 10x10^x as its denominator.

Avoid divisions between decimals​

A core reason to use Rational numbers is to keep accurate representation for any rational - even those that would be periodic if written in decimal form. For example:

var a_decimal = 9m / 11m; // = 0.818181..
var a_rational = new Rational(9, 11); // = 9/11

The first form introduces a loss of precision, and big numerators and denominators when converted to a rational. The second form does not introduce any of the above.

Therefore, I recommend converting your parameters to Rationals as soon as possible.