Skip to content
FutureBlitz

    Install

    Import std.math.scalar

    std.math.scalar

    Math on single numbers: signs, rounding, remainders, powers, logs, and trigonometry.

    The scalar Functions: signs, whole parts, remainders, divisors, the steps between two values, powers and logs, trig and its hyperbolic twins, and IEEE's checks, each in its arguments' type, for the number types it makes sense for. Powers are the ** operator. Angles are radians, so Deg(90) is 90 degrees, in radians.

    Examples

    
    Local zero = 0.0
    Expect(Abs(-3) == 3 && Sign(-2.5) == -1.0)
    Expect(Floor(2.7) == 2.0 && Ceil(2.1) == 3.0 && Round(2.6) == 3.0 && Trunc(-2.7) == -2.0)
    Expect(IsClose(Fract(2.75), 0.75) && Rem(7, 3) == 1 && Lcm(4, 6) == 12 && IntSqrt(17) == 4)
    Expect(SmoothStep(0.0, 1.0, 0.5) == 0.5 && PingPong(3.0, 2.0) == 1.0)
    Expect(MoveTowards(0.0, 10.0, 3.0) == 3.0 && InverseLerp(0.0, 10.0, 5.0) == 0.5)
    Expect(IsClose(Exp(Log(5.0)), 5.0) && Log2(8.0) == 3.0 && IsClose(Log10(1000.0), 3.0))
    Expect(IsClose(Cbrt(27.0), 3.0) && Exp2(3.0) == 8.0 && Fma(2.0, 3.0, 1.0) == 7.0)
    Expect(IsClose(Sin(Pi / 6), 0.5) && IsClose(Cos(Pi), -1.0) && IsClose(Tan(Pi / 4), 1.0))
    Expect(IsClose(Asin(1.0), Pi / 2) && IsClose(Acos(1.0), 0.0) && IsClose(Atan(1.0), Pi / 4))
    Expect(IsClose(Atan2(1.0, 1.0), Pi / 4) && Sinh(0.0) == 0.0 && Cosh(0.0) == 1.0)
    Expect(Tanh(0.0) == 0.0 && Asinh(0.0) == 0.0 && Acosh(1.0) == 0.0 && Atanh(0.0) == 0.0)
    Expect(IsNaN(Sqrt(-1.0)) && !IsFinite(1.0 / zero) && CopySign(2.0, -1.0) == -2.0)
    Expect(IsClose(ToDegrees(Pi), 180.0) && IsClose(ToRadians(180), Pi) && Rad(2) == 2.0)

    std/math/scalar.fb

    Functions

    Abs function

    @PanicsAtCaller Public Function Abs<T>(value: T): T

    The value without its sign: 0 for -0, and NaN for NaN. An integer's lowest value, which has none in its type, overflows.

    std/math/scalar.fb, line 35

    Sign function

    Public Function Sign<T>(value: T): T

    1 for a value above 0, -1 for one below, and the value itself for 0 or NaN.

    std/math/scalar.fb, line 42

    Floor function

    Public Function Floor<T: FloatingPoint>(value: T): T

    The largest whole number that isn't above the value.

    std/math/scalar.fb, line 50

    Ceil function

    Public Function Ceil<T: FloatingPoint>(value: T): T

    The smallest whole number that isn't below the value.

    std/math/scalar.fb, line 53

    Round function

    Public Function Round<T: FloatingPoint>(value: T): T

    The nearest whole number, with a value halfway between two going away from 0: 2.5 is 3, and -2.5 is -3.

    std/math/scalar.fb, line 57

    Trunc function

    Public Function Trunc<T: FloatingPoint>(value: T): T

    The whole part, toward 0: Trunc(-2.7) is -2.

    std/math/scalar.fb, line 60

    Fract function

    Public Function Fract<T>(value: T): T

    What Floor takes away, from 0 to 1: Fract(-0.25) is 0.75. A negative value too close to a whole number for the type to hold the difference gives 1.

    std/math/scalar.fb, line 64

    Mod function

    @PanicsAtCaller Public Function Mod<T>(a: T, b: T): T

    The remainder of a divided by b with the sign of b, as dividing and rounding down leaves it: Mod(-1, 3) is 2, and Mod(1, -3) is -2. A b of 0 panics.

    Examples

    Expect(Mod(-1, 3) == 2)
    Expect(Rem(-1, 3) == -1)

    std/math/scalar.fb, line 86

    Mod function

    Public Function Mod<T>(a: T, b: T): T

    The remainder of a divided by b with the sign of b, as dividing and rounding down leaves it: Mod(-1.5, 4.0) is 2.5, and a 0 has b's sign. A b of 0 gives NaN. A remainder too small beside b for their sum to be below it gives 0, which is as near, a whole b away.

    std/math/scalar.fb, line 96

    Rem function

    @PanicsAtCaller Public Function Rem<T>(a: T, b: T): T

    The remainder of a divided by b with the sign of a, as % gives it: Rem(-1, 3) is -1. A b of 0 panics.

    std/math/scalar.fb, line 107

    Rem function

    Public Function Rem<T: FloatingPoint>(a: T, b: T): T

    The remainder of a divided by b with the sign of a, exactly, as C's fmod gives it: Rem(-1.5, 4.0) is -1.5. A b of 0 gives NaN.

    std/math/scalar.fb, line 115

    Gcd function

    @PanicsAtCaller Public Function Gcd<T>(a: T, b: T): T

    The greatest common divisor of the two, which isn't negative: Gcd(12, -18) is 6, and Gcd(0, 0) is 0. One too large for T overflows.

    Examples

    Expect(Gcd(12, 18) == 6 && Lcm(4, 6) == 12)

    std/math/scalar.fb, line 125

    Lcm function

    @PanicsAtCaller Public Function Lcm<T>(a: T, b: T): T

    The least common multiple of the two, which isn't negative: Lcm(4, 6) is 12, and 0 when either is 0. One too large for T overflows.

    std/math/scalar.fb, line 140

    IntSqrt function

    @PanicsAtCaller Public Function IntSqrt<T>(value: T): T

    The largest integer whose square isn't above the value: IntSqrt(10) is 3. A negative value panics.

    std/math/scalar.fb, line 149

    Lerp function

    Public Function Lerp<T>(a: T, b: T, t: T): T

    The value t of the way from a to b: a at 0, b itself at 1, though b - a rounds, and past them outside 0 to 1.

    Examples

    Expect(Lerp(0.0, 10.0, 0.25) == 2.5)
    Expect(InverseLerp(0.0, 10.0, 2.5) == 0.25)
    Expect(Remap(5.0, 0.0, 10.0, 100.0, 200.0) == 150.0)

    std/math/scalar.fb, line 182

    InverseLerp function

    Public Function InverseLerp<T>(a: T, b: T, value: T): T

    How far of the way from a to b the value is, as Lerp's t: 0 at a, 1 at b. With a equal to b, it's 0.

    std/math/scalar.fb, line 189

    Remap function

    Public Function Remap<T>(value: T, fromLow: T, fromHigh: T, toLow: T, toHigh: T): T

    The value moved from one range to another, as far through toLow to toHigh as it is through fromLow to fromHigh: Remap(5, 0, 10, 100, 200) is 150.

    std/math/scalar.fb, line 197

    SmoothStep function

    Public Function SmoothStep<T>(low: T, high: T, value: T): T

    0 below low and 1 above high, and a smooth curve between, as GLSL's smoothstep: with low equal to high, 0 below it and 1 from it up.

    std/math/scalar.fb, line 204

    Wrap function

    Public Function Wrap<T>(value: T, low: T, high: T): T

    The value wrapped into the range from low to high, high left out, as an angle wraps into a turn: Wrap(370, 0, 360) is 10. With low equal to high, it's low. The distances are worked out in a UInt64, so a range of any width, in any of the eight types, can't overflow.

    Examples

    Expect(Wrap(370, 0, 360) == 10)
    Expect(Wrap(-30, 0, 360) == 330)

    std/math/scalar.fb, line 228

    Wrap function

    Public Function Wrap<T>(value: T, low: T, high: T): T

    The value wrapped into the range from low to high, high left out, as an angle wraps into a turn: Wrap(370.0, 0.0, 360.0) is 10. With low equal to high, it's low. One that rounds to high gives low, which is as near, a turn away.

    std/math/scalar.fb, line 244

    PingPong function

    @PanicsAtCaller Public Function PingPong<T>(value: T, length: T): T

    The value going from 0 up to length and back down, over and over: PingPong(3, 2) is 1. A negative length panics. It's worked out in a UInt64, by whole lengths, so no length of any of the eight types overflows.

    std/math/scalar.fb, line 255

    PingPong function

    @PanicsAtCaller Public Function PingPong<T>(value: T, length: T): T

    The value going from 0 up to length and back down, over and over: PingPong(3.0, 2.0) is 1. A negative or NaN length panics.

    std/math/scalar.fb, line 275

    MoveTowards function

    Public Function MoveTowards<T>(current: T, target: T, maxDelta: T): T

    Current moved toward target by maxDelta, or to target when it's no further: a negative maxDelta moves it away.

    std/math/scalar.fb, line 287

    IsClose function

    Public Function IsClose<T>(a: T, b: T, tolerance: T): Bool

    Whether a and b are no further apart than tolerance.

    std/math/scalar.fb, line 294

    IsClose function

    Public Function IsClose<T>(a: T, b: T): Bool

    Whether a and b are equal but for rounding: no further apart than 0.00001 of the larger one's size, or of 1 near 0. An infinity is close only to itself, and NaN to nothing.

    Examples

    Expect(IsClose(0.1 + 0.2, 0.3))
    Expect(!IsClose(1.0, 1.1))

    std/math/scalar.fb, line 308

    Sqrt function

    Public Function Sqrt<T: FloatingPoint>(value: T): T

    The square root. A negative value's is NaN.

    Examples

    Expect(Sqrt(16.0) == 4.0)
    Expect(IsClose(Sin(Deg(30)), 0.5))
    Expect(IsClose(Atan2(1.0, 1.0), Deg(45)))

    std/math/scalar.fb, line 325

    Cbrt function

    Public Function Cbrt<T: FloatingPoint>(value: T): T

    The cube root, negative for a negative value.

    std/math/scalar.fb, line 328

    Exp function

    Public Function Exp<T: FloatingPoint>(value: T): T

    E to the power of the value.

    std/math/scalar.fb, line 331

    Exp2 function

    Public Function Exp2<T: FloatingPoint>(value: T): T

    2 to the power of the value.

    std/math/scalar.fb, line 334

    Log function

    Public Function Log<T: FloatingPoint>(value: T): T

    The natural logarithm, of base E. 0's is -Inf, and a negative value's NaN.

    std/math/scalar.fb, line 337

    Log function

    Public Function Log<T>(value: T, base: T): T

    The logarithm of the base: Log(8.0, 2.0) is 3.

    std/math/scalar.fb, line 340

    Log2 function

    Public Function Log2<T: FloatingPoint>(value: T): T

    The logarithm of base 2.

    std/math/scalar.fb, line 346

    Log10 function

    Public Function Log10<T: FloatingPoint>(value: T): T

    The logarithm of base 10.

    std/math/scalar.fb, line 349

    Sin function

    Public Function Sin<T: FloatingPoint>(angle: T): T

    The sine of an angle in radians.

    std/math/scalar.fb, line 352

    Cos function

    Public Function Cos<T: FloatingPoint>(angle: T): T

    The cosine of an angle in radians.

    std/math/scalar.fb, line 355

    Tan function

    Public Function Tan<T: FloatingPoint>(angle: T): T

    The tangent of an angle in radians.

    std/math/scalar.fb, line 358

    Asin function

    Public Function Asin<T: FloatingPoint>(value: T): T

    The angle whose sine is the value, from -Pi / 2 to Pi / 2. Outside -1 to 1, it's NaN.

    std/math/scalar.fb, line 361

    Acos function

    Public Function Acos<T: FloatingPoint>(value: T): T

    The angle whose cosine is the value, from 0 to Pi. Outside -1 to 1, it's NaN.

    std/math/scalar.fb, line 364

    Atan function

    Public Function Atan<T: FloatingPoint>(value: T): T

    The angle whose tangent is the value, from -Pi / 2 to Pi / 2.

    std/math/scalar.fb, line 367

    Atan2 function

    Public Function Atan2<T: FloatingPoint>(y: T, x: T): T

    The angle from the X axis to the point (x, y), from -Pi to Pi.

    std/math/scalar.fb, line 370

    Sinh function

    Public Function Sinh<T: FloatingPoint>(value: T): T

    The hyperbolic sine.

    std/math/scalar.fb, line 373

    Cosh function

    Public Function Cosh<T: FloatingPoint>(value: T): T

    The hyperbolic cosine.

    std/math/scalar.fb, line 376

    Tanh function

    Public Function Tanh<T: FloatingPoint>(value: T): T

    The hyperbolic tangent.

    std/math/scalar.fb, line 379

    Asinh function

    Public Function Asinh<T: FloatingPoint>(value: T): T

    The value whose hyperbolic sine this is.

    std/math/scalar.fb, line 382

    Acosh function

    Public Function Acosh<T: FloatingPoint>(value: T): T

    The value, not negative, whose hyperbolic cosine this is. Below 1, it's NaN.

    std/math/scalar.fb, line 385

    Atanh function

    Public Function Atanh<T: FloatingPoint>(value: T): T

    The value whose hyperbolic tangent this is. Outside -1 to 1, it's NaN.

    std/math/scalar.fb, line 388

    IsNaN function

    Public Function IsNaN<T>(value: T): Bool

    Whether the value is NaN, the one value that isn't equal to itself.

    std/math/scalar.fb, line 391

    IsFinite function

    Public Function IsFinite<T>(value: T): Bool

    Whether the value is a number, neither infinite nor NaN.

    std/math/scalar.fb, line 397

    CopySign function

    Public Function CopySign<T: FloatingPoint>(magnitude: T, sign: T): T

    The magnitude with the sign of sign, -0 and NaN's included: CopySign(2.0, -0.0) is -2.

    std/math/scalar.fb, line 403

    Fma function

    Public Function Fma<T: FloatingPoint>(a: T, b: T, c: T): T

    a * b + c, rounded once, at the end.

    std/math/scalar.fb, line 408

    Deg function

    Public Function Deg(degrees: Int): Float

    The angle of so many degrees, in radians, the unit of every angle in std.math: Deg(90) is Pi / 2.

    std/math/scalar.fb, line 412

    Deg function

    Public Function Deg<T: FloatingPoint>(degrees: T): T

    The angle of so many degrees, in radians, the unit of every angle in std.math: Deg(22.5) is Pi / 8.

    std/math/scalar.fb, line 416

    Rad function

    Public Function Rad(radians: Int): Float

    The angle of so many radians, which is the number itself, for code that says its units, as Deg does.

    std/math/scalar.fb, line 422

    Rad function

    Public Function Rad<T: FloatingPoint>(radians: T): T

    The angle of so many radians, which is the number itself, for code that says its units, as Deg does.

    std/math/scalar.fb, line 426

    ToRadians function

    Public Function ToRadians(degrees: Int): Float

    The angle of so many degrees, in radians, as Deg gives it.

    std/math/scalar.fb, line 429

    ToRadians function

    Public Function ToRadians<T: FloatingPoint>(degrees: T): T

    The angle of so many degrees, in radians, as Deg gives it.

    std/math/scalar.fb, line 432

    ToDegrees function

    Public Function ToDegrees(radians: Int): Float

    The angle of so many radians, in degrees: ToDegrees(1) is about 57.3.

    std/math/scalar.fb, line 435

    ToDegrees function

    Public Function ToDegrees<T: FloatingPoint>(radians: T): T

    The angle in radians, in degrees: ToDegrees(Pi) is 180.

    std/math/scalar.fb, line 438

    Report an issue with this page or with FutureBlitz on GitHub.