non/spire — pull request #127 by non (2013-07-15 02:03:39+00:00)
Title: Topic/fixed point

This commit introduces the FixedPoint value class, which uses raw Long
values to encode fractions with a fixed base. The denominator is
provided using an implicit FixedScale instance.

import spire.math._

implicit val scale = FixedScale(1000000000)

// apply is the usual factory constructor, whereas new will
// directly construct a fixed point value.
val a = FixedPoint(scala.math.Pi)
val b = new FixedPoint(3141592653L)

a == b // these values are equal

Unlike normal Long values, FixedPoint values will throw errors on
overflow. This means that using FixedPoint values with FixedScale(1)
just adds overflow checking to "normal" Long values. While powers of
10 are probably the most obvious kind of fixed point denominator,
any positive Int denominator is valid.

Using FixedPoint values should not normally cause any allocations,
although in some edge cases SafeLong may be used temporarily.