Documentation
¶
Index ¶
- func Add(a, b int64) (int64, error)
- func Div(a, b int64) (int64, error)
- func DivMod(a, b int64) (int64, int64, error)
- func ExpFrac(numerator, denominator int64) (int64, error)
- func Mod(a, b int64) (int64, error)
- func Mul(a, b int64) (int64, error)
- func MulDiv(v, n, d int64) (int64, error)
- func Sub(a, b int64) (int64, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DivMod ¶
DivMod calculates the quotient and the remainder of dividing a by b, returns both, and and returns errors if there are issues.
func ExpFrac ¶
ExpFrac calculates e^x, where x is a fraction numerator/denominator between 0 and 1. We use a Taylor Series expansion of e^x that converges well in the target range. This expansion is x^0/0! + x^1/1! + x^2/2! ... We can collapse the first two terms for convenience to 1+x. In addition, we make use of the fact that (numerator/denominator)^2 = numerator^2/denominator^2 so we can use muldiv and we require that denominator <= maxint32, and that numerator < denominator. Basically, we compute (denominator + numerator + numerator^2/2denominator + numerator^3/6denominator^2 ...) which is denominator times our desired result (so that we have the implied denominator).
The return value is the numerator for the fraction; the denominator is unchanged. This fixed point calculation tends to produce values that are slightly off in the last digit (as compared to a floating point implementation) because of accumulated rounding errors. Therefore, what we do is scale the input fraction by multiplying both numerator and denominator by a scaling value and then divide by it again at the end. This means that the practical limit for denominator is maxint32 / 10, which is still larger than our napu multiplication factor of 100,000,000 (which is also the value we use for percentages).
Types ¶
This section is empty.