Math

更新时间:
复制 MD 格式

The Math class in cava.lang provides mathematical functions for use in Cava scripts, including comparison, power, logarithmic, and trigonometric functions.

import cava.lang.Math;

All functions are static and can be called directly as Math.<function>(...).

Functions

Functions are grouped by category. Each function is overloaded by numeric type where applicable.

Comparison functions

SignatureDescription
static int abs(int v)Returns the absolute value of v as an int.
static long abs(long v)Returns the absolute value of v as a long.
static float abs(float v)Returns the absolute value of v as a float.
static double abs(double v)Returns the absolute value of v as a double.
static int max(int a, int b)Returns the greater of two int values.
static long max(long a, long b)Returns the greater of two long values.
static float max(float a, float b)Returns the greater of two float values.
static double max(double a, double b)Returns the greater of two double values.
static int min(int a, int b)Returns the smaller of two int values.
static long min(long a, long b)Returns the smaller of two long values.
static float min(float a, float b)Returns the smaller of two float values.
static double min(double a, double b)Returns the smaller of two double values.
static double random()Returns a random double in [0.0, 1.0).
static double random(long seed)Returns a seeded random double in [0.0, 1.0).

Power, exponential, and logarithmic functions

SignatureDescription
static double sqrt(double v)Returns the square root of v. Returns NAN if v < 0.
static double pow(double x, double y)Returns x raised to the power of y. Returns 0 if x is negative and y is a decimal.
static double ceil(double v)Returns the smallest integer >= v.
static double floor(double v)Returns the largest integer <= v.
static double exp(double v)Returns e raised to the power of v.
static double ln(double v)Returns the natural logarithm (base e) of v.
static double log(double v)Alias for ln(v). Returns the natural logarithm (base e) of v.
static double log2(double v)Returns the base-2 logarithm of v.
static double log10(double v)Returns the base-10 logarithm of v.

Trigonometric functions

All trigonometric functions accept and return values in radians.

SignatureDescription
static double sin(double v)Returns the sine of v.
static double cos(double v)Returns the cosine of v.
static double tan(double v)Returns the tangent of v.
static double asin(double v)Returns the arcsine of v, in radians.
static double acos(double v)Returns the arccosine of v, in radians.
static double atan(double v)Returns the arctangent of v, in radians.
static double atan2(double x, double y)Returns the angle θ (in radians) from the conversion of rectangular coordinates (x, y) to polar coordinates (r, θ).

Hyperbolic functions

SignatureDescription
static double sinh(double v)Returns the hyperbolic sine of v.
static double cosh(double v)Returns the hyperbolic cosine of v.
static double tanh(double v)Returns the hyperbolic tangent of v.
static double asinh(double v)Returns the hyperbolic arcsine of v.
static double acosh(double v)Returns the hyperbolic arccosine of v.
static double atanh(double v)Returns the hyperbolic arctangent of v.

Function details

abs

Returns the absolute value of the input. For a negative v, the result is -v. For a non-negative v, the result is v. Overloaded for int, long, float, and double.

Parameters

NameTypeDescription
vint | long | float | doubleThe input value.

Returns: The absolute value of v, in the same type as the input.

max

Compares two values and returns the greater one. Overloaded for int, long, float, and double.

Parameters

NameTypeDescription
aint | long | float | doubleThe first value to compare.
bint | long | float | doubleThe second value to compare.

Returns: The greater of a and b.

min

Compares two values and returns the smaller one. Overloaded for int, long, float, and double.

Parameters

NameTypeDescription
aint | long | float | doubleThe first value to compare.
bint | long | float | doubleThe second value to compare.

Returns: The smaller of a and b.

random

Returns a pseudorandom double in [0.0, 1.0) — inclusive of 0.0, exclusive of 1.0.

Parameters

NameTypeDescription
seedlong(Optional) The seed for the random number generator.

Returns: A double in [0.0, 1.0).

