README
¶
math
This was not written by the Engo project. The original author decided to remove the original project from GitHub, we were given permission to reupload the code as long as we did not credit them.
This library is a copy of the standard math library but for float32 (more common in computer graphics and games. Most of the function are simply casting the arguments as float64
and forwarding to the original math library (something you would have to do anyway, but slowly we are translating the original function to the float32
version.
ALL tests are passing, meaning whatever program you we're running using the 64 bit version should still be correct using this library (using the IEEE floating point definition of "correct").
I only care about i386 and amd64, but if I receive pull request for other architecture I will merge them (I can't test it so please send me proof that all test pass and benchmark are better :)
rewriten functions
The benchmark is based on my macbook pro, with an i386 processor but a x86_64 operating system. benchmark may produce different results on different computers. However if you see an architecture for which casting to float64, calling the standard lib then casting back to flaot32 is faster then my implementation please tell me.
✓ = Test passing.
✗ = Not implemented yet.
? = Implemented but not tested.
* = has no ASM implementation.
Abs | status | lux math | casting | std math | accel |
---|---|---|---|---|---|
function name | ✓, ✗, ? or * | benchmark for float32 using lux math package | benchmark for casting a var to float64, doing the op then casting back | benchmark for float64 using standard math package | accelleration between casting and lux math |
Abs | status | lux math | casting | std math | accel |
---|---|---|---|---|---|
software | ✓ | ||||
amd64 | ✓ | 2.10 ns/op | 2.37 ns/op | 2.08 ns/op | +13% |
386 | ✓ | 2.02 ns/op | 2.24 ns/op | 2.02 ns/op | +11% |
Sqrt | status | lux math | casting | std math | accel |
---|---|---|---|---|---|
software | ✓ | ||||
amd64 | ✓ | 2.33 ns/op | 4.70 ns/op | 0.33 ns/op | +102% |
386 | ✓ | 2.33 ns/op | 5.14 ns/op | 4.74 ns/op | +121% |
Acosh | status | lux math | casting | std math | accel |
---|---|---|---|---|---|
software | ✓ | 43.5 ns/op | 75.8 ns/op | 40.3 ns/op | +74% |
Asinh | status | lux math | casting | std math | accel |
---|---|---|---|---|---|
software | ✓ | 47.3 ns/op | 81.3 ns/op | 46.1 ns/op | +72% |
Gamma | status | lux math | casting | std math | accel |
---|---|---|---|---|---|
software | ✓ | 25.1 ns/op | 38.5 ns/op | 24.0 ns/op | +53% |
template | status | lux math | casting | std math | accel |
---|---|---|---|---|---|
software | ✓ | ns/op | ns/op | ns/op | % |
amd64 | ✓ | ns/op | ns/op | ns/op | % |
i386 | ✓ | ns/op | ns/op | ns/op | % |
Documentation
¶
Overview ¶
Package math provides basic constants and mathematical functions.
Index ¶
- Constants
- func Abs(x float32) float32
- func Acos(x float32) float32
- func Acosh(x float32) float32
- func Asin(x float32) float32
- func Asinh(x float32) float32
- func Atan(x float32) float32
- func Atan2(y, x float32) float32
- func Atanh(x float32) float32
- func Cbrt(x float32) float32
- func Ceil(x float32) float32
- func Clamp(f, low, high float32) float32
- func Copysign(x, y float32) float32
- func Cos(x float32) float32
- func Cosh(x float32) float32
- func Dim(x, y float32) float32
- func Erf(x float32) float32
- func Erfc(x float32) float32
- func Exp(x float32) float32
- func Exp2(x float32) float32
- func Expm1(x float32) float32
- func Float32bits(f float32) uint32
- func Float32frombits(b uint32) float32
- func Float64bits(f float64) uint64
- func Float64frombits(b uint64) float64
- func Floor(x float32) float32
- func Frexp(f float32) (frac float32, exp int)
- func Gamma(x float32) float32
- func Hypot(p, q float32) float32
- func Ilogb(x float32) int
- func Inf(sign int) float32
- func IsInf(f float32, sign int) bool
- func IsNaN(f float32) (is bool)
- func J0(x float32) float32
- func J1(x float32) float32
- func Jn(n int, x float32) float32
- func Ldexp(frac float32, exp int) float32
- func Lgamma(x float32) (lgamma float32, sign int)
- func Log(x float32) float32
- func Log10(x float32) float32
- func Log1p(x float32) float32
- func Log2(x float32) float32
- func Logb(x float32) float32
- func Max(x, y float32) float32
- func Min(x, y float32) float32
- func Mod(x, y float32) float32
- func Modf(f float32) (int float32, frac float32)
- func NaN() float32
- func Nextafter(x, y float32) (r float32)
- func Nextafter64(x, y float64) (r float64)
- func Pow(x, y float32) float32
- func Pow10(e int) float32
- func Remainder(x, y float32) float32
- func Signbit(x float32) bool
- func Sin(x float32) float32
- func Sincos(x float32) (sin, cos float32)
- func Sinh(x float32) float32
- func Sqrt(x float32) float32
- func Tan(x float32) float32
- func Tanh(x float32) float32
- func Trunc(x float32) float32
- func Y0(x float32) float32
- func Y1(x float32) float32
- func Yn(n int, x float32) float32
Constants ¶
const ( Totalsize = 32 MantissaSize = 23 ExponentSize = 8 Signsize = 1 Uvnan = 0x7FC00001 Uvinf = 0x7F800000 Uvneginf = 0xFF800000 //mask for exponent part Mask = 0xFF Shift = Totalsize - ExponentSize - Signsize //bias = (2 ^ (exponentSize - 1)) - 1 Bias = 127 )
Constants used for various operations.
const ( E = 2.71828182845904523536028747135266249775724709369995957496696763 // A001113 Pi = 3.14159265358979323846264338327950288419716939937510582097494459 // A000796 Phi = 1.61803398874989484820458683436563811772030917980576286213544862 // A001622 Sqrt2 = 1.41421356237309504880168872420969807856967187537694807317667974 // A002193 SqrtE = 1.64872127070012814684865078781416357165377610071014801157507931 // A019774 SqrtPi = 1.77245385090551602729816748334114518279754945612238712821380779 // A002161 SqrtPhi = 1.27201964951406896425242246173749149171560804184009624861664038 // A139339 Ln2 = 0.693147180559945309417232121458176568075500134360255254120680009 // A002162 Log2E = 1 / Ln2 Ln10 = 2.30258509299404568401799145468436420760110148862877297603332790 // A002392 Log10E = 1 / Ln10 Log10Ef = float32(1) / float32(Ln10) )
Mathematical constants. Reference: http://oeis.org/Axxxxxx
const ( MaxFloat32 = 3.40282346638528859811704183484516925440e+38 // 2**127 * (2**24 - 1) / 2**23 SmallestNonzeroFloat32 = 1.401298464324817070923729583289916131280e-45 // 1 / 2**(127 - 1 + 23) MaxFloat64 = 1.797693134862315708145274237317043567981e+308 // 2**1023 * (2**53 - 1) / 2**52 SmallestNonzeroFloat64 = 4.940656458412465441765687928682213723651e-324 // 1 / 2**(1023 - 1 + 52) )
Floating-point limit values. Max is the largest finite value representable by the type. SmallestNonzero is the smallest positive, non-zero value representable by the type.
const ( MaxInt8 = 1<<7 - 1 MinInt8 = -1 << 7 MaxInt16 = 1<<15 - 1 MinInt16 = -1 << 15 MaxInt32 = 1<<31 - 1 MinInt32 = -1 << 31 MaxInt64 = 1<<63 - 1 MinInt64 = -1 << 63 MaxUint8 = 1<<8 - 1 MaxUint16 = 1<<16 - 1 MaxUint32 = 1<<32 - 1 MaxUint64 = 1<<64 - 1 )
Integer limit values.
Variables ¶
This section is empty.
Functions ¶
func Acos ¶
Acos returns the arccosine, in radians, of x.
Special case is:
Acos(x) = NaN if x < -1 or x > 1
func Acosh ¶
Acosh returns the inverse hyperbolic cosine of x.
Special cases are:
Acosh(+Inf) = +Inf Acosh(x) = NaN if x < 1 Acosh(NaN) = NaN
func Asin ¶
Asin returns the arcsine, in radians, of x.
Special cases are:
Asin(±0) = ±0 Asin(x) = NaN if x < -1 or x > 1
func Asinh ¶
Asinh returns the inverse hyperbolic sine of x.
Special cases are:
Asinh(±0) = ±0 Asinh(±Inf) = ±Inf Asinh(NaN) = NaN
func Atan ¶
Atan returns the arctangent, in radians, of x.
Special cases are:
Atan(±0) = ±0 Atan(±Inf) = ±Pi/2
func Atan2 ¶
Atan2 returns the arc tangent of y/x, using the signs of the two to determine the quadrant of the return value.
Special cases are (in order):
Atan2(y, NaN) = NaN Atan2(NaN, x) = NaN Atan2(+0, x>=0) = +0 Atan2(-0, x>=0) = -0 Atan2(+0, x<=-0) = +Pi Atan2(-0, x<=-0) = -Pi Atan2(y>0, 0) = +Pi/2 Atan2(y<0, 0) = -Pi/2 Atan2(+Inf, +Inf) = +Pi/4 Atan2(-Inf, +Inf) = -Pi/4 Atan2(+Inf, -Inf) = 3Pi/4 Atan2(-Inf, -Inf) = -3Pi/4 Atan2(y, +Inf) = 0 Atan2(y>0, -Inf) = +Pi Atan2(y<0, -Inf) = -Pi Atan2(+Inf, x) = +Pi/2 Atan2(-Inf, x) = -Pi/2
func Atanh ¶
Atanh returns the inverse hyperbolic tangent of x.
Special cases are:
Atanh(1) = +Inf Atanh(±0) = ±0 Atanh(-1) = -Inf Atanh(x) = NaN if x < -1 or x > 1 Atanh(NaN) = NaN
func Cbrt ¶
Cbrt returns the cube root of x.
Special cases are:
Cbrt(±0) = ±0 Cbrt(±Inf) = ±Inf Cbrt(NaN) = NaN
func Ceil ¶
Ceil returns the least integer value greater than or equal to x.
Special cases are:
Ceil(±0) = ±0 Ceil(±Inf) = ±Inf Ceil(NaN) = NaN
func Cos ¶
Cos returns the cosine of the radian argument x.
Special cases are:
Cos(±Inf) = NaN Cos(NaN) = NaN
func Cosh ¶
Cosh returns the hyperbolic cosine of x.
Special cases are:
Cosh(±0) = 1 Cosh(±Inf) = +Inf Cosh(NaN) = NaN
func Dim ¶
Dim returns the maximum of x-y or 0.
Special cases are:
Dim(+Inf, +Inf) = NaN Dim(-Inf, -Inf) = NaN Dim(x, NaN) = Dim(NaN, x) = NaN
func Erf ¶
Erf returns the error function of x.
Special cases are:
Erf(+Inf) = 1 Erf(-Inf) = -1 Erf(NaN) = NaN
func Erfc ¶
Erfc returns the complementary error function of x.
Special cases are:
Erfc(+Inf) = 0 Erfc(-Inf) = 2 Erfc(NaN) = NaN
func Exp ¶
Exp returns e**x, the base-e exponential of x.
Special cases are:
Exp(+Inf) = +Inf Exp(NaN) = NaN
Very large values overflow to 0 or +Inf. Very small values underflow to 1.
func Expm1 ¶
Expm1 returns e**x - 1, the base-e exponential of x minus 1. It is more accurate than Exp(x) - 1 when x is near zero.
Special cases are:
Expm1(+Inf) = +Inf Expm1(-Inf) = -1 Expm1(NaN) = NaN
Very large values overflow to -1 or +Inf.
func Float32bits ¶
Float32bits returns the IEEE 754 binary representation of f.
func Float32frombits ¶
Float32frombits returns the floating point number corresponding to the IEEE 754 binary representation b.
func Float64bits ¶
Float64bits returns the IEEE 754 binary representation of f.
func Float64frombits ¶
Float64frombits returns the floating point number corresponding the IEEE 754 binary representation b.
func Floor ¶
Floor returns the greatest integer value less than or equal to x.
Special cases are:
Floor(±0) = ±0 Floor(±Inf) = ±Inf Floor(NaN) = NaN
func Frexp ¶
Frexp breaks f into a normalized fraction and an integral power of two. It returns frac and exp satisfying f == frac × 2**exp, with the absolute value of frac in the interval [½, 1).
Special cases are:
Frexp(±0) = ±0, 0 Frexp(±Inf) = ±Inf, 0 Frexp(NaN) = NaN, 0
func Gamma ¶
Gamma returns the Gamma function of x.
Special cases are:
Gamma(+Inf) = +Inf Gamma(+0) = +Inf Gamma(-0) = -Inf Gamma(x) = NaN for integer x < 0 Gamma(-Inf) = NaN Gamma(NaN) = NaN
func Hypot ¶
Hypot returns Sqrt(p*p + q*q), taking care to avoid unnecessary overflow and underflow.
Special cases are:
Hypot(±Inf, q) = +Inf Hypot(p, ±Inf) = +Inf Hypot(NaN, q) = NaN Hypot(p, NaN) = NaN
func Ilogb ¶
Ilogb returns the binary exponent of x as an integer.
Special cases are:
Ilogb(±Inf) = MaxInt32 Ilogb(0) = MinInt32 Ilogb(NaN) = MaxInt32
func IsInf ¶
IsInf reports whether f is an infinity, according to sign. If sign > 0, IsInf reports whether f is positive infinity. If sign < 0, IsInf reports whether f is negative infinity. If sign == 0, IsInf reports whether f is either infinity.
func J0 ¶
J0 returns the order-zero Bessel function of the first kind.
Special cases are:
J0(±Inf) = 0 J0(0) = 1 J0(NaN) = NaN
func J1 ¶
J1 returns the order-one Bessel function of the first kind.
Special cases are:
J1(±Inf) = 0 J1(NaN) = NaN
func Jn ¶
Jn returns the order-n Bessel function of the first kind.
Special cases are:
Jn(n, ±Inf) = 0 Jn(n, NaN) = NaN
func Ldexp ¶
Ldexp is the inverse of Frexp. It returns frac × 2**exp.
Special cases are:
Ldexp(±0, exp) = ±0 Ldexp(±Inf, exp) = ±Inf Ldexp(NaN, exp) = NaN
func Lgamma ¶
Lgamma returns the natural logarithm and sign (-1 or +1) of Gamma(x).
Special cases are:
Lgamma(+Inf) = +Inf Lgamma(0) = +Inf Lgamma(-integer) = +Inf Lgamma(-Inf) = -Inf Lgamma(NaN) = NaN
func Log ¶
Log returns the natural logarithm of x.
Special cases are:
Log(+Inf) = +Inf Log(0) = -Inf Log(x < 0) = NaN Log(NaN) = NaN
func Log1p ¶
Log1p returns the natural logarithm of 1 plus its argument x. It is more accurate than Log(1 + x) when x is near zero.
Special cases are:
Log1p(+Inf) = +Inf Log1p(±0) = ±0 Log1p(-1) = -Inf Log1p(x < -1) = NaN Log1p(NaN) = NaN
func Logb ¶
Logb returns the binary exponent of x.
Special cases are:
Logb(±Inf) = +Inf Logb(0) = -Inf Logb(NaN) = NaN
func Max ¶
Max returns the larger of x or y.
Special cases are:
Max(x, +Inf) = Max(+Inf, x) = +Inf Max(x, NaN) = Max(NaN, x) = NaN Max(+0, ±0) = Max(±0, +0) = +0 Max(-0, -0) = -0
func Min ¶
Min returns the smaller of x or y.
Special cases are:
Min(x, -Inf) = Min(-Inf, x) = -Inf Min(x, NaN) = Min(NaN, x) = NaN Min(-0, ±0) = Min(±0, -0) = -0
func Mod ¶
Mod returns the floating-point remainder of x/y. The magnitude of the result is less than y and its sign agrees with that of x.
Special cases are:
Mod(±Inf, y) = NaN Mod(NaN, y) = NaN Mod(x, 0) = NaN Mod(x, ±Inf) = x Mod(x, NaN) = NaN
func Modf ¶
Modf returns integer and fractional floating-point numbers that sum to f. Both values have the same sign as f.
Special cases are:
Modf(±Inf) = ±Inf, NaN Modf(NaN) = NaN, NaN
func Nextafter ¶
Nextafter returns the next representable float32 value after x towards y. Special cases:
Nextafter32(x, x) = x Nextafter32(NaN, y) = NaN Nextafter32(x, NaN) = NaN
Since this is a float32 math package the 32 bit version has no number and the 64 bit version has the number in the method name.
func Nextafter64 ¶
Nextafter64 returns the next representable float64 value after x towards y. Special cases:
Nextafter64(x, x) = x Nextafter64(NaN, y) = NaN Nextafter64(x, NaN) = NaN
Since this is a float32 math package the 32 bit version has no number and the 64 bit version has the number in the method name.
func Pow ¶
Pow returns x**y, the base-x exponential of y.
Special cases are (in order):
Pow(x, ±0) = 1 for any x Pow(1, y) = 1 for any y Pow(x, 1) = x for any x Pow(NaN, y) = NaN Pow(x, NaN) = NaN Pow(±0, y) = ±Inf for y an odd integer < 0 Pow(±0, -Inf) = +Inf Pow(±0, +Inf) = +0 Pow(±0, y) = +Inf for finite y < 0 and not an odd integer Pow(±0, y) = ±0 for y an odd integer > 0 Pow(±0, y) = +0 for finite y > 0 and not an odd integer Pow(-1, ±Inf) = 1 Pow(x, +Inf) = +Inf for |x| > 1 Pow(x, -Inf) = +0 for |x| > 1 Pow(x, +Inf) = +0 for |x| < 1 Pow(x, -Inf) = +Inf for |x| < 1 Pow(+Inf, y) = +Inf for y > 0 Pow(+Inf, y) = +0 for y < 0 Pow(-Inf, y) = Pow(-0, -y) Pow(x, y) = NaN for finite x < 0 and finite non-integer y
func Pow10 ¶
Pow10 returns 10**e, the base-10 exponential of e.
Special cases are:
Pow10(e) = +Inf for e > 309 Pow10(e) = 0 for e < -324
func Remainder ¶
Remainder returns the IEEE 754 floating-point remainder of x/y.
Special cases are:
Remainder(±Inf, y) = NaN Remainder(NaN, y) = NaN Remainder(x, 0) = NaN Remainder(x, ±Inf) = x Remainder(x, NaN) = NaN
func Sin ¶
Sin returns the sine of the radian argument x.
Special cases are:
Sin(±0) = ±0 Sin(±Inf) = NaN Sin(NaN) = NaN
func Sincos ¶
Sincos returns Sin(x), Cos(x).
Special cases are:
Sincos(±0) = ±0, 1 Sincos(±Inf) = NaN, NaN Sincos(NaN) = NaN, NaN
func Sinh ¶
Sinh returns the hyperbolic sine of x.
Special cases are:
Sinh(±0) = ±0 Sinh(±Inf) = ±Inf Sinh(NaN) = NaN
func Sqrt ¶
Sqrt returns the square root of x.
Special cases are:
Sqrt(+Inf) = +Inf Sqrt(±0) = ±0 Sqrt(x < 0) = NaN Sqrt(NaN) = NaN
func Tan ¶
Tan returns the tangent of the radian argument x.
Special cases are:
Tan(±0) = ±0 Tan(±Inf) = NaN Tan(NaN) = NaN
func Tanh ¶
Tanh returns the hyperbolic tangent of x.
Special cases are:
Tanh(±0) = ±0 Tanh(±Inf) = ±1 Tanh(NaN) = NaN
func Trunc ¶
Trunc returns the integer value of x.
Special cases are:
Trunc(±0) = ±0 Trunc(±Inf) = ±Inf Trunc(NaN) = NaN
func Y0 ¶
Y0 returns the order-zero Bessel function of the second kind.
Special cases are:
Y0(+Inf) = 0 Y0(0) = -Inf Y0(x < 0) = NaN Y0(NaN) = NaN
Types ¶
This section is empty.