Bitbybit Docs
    Preparing search index...

    Class MathBitByBit

    Arithmetic, rounding, ranges, random numbers and the trigonometric functions on plain numbers. The trigonometric functions take and give angles in radians; degToRad and radToDeg convert at the boundary, because almost every angle a user types is in degrees. The interpolation helpers (lerp, remap, ease, smoothstep, pingPong) are the building blocks of animation and parametric variation.

    Index

    Constructors

    basics

    • Adds two numbers.

      Example: 5 and 3 -> 8, -2 and 7 -> 5

      Parameters

      Returns number

      Their sum

      const sum = bitbybit.math.add({ first: 5, second: 3 });
      
    • Subtracts the second number from the first.

      Example: 10 and 3 -> 7, 5 and 8 -> -3

      Parameters

      • inputs: TwoNumbersDto

        The number to subtract from and the number to subtract

      Returns number

      Their difference

      const difference = bitbybit.math.subtract({ first: 10, second: 3 });
      
    • Multiplies two numbers.

      Example: 5 and 3 -> 15, -2 and 4 -> -8

      Parameters

      Returns number

      Their product

      const product = bitbybit.math.multiply({ first: 5, second: 3 });
      
    • Divides the first number by the second.

      Dividing by 0 gives Infinity, as in JavaScript. Example: 10 and 2 -> 5, 7 and 2 -> 3.5

      Parameters

      • inputs: TwoNumbersDto

        The number to divide and the number to divide by

      Returns number

      Their quotient

      const quotient = bitbybit.math.divide({ first: 7, second: 2 });
      
    • Raises the first number to the power of the second.

      Example: 2 to the 3 -> 8, 5 to the 2 -> 25, 10 to the -1 -> 0.1

      Parameters

      Returns number

      The power

      const cube = bitbybit.math.power({ first: 2, second: 3 });
      
    • Finds the square root of a number.

      A negative number gives NaN. Example: 9 -> 3, 2 -> 1.414

      Parameters

      Returns number

      The square root

    • Drops the sign of a number, so the result is never negative.

      Example: -5 -> 5, 3 -> 3, 0 -> 0

      Parameters

      Returns number

      The absolute value

    • Rounds a number to the nearest whole number; halves round up.

      Example: 3.7 -> 4, 2.3 -> 2, 5.5 -> 6

      Parameters

      Returns number

      The nearest whole number

    • Rounds a number down to the whole number below it.

      Example: 3.7 -> 3, -2.3 -> -3, 5 -> 5

      Parameters

      Returns number

      The whole number below

    • Rounds a number up to the whole number above it.

      Example: 3.2 -> 4, -2.8 -> -2, 5 -> 5

      Parameters

      Returns number

      The whole number above

    • Flips the sign of a number.

      Example: 5 -> -5, -3 -> 3, 0 -> 0

      Parameters

      Returns number

      The number with the opposite sign

    • Finds the natural logarithm of a number: the power e must be raised to for that number.

      Example: 2.718 -> about 1, 1 -> 0

      Parameters

      Returns number

      The natural logarithm

    • Finds the base-10 logarithm of a number: the power 10 must be raised to for that number.

      Example: 100 -> 2, 1000 -> 3, 10 -> 1

      Parameters

      Returns number

      The base-10 logarithm

    • Raises 10 to the power of a number.

      Example: 2 -> 100, 3 -> 1000, -1 -> 0.1

      Parameters

      Returns number

      10 to that power

    • Finds the sine of an angle given in radians.

      Example: 0 -> 0, pi/2 -> 1

      Parameters

      Returns number

      The sine, between -1 and 1

    • Finds the cosine of an angle given in radians.

      Example: 0 -> 1, pi -> -1

      Parameters

      Returns number

      The cosine, between -1 and 1

    • Finds the tangent of an angle given in radians.

      Example: 0 -> 0, pi/4 -> about 1

      Parameters

      Returns number

      The tangent

    • Finds the angle, in radians, whose sine is the given number.

      Example: 0 -> 0, 1 -> pi/2 (about 1.57)

      Parameters

      Returns number

      The angle in radians

    • Finds the angle, in radians, whose cosine is the given number.

      Example: 1 -> 0, -1 -> pi (about 3.14)

      Parameters

      Returns number

      The angle in radians

    • Finds the angle, in radians, whose tangent is the given number.

      Example: 0 -> 0, 1 -> pi/4 (about 0.785)

      Parameters

      Returns number

      The angle in radians, between -pi/2 and pi/2

    • Raises e, the base of the natural logarithm, to the power of a number.

      Example: 0 -> 1, 1 -> about 2.718, 2 -> about 7.389

      Parameters

      Returns number

      e to that power

    • Converts an angle from degrees to radians.

      Example: 180 -> pi (about 3.14159), 90 -> pi/2

      Parameters

      Returns number

      The angle in radians

    • Converts an angle from radians to degrees.

      Example: pi -> 180, pi/2 -> 90

      Parameters

      Returns number

      The angle in degrees

    create

    • Passes a number through unchanged, so a value can be given a name and reused.

      Example: 42 -> 42

      Parameters

      Returns number

      The same number

    generate

    • Gives a random number from 0 up to, but not including, 1.

      Example: 0.342, 0.891 or any other value in that range

      Returns number

      A random number between 0 and 1

    • Gives a random number between low and high.

      Example: low 0, high 10 -> 3.7, 8.2 or any other value between them

      Parameters

      Returns number

      A random number in the range

      const value = bitbybit.math.randomNumber({ low: 0, high: 10 });
      
    • Gives a list of random numbers between low and high.

      Example: low 0, high 10, count 3 -> [2.5, 7.1, 4.8]

      Parameters

      • inputs: RandomNumbersDto

        The low and high ends of the range and how many numbers to make

      Returns number[]

      The random numbers

      const values = bitbybit.math.randomNumbers({ low: 0, high: 10, count: 3 });
      
    • Gives the constant pi, the ratio of a circle's circumference to its diameter.

      Example: 3.141592653589793

      Returns number

      The number pi

    operations

    • Applies one arithmetic operation to two numbers: add, subtract, multiply, divide, power or modulus.

      The operation reads first then second: subtract gives first minus second, power gives first to the power of second. Example: 5 add 3 -> 8, 10 modulus 3 -> 1, 2 power 3 -> 8

      Parameters

      Returns number

      The result of the operation

      const result = bitbybit.math.twoNrOperation({ first: 2, second: 3, operation: Bit.Inputs.Math.mathTwoNrOperatorEnum.power });
      
    • Finds the remainder after dividing one number by another.

      The sign follows the first number, as it does in JavaScript. Example: 10 modulus 3 -> 1, 17 modulus 5 -> 2

      Parameters

      • inputs: ModulusDto

        The number to divide and the number to divide by

      Returns number

      The remainder

      const remainder = bitbybit.math.modulus({ number: 17, modulus: 5 });
      
    • Rounds a number to a given number of decimal places.

      Example: 1.32156 to 3 places -> 1.322

      Parameters

      Returns number

      The rounded number

      const rounded = bitbybit.math.roundToDecimals({ number: 1.32156, decimalPlaces: 3 });
      
    • Rounds a number to a given number of decimal places and drops the zeros at the end.

      As a number the result cannot carry trailing zeros anyway; the difference from roundToDecimals is that floating-point noise such as 1.320000001 is cleaned to 1.32. Example: 1.32156 to 3 places -> 1.322, 1.320000001 -> 1.32, 1.000 -> 1

      Parameters

      Returns number

      The rounded number

      const clean = bitbybit.math.roundAndRemoveTrailingZeros({ number: 1.320000001, decimalPlaces: 3 });
      
    • Applies one operation to a single number: absolute, negate, square root, rounding, logarithms, the trigonometric functions and their inverses, exponential, or a conversion between radians and degrees.

      The trigonometric functions work in radians. Example: sqrt of 5 -> 2.236, absolute of -3 -> 3

      Parameters

      Returns number

      The result of the operation

      const root = bitbybit.math.oneNrOperation({ number: 5, operation: Bit.Inputs.Math.mathOneNrOperatorEnum.sqrt });
      
    • Maps a number from one range onto another, keeping its relative position.

      A number outside the source range maps proportionally beyond the target range. Example: 5 from [0,10] to [0,100] -> 50, 0.5 from [0,1] to [-10,10] -> 0

      Parameters

      • inputs: RemapNumberDto

        The number, the range it is in and the range to map it to

      Returns number

      The number at the same relative position in the target range

      const percent = bitbybit.math.remap({ number: 5, fromLow: 0, fromHigh: 10, toLow: 0, toHigh: 100 });
      
    • Formats a number as text with a fixed number of decimal places, keeping trailing zeros.

      Example: 3.14159 with 2 places -> '3.14', 5 with 3 places -> '5.000'

      Parameters

      • inputs: ToFixedDto

        The number and how many decimal places to show

      Returns string

      The formatted text

      const label = bitbybit.math.toFixed({ number: 3.14159, decimalPlaces: 2 });
      
    • Maps a value from 0 to 1 onto the range min to max along an easing curve, so the result speeds up or slows down instead of changing evenly.

      An easeIn curve starts slowly, an easeOut curve ends slowly, an easeInOut curve does both. Example: 0.5 from [0,100] with easeInQuad -> 25

      Parameters

      • inputs: EaseDto

        The value between 0 and 1, the target range and the easing curve

      Returns number

      The eased value in the target range

      const eased = bitbybit.math.ease({ x: 0.5, min: 0, max: 100, ease: Bit.Inputs.Math.easeEnum.easeInQuad });
      
    • Keeps a number within a range: below min becomes min, above max becomes max.

      Example: 5 in [0,3] -> 3, -1 in [0,3] -> 0, 1.5 in [0,3] -> 1.5

      Parameters

      • inputs: ClampDto

        The number and the range to keep it in

      Returns number

      The number, limited to the range

      const limited = bitbybit.math.clamp({ number: 5, min: 0, max: 3 });
      
    • Blends from a start value to an end value by a fraction t: 0 gives the start, 1 the end, 0.5 the midpoint.

      A t outside 0 to 1 extrapolates past the ends. Example: 0 to 100 at 0.5 -> 50, 10 to 20 at 0.25 -> 12.5

      Parameters

      • inputs: LerpDto

        The start value, the end value and the fraction

      Returns number

      The blended value

      const mid = bitbybit.math.lerp({ start: 10, end: 20, t: 0.25 });
      
    • Finds where a value sits between a start and an end, as a fraction: the t that lerp would need to produce it.

      Example: 5 in [0,10] -> 0.5, 2.5 in [0,10] -> 0.25

      Parameters

      • inputs: InverseLerpDto

        The start value, the end value and the value to locate

      Returns number

      The fraction from start to end

      const fraction = bitbybit.math.inverseLerp({ start: 0, end: 10, value: 2.5 });
      
    • Turns a value from 0 to 1 into a smooth S-curve that starts and ends gently; the value is clamped to that range first.

      Example: 0 -> 0, 0.5 -> 0.5, 0.25 -> 0.156

      Parameters

      Returns number

      The smoothed value between 0 and 1

    • Tells the sign of a number: -1 when negative, 0 when zero, 1 when positive.

      Example: -5 -> -1, 0 -> 0, 3.14 -> 1

      Parameters

      Returns number

      -1, 0 or 1

    • Keeps the part of a number after the decimal point, measured up from the whole number below it, so the result is always from 0 up to 1.

      Example: 3.14 -> 0.14, -2.3 -> 0.7

      Parameters

      Returns number

      The fractional part, from 0 up to 1

    • Wraps a number into a range so it cycles round: past max it comes back in at min, and below min it comes back in at max.

      Useful for angles and repeating patterns; unlike a plain modulus it handles negative numbers. Example: 1.5 in [0,1) -> 0.5, -0.3 in [0,1) -> 0.7, 370 in [0,360) -> 10

      Parameters

      • inputs: WrapDto

        The number and the range to wrap it into

      Returns number

      The wrapped number, from min up to max

      const angle = bitbybit.math.wrap({ number: 370, min: 0, max: 360 });
      
    • Bounces a value back and forth between 0 and length as t grows: up to length, back down to 0, and again.

      Example: length 1 at t 0.5 -> 0.5, t 1 -> 1, t 1.5 -> 0.5, t 2 -> 0

      Parameters

      • inputs: PingPongDto

        The running value and the length to bounce within

      Returns number

      The bounced value between 0 and length

      const bounce = bitbybit.math.pingPong({ t: 1.5, length: 1 });
      
    • Moves a value toward a target by at most maxDelta, without overshooting it.

      Example: 0 toward 10 by 3 -> 3, 8 toward 10 by 3 -> 10

      Parameters

      • inputs: MoveTowardsDto

        The current value, the target and the largest step allowed

      Returns number

      The value after one step

      const next = bitbybit.math.moveTowards({ current: 8, target: 10, maxDelta: 3 });
      
    • Works out a simple arithmetic expression written as text: numbers, +, -, the multiplication sign, /, parentheses and spaces.

      The expression is parsed and computed by the library itself, never handed to the JavaScript engine to run, so it is safe with text a user typed. Example: '(3+2) times 4' written with the sign -> 20, '10/3' -> 3.3333

      Parameters

      Returns number

      The computed value

      const value = bitbybit.math.evalArithmetic({ expression: "(3 + 2) * 4" });