sqrt

Returns the square root of v.

Parameters

NameTypeDescription
vdoubleA non-negative value.

Returns: The square root of v. Returns NAN if v < 0.

pow

Returns x raised to the power of y.

Parameters

NameTypeDescription
xdoubleThe base.
ydoubleThe exponent.

Returns: The value of x^y. Returns 0 if x is negative and y is a decimal.

ceil

Returns the smallest integer value that is greater than or equal to v.

Parameters

NameTypeDescription
vdoubleThe input value.

Returns: A double representing the ceiling of v.

floor

Returns the largest integer value that is less than or equal to v.

Parameters

NameTypeDescription
vdoubleThe input value.

Returns: A double representing the floor of v.

ln

Returns the natural logarithm (base e) of v.

Parameters

NameTypeDescription
vdoubleThe input value.

Returns: The natural logarithm of v.

log

Alias for ln(v). Returns the natural logarithm (base e) of v.

Parameters

NameTypeDescription
vdoubleThe input value.

Returns: The natural logarithm of v.

log2

Returns the base-2 logarithm of v.

Parameters

NameTypeDescription
vdoubleThe input value.

Returns: The base-2 logarithm of v.

log10

Returns the base-10 (common) logarithm of v.

Parameters

NameTypeDescription
vdoubleThe input value.

Returns: The base-10 logarithm of v.

exp

Returns e raised to the power of v, where e is the mathematical constant approximately equal to 2.718.

Parameters

NameTypeDescription
vdoubleThe exponent.

Returns: The value of e^v.

sin

Returns the sine of v.

Parameters

NameTypeDescription
vdoubleAn angle in radians.

Returns: The sine of v.

cos

Returns the cosine of v.

Parameters

NameTypeDescription
vdoubleAn angle in radians.

Returns: The cosine of v.

tan

Returns the tangent of v.

Parameters

NameTypeDescription
vdoubleAn angle in radians.

Returns: The tangent of v.

asin

Returns the arcsine of v, in radians.

Parameters

NameTypeDescription
vdoubleThe input value.

Returns: The arcsine of v, in radians.

acos

Returns the arccosine of v, in radians.

Parameters

NameTypeDescription
vdoubleThe input value.

Returns: The arccosine of v, in radians.

atan

Returns the arctangent of v, in radians.

Parameters

NameTypeDescription
vdoubleThe input value.

Returns: The arctangent of v, in radians.

atan2

Returns the angle θ from the conversion of rectangular coordinates (x, y) to polar coordinates (r, θ).

Parameters

NameTypeDescription
xdoubleThe x coordinate.
ydoubleThe y coordinate.

Returns: The angle θ, in radians.

sinh

Returns the hyperbolic sine of v.

Parameters

NameTypeDescription
vdoubleThe input value.

Returns: The hyperbolic sine of v.

cosh

Returns the hyperbolic cosine of v.

Parameters

NameTypeDescription
vdoubleThe input value.

Returns: The hyperbolic cosine of v.

tanh

Returns the hyperbolic tangent of v.

Parameters

NameTypeDescription
vdoubleThe input value.

Returns: The hyperbolic tangent of v.

asinh

Returns the hyperbolic arcsine of v.

Parameters

NameTypeDescription
vdoubleThe input value.

Returns: The hyperbolic arcsine of v.

acosh

Returns the hyperbolic arccosine of v.

Parameters

NameTypeDescription
vdoubleThe input value.

Returns: The hyperbolic arccosine of v.

atanh

Returns the hyperbolic arctangent of v.

Parameters

NameTypeDescription
vdoubleThe input value.

Returns: The hyperbolic arctangent of v.

Example

The following example imports the Math class and uses log and max in a scoring expression.

import cava.lang.Math;

class Example {
    static int main() {
        double a = 100.0;
        double b = 20;
        double c = Math.log(a);
        double d = Math.max(b, c);
        return d;
    }
}