math32

package
v0.0.0-...-d322e89 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2018 License: BSD-2-Clause Imports: 2 Imported by: 0

Documentation

Overview

Package math provides efficient float32 math functions.

Index

Constants

View Source
const (
	E   = float32(2.71828182845904523536028747135266249775724709369995957496696763) // A001113
	Pi  = float32(3.14159265358979323846264338327950288419716939937510582097494459) // A000796
	Phi = float32(1.61803398874989484820458683436563811772030917980576286213544862) // A001622

	Sqrt2   = float32(1.41421356237309504880168872420969807856967187537694807317667974) // A002193
	SqrtE   = float32(1.64872127070012814684865078781416357165377610071014801157507931) // A019774
	SqrtPi  = float32(1.77245385090551602729816748334114518279754945612238712821380779) // A002161
	SqrtPhi = float32(1.27201964951406896425242246173749149171560804184009624861664038) // A139339

	Ln2    = float32(0.693147180559945309417232121458176568075500134360255254120680009) // A002162
	Log2E  = float32(1 / Ln2)
	Ln10   = float32(2.30258509299404568401799145468436420760110148862877297603332790) // A002392
	Log10E = float32(1 / Ln10)
)

Mathematical constants. Reference: http://oeis.org/Axxxxxx

View Source
const (
	MaxFloat32             = float32(3.40282346638528859811704183484516925440e+38)  // 2**127 * (2**24 - 1) / 2**23
	SmallestNormalFloat32  = float32(1.17549435082229e-38)                          // 1 / 2**(127 - 1)
	EpsilonFloat32         = float32(1.19209290e-07)                                // 1 / 2**23
	SmallestNonzeroFloat32 = float32(1.401298464324817070923729583289916131280e-45) // 1 / 2**(127 - 1 + 23)

	MaxFloat64             = 1.797693134862315708145274237317043567981e+308 // 2**1023 * (2**53 - 1) / 2**52
	SmallestNormalFloat64  = 2.2250738585072014e-308                        // 1 / 2**(1023 - 1)
	EpsilonFloat64         = 2.2204460492503131e-16                         // 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. SmallestNormal is the smallest normal value representable by the type. Epsilon is the smallest value that, when added to one, yields a result different from one. SmallestNonzero is the smallest positive, non-zero value representable by the type.

View Source
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 Abs

func Abs(x float32) float32

Abs returns the absolute value of x.

func Copysign

func Copysign(x, y float32) float32

Copysign returns a value with the magnitude of x and the sign of y.

func Cos

func Cos(x float32) float32

Cos returns the sinus of x.

func FastCos

func FastCos(x float32) float32

FastCos returns an approximation of sin(x).

Max absolute error in range [-Pi, Pi]: less than 1e-3

Faster than Cos.

func FastFloor

func FastFloor(x float32) int32

FastFloor returns int32(x) if x>0, int32(x-1) otherwise.

func FastSin

func FastSin(x float32) float32

FastSin returns an approximation of sin(x).

Max absolute error in range [-Pi, Pi]: less than 1e-3

Faster than Sin.

func Float32bits

func Float32bits(f float32) uint32

Float32bits returns the IEEE 754 binary representation of f.

func Float32frombits

func Float32frombits(b uint32) float32

Float32frombits returns the floating point number corresponding to the IEEE 754 binary representation b.

func Float64bits

func Float64bits(f float64) uint64

Float64bits returns the IEEE 754 binary representation of f.

func Float64frombits

func Float64frombits(b uint64) float64

Float64frombits returns the floating point number corresponding the IEEE 754 binary representation b.

func Floor

func Floor(x float32) float32

Floor returns the nearest integer less than or equal to x.

func Frexp

func Frexp(f float32) (frac float32, exp int)

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 Inf

func Inf(sign int) float32

Inf returns positive infinity if sign >= 0, negative infinity if sign < 0.

func IsAlmostEqual

func IsAlmostEqual(a, b float32, ulps uint32) bool

IsAlmostEqual returns true if the difference between a and b in ULPs (Unit in the Last Place) is less than ulps.

Handle special cases: zero, infinites, denormals.

See also IsNearlyEqual and IsRoughlyEqual.

func IsInf

func IsInf(f float32, sign int) bool

IsInf returns whether f is an infinity, according to sign. If sign > 0, IsInf returns whether f is positive infinity. If sign < 0, IsInf returns whether f is negative infinity. If sign == 0, IsInf returns whether f is either infinity.

func IsNaN

func IsNaN(f float32) (is bool)

IsNaN returns whether f is an IEEE 754 "not-a-number" value.

func IsNearlyEqual

func IsNearlyEqual(a, b float32, epsilon float32) bool

IsNearlyEqual Returns true if the relative error between a and b is less than epsilon.

Handles special cases: zero, infinites, denormals.

See also IsAlmostEqual and IsRoughlyEqual.

func IsRoughlyEqual

func IsRoughlyEqual(a, b float32, epsilon float32) bool

IsRoughlyEqual Returns true if the absolute error between a and b is less than epsilon.

See also IsNearlyEqual and IsAlmostEqual.

func Ldexp

func Ldexp(frac float32, exp int) float32

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 Mix

func Mix(x, y float32, a float32) float32

Mix returns the linear interpolation between x and y using a to weight between them. The value is computed as follows: x*(1-a) + y*a

func Mod

func Mod(x, y float32) float32

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

func Modf(f float32) (int float32, frac float32)

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 NaN

func NaN() float32

NaN returns an IEEE 754 "not-a-number" value.

func Normalized

func Normalized(x float32) (y float32, exp int)

Normalized returns a normal number y and exponent exp satisfying x == y × 2**exp. It assumes x is finite and non-zero.

func Round

func Round(x float32) int32

Round returns the nearest integer to x.

func Sin

func Sin(x float32) float32

Sin returns the sinus of x.

func Sqrt

func Sqrt(x float32) float32

Sqrt returns the square root of x.

func Tan

func Tan(x float32) float32

Tan returns the tangent of x.

Types

This section is empty.

Jump to

Keyboard shortcuts

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