Package Usage: go: github.com/thomaso-mirodin/intmath
Package intmath provides a simple integer math library.
The standard Go math library uses the float64 type for everything.
This often requires casting both input and result, resulting in
more verbose and (possibly) slower code. This library aims to
extend the Go math library by providing math packages for the int,
uint, int32, uint32, int64 and uint64 types.
The intmath package itself is just a dummy package used for
documentation, testing and benchmarking - the real library is
in the subpackages. Import the relevant type to use math
functions tailored to that type.
Note that the dedicated FPU in your computer can be so fast that
casting to and from float64 would be faster than using integer
math. In other words, the standard math library can in theory
outperform these libraries despite repeated casting to and from
float64.
Similarly, some code in u64 and i64 may be optimised to run faster
on 32bit systems, at a minor cost for 64bit systems. All else being
equal, the code in the library will favour this behaviour
(see for example the u64.Log2 source).
Benchmarks are included so you can easily test the differences for
yourself. They assume a uniform distribution of input.
Not all math functions will be copied, as not all of them make
sense in an integer math context. At the moment, the following
functions have been implemented:
As a general rule, all functions take and return types matching
the type of the library. Functions in u64 only take and return
uint64, for example. Think of it as fake function overloading:
the functions have the same name in every library, but the
library name determines the type. Compare the following code
snippets using the standard math library, a hypothetical library
throwing all types in the same library, and this library:
Note that the last example is the least verbose, yet it is
immediately clear what the types of c, k and z are. If there ever
will be exceptions to this general rule of input and return types
the functions will be listed here and the reasoning behind making
an exception will be explained in the documentation.
As mentioned before, the speed of modern FPUs should not be
underestimated. Benchmarking is advised to see if your system
actually benefits form this library. To do this run go test:
Some benchmarks also include alternate implementations for
different functions. These might just be more effective on your
architecture.
1 version
Latest release: over 9 years ago
136 dependent packages
View more package details: https://packages.ecosystem.code.gouv.fr/registries/proxy.golang.org/packages/github.com/thomaso-mirodin/intmath