utils

package
v0.0.0-...-e7bc4dc Latest Latest
Warning

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

Go to latest
Published: Dec 26, 2015 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Utility and fast math functions.

Index

Constants

This section is empty.

Variables

View Source
var (
	NanoToSec float32 = 1 / 1000000000

	FLOAT_ROUNDING_ERROR float32 = 0.000001 // 32 bits
	PI                   float32 = 3.1415927
	PI2                  float32 = PI * 2
	PI_4_3               float32 = PI * 4 / 3
	E                    float32 = 2.7182818

	SIN_BITS  uint = 14 // 16KB. Adjust for accuracy.
	SIN_MASK  int  = ^(-1 << SIN_BITS)
	SIN_COUNT int  = SIN_MASK + 1

	RadFull    float32 = PI * 2
	DegFull    float32 = 360
	RadToIndex float32 = float32(SIN_COUNT) / RadFull
	DegToIndex float32 = float32(SIN_COUNT) / DegFull

	// multiply by this to convert from radians to degrees
	RadiansToDegrees float32 = 180 / PI

	// multiply by this to convert from degrees to radians
	DegreesToRadians float32 = PI / 180
	DegRad           float32 = DegreesToRadians

	BIG_ENOUGH_INT   float32 = 16 * 1024
	BIG_ENOUGH_FLOOR float32 = BIG_ENOUGH_INT
	CEIL             float32 = 0.9999999
	BIG_ENOUGH_CEIL  float32 = 16384.999999999996
	BIG_ENOUGH_ROUND float32 = BIG_ENOUGH_INT + 0.5
)

Functions

func Atan2

func Atan2(y, x float32) float32

* Returns atan2 in radians, faster but less accurate than math.atan2. Average error of 0.00231 radians (0.1323 degrees),

  • largest error of 0.00488 radians (0.2796 degrees).

func Ceil

func Ceil(value float32) int

* Returns the smallest integer greater than or equal to the specified float. This method will only properly ceil floats from

  • -(2^14) to (Float.MAX_VALUE - 2^14).

func CeilPositive

func CeilPositive(value float32) int

* Returns the smallest integer greater than or equal to the specified float. This method will only properly ceil floats that

  • are positive.

func ClampFloat32

func ClampFloat32(value float32, min float32, max float32) float32

func ClampFloat64

func ClampFloat64(value float64, min float64, max float64) float64

func ClampInt

func ClampInt(value int, min int, max int) int

func ClampInt64

func ClampInt64(value int64, min int64, max int64) int64

func ClampInt8

func ClampInt8(value int8, min int8, max int8) int8

func Cos

func Cos(radians float32) float32

Returns the cosine in radians from a lookup table.

func CosDeg

func CosDeg(degrees float32) float32

Returns the cosine in radians from a lookup table.

func Floor

func Floor(value float32) int

* Returns the largest integer less than or equal to the specified float. This method will only properly floor floats from

  • -(2^14) to (Float.MAX_VALUE - 2^14).

func FloorPositive

func FloorPositive(value float32) int

* Returns the largest integer less than or equal to the specified float. This method will only properly floor floats that are

  • positive. Note this method simply casts the float32 to int.

func IsEqual

func IsEqual(a, b float32) bool

Returns true if a is nearly equal to b. The function uses the default floating error tolerance.

func IsEqualTolerance

func IsEqualTolerance(a, b, tolerance float32) bool

* Returns true if a is nearly equal to b.

  • @param a the first value.
  • @param b the second value.
  • @param tolerance represent an upper bound below which the two values are considered equal.

func IsPowerOfTwo

func IsPowerOfTwo(value int) bool

func IsZero

func IsZero(value float32) bool

Returns true if the value is zero (using the default tolerance as upper bound)

func IsZeroTolerance

func IsZeroTolerance(value, tolerance float32) bool

* Returns true if the value is zero.

  • @param tolerance represent an upper bound below which the value is considered zero.

func Lerp

func Lerp(fromValue, toValue, progress float32) float32

Linearly interpolates between fromValue to toValue on progress position.

func Log

func Log(a, value float32) float32

@return the logarithm of value with base a

func Log2

func Log2(value float32) float32

@return the logarithm of value with base 2

func NextPowerOfTwo

func NextPowerOfTwo(value int) int

Returns the next power of two. Returns the specified value if the value is already a power of two.

func Random

func Random(n int) int

* Returns a random number between 0 (inclusive) and the specified value (inclusive).

func RandomFloat

func RandomFloat() float32

func RandomRange

func RandomRange(start, end int) int

Returns a random number between start (inclusive) and end (inclusive).

func RandomTriangular

func RandomTriangular() float32

* Returns a triangularly distributed random number between -1.0 (exclusive) and 1.0 (exclusive), where values around zero are

  • more likely.
  • <p>
  • This is an optimized version of {@link #randomTriangular(float, float, float) randomTriangular(-1, 1, 0)}

func RandomTriangularMax

func RandomTriangularMax(max float32) float32

Returns a triangularly distributed random number between {@code -max} (exclusive) and {@code max} (exclusive), where values * around zero are more likely. * <p> * This is an optimized version of {@link #randomTriangular(float, float, float) randomTriangular(-max, max, 0)} * @param max the upper limit

func RandomTriangularMinMax

func RandomTriangularMinMax(min, max float32) float32

* Returns a triangularly distributed random number between {@code min} (inclusive) and {@code max} (exclusive), where the

  • {@code mode} argument defaults to the midpoint between the bounds, giving a symmetric distribution.
  • <p>
  • This method is equivalent of {@link #randomTriangular(float, float, float) randomTriangular(min, max, (max - min) * .5f)}
  • @param min the lower limit
  • @param max the upper limit

func RandomTriangularMinMaxMode

func RandomTriangularMinMaxMode(min, max, mode float32) float32

* Returns a triangularly distributed random number between {@code min} (inclusive) and {@code max} (exclusive), where values

  • around {@code mode} are more likely.
  • @param min the lower limit
  • @param max the upper limit
  • @param mode the point around which the values are more likely

func Round

func Round(value float32) int

* Returns the closest integer to the specified float. This method will only properly round floats from -(2^14) to

  • (Float.MAX_VALUE - 2^14).

func RoundPositive

func RoundPositive(value float32) int

Returns the closest integer to the specified float. This method will only properly round floats that are positive.

func Sin

func Sin(radians float32) float32

Returns the sine in radians from a lookup table.

func SinDeg

func SinDeg(degrees float32) float32

Returns the sine in radians from a lookup table.

Types

This section is empty.

Jump to

Keyboard shortcuts

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