signed

package
v1.3.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 26, 2021 License: Apache-2.0 Imports: 4 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Add

func Add(a, b int64) (int64, error)

Add adds two int64s and errors if there is an overflow

func Div

func Div(a, b int64) (int64, error)

Div divides two int64s and throws errors if there are problems

func DivMod

func DivMod(a, b int64) (int64, int64, error)

DivMod calculates the quotient and the remainder of dividing a by b, returns both, and and returns errors if there are issues.

func ExpFrac

func ExpFrac(numerator, denominator int64) (int64, error)

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).

func Mod

func Mod(a, b int64) (int64, error)

Mod calculates the remainder of dividing a by b and returns errors if there are issues.

func Mul

func Mul(a, b int64) (int64, error)

Mul multiplies two int64s and errors if there is an overflow

func MulDiv

func MulDiv(v, n, d int64) (int64, error)

MulDiv multiplies a int64 value by the ratio n/d without overflowing the int64, provided that the final result does not overflow. Returns error if the result cannot be converted back to int64.

func Sub

func Sub(a, b int64) (int64, error)

Sub subtracts two int64s and errors if there is an overflow

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL