Package com.github.tommyettinger.digital
Class TrigTools
java.lang.Object
com.github.tommyettinger.digital.TrigTools
Various trigonometric approximations, using a lookup table for sin(), cos(), and tan(), and Taylor series for their
inverses. This supplies variants for radians, degrees, and turns. This also has an atan2() approximation defined with
output in radians, degrees, and turns.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final float
Multiply by this to convert from degrees to radians.static final float
PI
divided by 2f.static final float
Thefloat
value that is closer than any other to pi, the ratio of the circumference of a circle to its diameter.static final float
1.0f divided byPI
.static final float
static final float
PI
divided by 4f.static final float
Multiply by this to convert from radians to degrees.static final float[]
A precalculated table of 16384 floats, corresponding to the y-value of points on the unit circle, ordered by increasing angle.static final int
The bitmask that can be used to confine any int to wrap withinTABLE_SIZE
.static final int
The size ofSIN_TABLE
, available separately from the table's length for convenience.static final float
-
Method Summary
Modifier and TypeMethodDescriptionstatic float
acos
(float a) Returns arccosine in radians; less accurate than Math.acos but may be faster.static float
acosDeg
(float a) Returns arccosine in degrees.static float
acosTurns
(float a) Returns arccosine in turns.static float
asin
(float a) Returns arcsine in radians; less accurate than Math.asin but may be faster.static float
asinDeg
(float a) Returns arcsine in degrees.static float
asinTurns
(float a) Returns arcsine in turns.static float
atan
(float i) Arc tangent approximation with very low error, using an algorithm from the 1955 research study "Approximations for Digital Computers," by RAND Corporation (this is sheet 11's algorithm, which is the fourth-fastest and fourth-least precise).static float
atan2
(float y, float x) Close approximation of the frequently-used trigonometric method atan2, using radians.static float
atan2Deg
(float y, float x) Close approximation of the frequently-used trigonometric method atan2, using positive or negative degrees.static float
atan2Deg360
(float y, float x) Close approximation of the frequently-used trigonometric method atan2, using non-negative degrees only.static float
atan2Turns
(float y, float x) Close approximation of the frequently-used trigonometric method atan2, using non-negative turns only.static float
atanDeg
(float i) Arc tangent approximation returning a value measured in positive or negative degrees, using an algorithm from the 1955 research study "Approximations for Digital Computers," by RAND Corporation (this is sheet 11's algorithm, which is the fourth-fastest and fourth-least precise).static float
atanTurns
(float i) Arc tangent approximation with very low error, using an algorithm from the 1955 research study "Approximations for Digital Computers," by RAND Corporation (this is sheet 11's algorithm, which is the fourth-fastest and fourth-least precise).static double
atanUnchecked
(double i) A variant onatan(float)
that does not tolerate infinite inputs for speed reasons.static double
atanUncheckedDeg
(double i) A variant onatanDeg(float)
that does not tolerate infinite inputs for speed reasons.static double
atanUncheckedTurns
(double i) A variant onatanTurns(float)
that does not tolerate infinite inputs for speed reasons.static float
cos
(float radians) Returns the cosine in radians from a lookup table.static float
cosDeg
(float degrees) Returns the cosine in degrees from a lookup table.static float
cosTurns
(float turns) Returns the cosine in turns from a lookup table.static float
sin
(float radians) Returns the sine in radians from a lookup table.static float
sinDeg
(float degrees) Returns the sine in degrees from a lookup table.static float
sinTurns
(float turns) Returns the sine in turns from a lookup table.static float
tan
(float radians) Returns the tangent in radians from a lookup table.static float
tanDeg
(float degrees) Returns the tangent in degrees from a lookup table.static float
tanTurns
(float turns) Returns the tangent in turns from a lookup table.
-
Field Details
-
PI
public static final float PIThefloat
value that is closer than any other to pi, the ratio of the circumference of a circle to its diameter.- See Also:
-
PI_INVERSE
public static final float PI_INVERSE1.0f divided byPI
.- See Also:
-
PI2
public static final float PI2- See Also:
-
TAU
public static final float TAU- See Also:
-
HALF_PI
public static final float HALF_PIPI
divided by 2f.- See Also:
-
QUARTER_PI
public static final float QUARTER_PIPI
divided by 4f.- See Also:
-
TABLE_SIZE
public static final int TABLE_SIZEThe size ofSIN_TABLE
, available separately from the table's length for convenience.- See Also:
-
TABLE_MASK
public static final int TABLE_MASKThe bitmask that can be used to confine any int to wrap withinTABLE_SIZE
. Any accesses toSIN_TABLE
with an index that could be out of bounds should probably be wrapped using this, as withSIN_TABLE[index & TABLE_MASK]
.- See Also:
-
radiansToDegrees
public static final float radiansToDegreesMultiply by this to convert from radians to degrees.- See Also:
-
degreesToRadians
public static final float degreesToRadiansMultiply by this to convert from degrees to radians.- See Also:
-
SIN_TABLE
public static final float[] SIN_TABLEA precalculated table of 16384 floats, corresponding to the y-value of points on the unit circle, ordered by increasing angle. This should not be mutated, but it can be accessed directly for things like getting random unit vectors, or implementing the "sincos" method (which assigns sin() to one item and cos() to another).
A quick way to get a random unit vector is to get a random 14-bit number, as withint angle = random.nextInt() >>> 18;
, look up angle in this table to get y, then look up(angle + 4096) & 16383
to get x.
-
-
Method Details
-
sin
public static float sin(float radians) Returns the sine in radians from a lookup table. For optimal precision, use radians between -PI2 and PI2 (both inclusive).- Parameters:
radians
- an angle in radians, where 0 toPI2
is one rotation
-
cos
public static float cos(float radians) Returns the cosine in radians from a lookup table. For optimal precision, use radians between -PI2 and PI2 (both inclusive).- Parameters:
radians
- an angle in radians, where 0 toPI2
is one rotation
-
tan
public static float tan(float radians) Returns the tangent in radians from a lookup table. For optimal precision, use radians between -PI2 and PI2 (both inclusive).- Parameters:
radians
- an angle in radians, where 0 toPI2
is one rotation
-
sinDeg
public static float sinDeg(float degrees) Returns the sine in degrees from a lookup table. For optimal precision, use degrees between -360 and 360 (both inclusive).- Parameters:
degrees
- an angle in degrees, where 0 to 360 is one rotation
-
cosDeg
public static float cosDeg(float degrees) Returns the cosine in degrees from a lookup table. For optimal precision, use degrees between -360 and 360 (both inclusive).- Parameters:
degrees
- an angle in degrees, where 0 to 360 is one rotation
-
tanDeg
public static float tanDeg(float degrees) Returns the tangent in degrees from a lookup table. For optimal precision, use degrees between -360 and 360 (both inclusive).- Parameters:
degrees
- an angle in degrees, where 0 to 360 is one rotation
-
sinTurns
public static float sinTurns(float turns) Returns the sine in turns from a lookup table. For optimal precision, use turns between -1 and 1 (both inclusive).- Parameters:
turns
- an angle in turns, where 0 to 1 is one rotation
-
cosTurns
public static float cosTurns(float turns) Returns the cosine in turns from a lookup table. For optimal precision, use turns between -1 and 1 (both inclusive).- Parameters:
turns
- an angle in turns, where 0 to 1 is one rotation
-
tanTurns
public static float tanTurns(float turns) Returns the tangent in turns from a lookup table. For optimal precision, use turns between -1 and 1 (both inclusive).- Parameters:
turns
- an angle in turns, where 0 to 1 is one rotation
-
atanUnchecked
public static double atanUnchecked(double i) A variant onatan(float)
that does not tolerate infinite inputs for speed reasons. This can be given a double parameter, but is otherwise the same as atan(float), and returns a float like that method. It uses the same approximation, from sheet 11 of "Approximations for Digital Computers." This is mostly meant to be used insideatan2(float, float)
, but it may be a tiny bit faster than atan(float) in other code.- Parameters:
i
- any finite double or float, but more commonly a float- Returns:
- an output from the inverse tangent function in radians, from
-HALF_PI
toHALF_PI
inclusive
-
atanUncheckedTurns
public static double atanUncheckedTurns(double i) A variant onatanTurns(float)
that does not tolerate infinite inputs for speed reasons. This can be given a double parameter, but is otherwise the same as atanTurns(float), but returns a double in case external code needs higher precision. It uses the same approximation, from sheet 11 of "Approximations for Digital Computers." This is mostly meant to be used insideatan2Turns(float, float)
, but it may be a tiny bit faster than atanTurns(float) in other code.- Parameters:
i
- any finite double or float, but more commonly a float- Returns:
- an output from the inverse tangent function in turns, from
-0.5
to0.5
inclusive
-
atanUncheckedDeg
public static double atanUncheckedDeg(double i) A variant onatanDeg(float)
that does not tolerate infinite inputs for speed reasons. This can be given a double parameter, but is otherwise the same as atanDeg(float), and returns a float like that method. It uses the same approximation, from sheet 11 of "Approximations for Digital Computers." This is mostly meant to be used insideatan2(float, float)
, but it may be a tiny bit faster than atanDeg(float) in other code.- Parameters:
i
- any finite double or float, but more commonly a float- Returns:
- an output from the inverse tangent function in degrees, from
-90
to90
inclusive
-
atan2
public static float atan2(float y, float x) Close approximation of the frequently-used trigonometric method atan2, using radians. Average error is 1.057E-6 radians; maximum error is 1.922E-6. Takes y and x (in that unusual order) as floats, and returns the angle from the origin to that point in radians. It is about 4 times faster thanMath.atan2(double, double)
(roughly 15 ns instead of roughly 60 ns for Math, on Java 8 HotSpot).
Credit for this goes to the 1955 research study "Approximations for Digital Computers," by RAND Corporation. This is sheet 11's algorithm, which is the fourth-fastest and fourth-least precise. The algorithms on sheets 8-10 are faster, but only by a very small degree, and are considerably less precise. That study provides anatan(float)
method, and that cleanly translates to atan2().- Parameters:
y
- y-component of the point to find the angle towards; note the parameter order is unusual by conventionx
- x-component of the point to find the angle towards; note the parameter order is unusual by convention- Returns:
- the angle to the given point, in radians as a float; ranges from
-PI
toPI
-
atan2Deg
public static float atan2Deg(float y, float x) Close approximation of the frequently-used trigonometric method atan2, using positive or negative degrees. Average error is ??? degrees; maximum error is ???. Takes y and x (in that unusual order) as floats, and returns the angle from the origin to that point in degrees.
Credit for this goes to the 1955 research study "Approximations for Digital Computers," by RAND Corporation. This is sheet 11's algorithm, which is the fourth-fastest and fourth-least precise. The algorithms on sheets 8-10 are faster, but only by a very small degree, and are considerably less precise. That study provides anatan(float)
method, and that cleanly translates to atan2Deg().- Parameters:
y
- y-component of the point to find the angle towards; note the parameter order is unusual by conventionx
- x-component of the point to find the angle towards; note the parameter order is unusual by convention- Returns:
- the angle to the given point, in degrees as a float; ranges from
-180
to180
-
atan2Deg360
public static float atan2Deg360(float y, float x) Close approximation of the frequently-used trigonometric method atan2, using non-negative degrees only. Average error is ??? degrees; maximum error is ???. Takes y and x (in that unusual order) as floats, and returns the angle from the origin to that point in degrees.
Credit for this goes to the 1955 research study "Approximations for Digital Computers," by RAND Corporation. This is sheet 11's algorithm, which is the fourth-fastest and fourth-least precise. The algorithms on sheets 8-10 are faster, but only by a very small degree, and are considerably less precise. That study provides anatan(float)
method, and that cleanly translates to atan2Deg360().- Parameters:
y
- y-component of the point to find the angle towards; note the parameter order is unusual by conventionx
- x-component of the point to find the angle towards; note the parameter order is unusual by convention- Returns:
- the angle to the given point, in degrees as a float; ranges from
0
to360
-
atan2Turns
public static float atan2Turns(float y, float x) Close approximation of the frequently-used trigonometric method atan2, using non-negative turns only. Average error is ??? degrees; maximum error is ???. Takes y and x (in that unusual order) as floats, and returns the angle from the origin to that point in turns.
Credit for this goes to the 1955 research study "Approximations for Digital Computers," by RAND Corporation. This is sheet 11's algorithm, which is the fourth-fastest and fourth-least precise. The algorithms on sheets 8-10 are faster, but only by a very small degree, and are considerably less precise. That study provides anatan(float)
method, and that cleanly translates to atan2Turns().- Parameters:
y
- y-component of the point to find the angle towards; note the parameter order is unusual by conventionx
- x-component of the point to find the angle towards; note the parameter order is unusual by convention- Returns:
- the angle to the given point, in turns as a float; ranges from
0.0f
to1.0f
-
asin
public static float asin(float a) Returns arcsine in radians; less accurate than Math.asin but may be faster. Average error of 0.000028447 radians (0.0016298931 degrees), largest error of 0.000067592 radians (0.0038727364 degrees). This implementation does not return NaN if given an out-of-range input (Math.asin does return NaN), unless the input is NaN.- Parameters:
a
- asin is defined only when a is between -1f and 1f, inclusive- Returns:
- between
-HALF_PI
andHALF_PI
when a is in the defined range
-
asinDeg
public static float asinDeg(float a) Returns arcsine in degrees. This implementation does not return NaN if given an out-of-range input (Math.asin does return NaN), unless the input is NaN.- Parameters:
a
- asin is defined only when a is between -1f and 1f, inclusive- Returns:
- between
-90
and90
when a is in the defined range
-
asinTurns
public static float asinTurns(float a) Returns arcsine in turns. This implementation does not return NaN if given an out-of-range input (Math.asin does return NaN), unless the input is NaN. Note that unlikeatan2Turns(float, float)
, this can return negative turn values.- Parameters:
a
- asin is defined only when a is between -1f and 1f, inclusive- Returns:
- between
-0.25
and0.25
when a is in the defined range
-
acos
public static float acos(float a) Returns arccosine in radians; less accurate than Math.acos but may be faster. Average error of 0.00002845 radians (0.0016300649 degrees), largest error of 0.000067548 radians (0.0038702153 degrees). This implementation does not return NaN if given an out-of-range input (Math.acos does return NaN), unless the input is NaN.- Parameters:
a
- acos is defined only when a is between -1f and 1f, inclusive- Returns:
- between
0
andPI
when a is in the defined range
-
acosDeg
public static float acosDeg(float a) Returns arccosine in degrees. This implementation does not return NaN if given an out-of-range input (Math.acos does return NaN), unless the input is NaN.- Parameters:
a
- acos is defined only when a is between -1f and 1f, inclusive- Returns:
- between
0
and180
when a is in the defined range
-
acosTurns
public static float acosTurns(float a) Returns arccosine in turns. This implementation does not return NaN if given an out-of-range input (Math.acos does return NaN), unless the input is NaN.- Parameters:
a
- acos is defined only when a is between -1f and 1f, inclusive- Returns:
- between
0
and0.5
when a is in the defined range
-
atan
public static float atan(float i) Arc tangent approximation with very low error, using an algorithm from the 1955 research study "Approximations for Digital Computers," by RAND Corporation (this is sheet 11's algorithm, which is the fourth-fastest and fourth-least precise). This method is usually about 4x faster thanMath.atan(double)
, but is somewhat less precise than Math's implementation. For finite inputs only, you may get a tiny speedup by usingatanUnchecked(double)
, but this method will be correct enough for infinite inputs, and atanUnchecked() will not be.- Parameters:
i
- an input to the inverse tangent function; any float is accepted- Returns:
- an output from the inverse tangent function in radians, from
-HALF_PI
toHALF_PI
inclusive - See Also:
-
atanDeg
public static float atanDeg(float i) Arc tangent approximation returning a value measured in positive or negative degrees, using an algorithm from the 1955 research study "Approximations for Digital Computers," by RAND Corporation (this is sheet 11's algorithm, which is the fourth-fastest and fourth-least precise). For finite inputs only, you may get a tiny speedup by usingatanUncheckedDeg(double)
, but this method will be correct enough for infinite inputs, and atanUnchecked() will not be.- Parameters:
i
- an input to the inverse tangent function; any float is accepted- Returns:
- an output from the inverse tangent function in degrees, from
-90
to90
inclusive - See Also:
-
atanTurns
public static float atanTurns(float i) Arc tangent approximation with very low error, using an algorithm from the 1955 research study "Approximations for Digital Computers," by RAND Corporation (this is sheet 11's algorithm, which is the fourth-fastest and fourth-least precise). For finite inputs only, you may get a tiny speedup by usingatanUncheckedTurns(double)
, but this method will be correct enough for infinite inputs, and atanUncheckedTurns() will not be.- Parameters:
i
- an input to the inverse tangent function; any float is accepted- Returns:
- an output from the inverse tangent function in turns, from
-HALF_PI
toHALF_PI
inclusive - See Also:
-