Math

Floating-point functions work on doubles. A float or integer argument is converted.

Abs ✅

Result.d = Abs(Number.d)

The value without its sign.

Sign ✅

Result = Sign(Number.d)

1 for a positive number, −1 for a negative one, 0 for 0.

Int ✅

Result = Int(Number.d)

The whole part, without rounding: Int(7.9) is 7, Int(-7.9) is −7. (Storing a float in an integer variable rounds instead.) IntQ is the same.

Round ✅

Result.d = Round(Number.d, Mode)

Rounds to a whole number. Mode is #PB_Round_Down, #PB_Round_Up or #PB_Round_Nearest (.5 rounds away from zero).

Sqr ✅

Result.d = Sqr(Number.d)

The square root.

Pow ✅

Result.d = Pow(Number.d, Power.d)

Mod ✅

Result.d = Mod(Number.d, Divisor.d)

The remainder of a floating-point division. For integers, use the % operator.

Log, Log10, Exp ✅

Result.d = Log(Number.d)
Result.d = Log10(Number.d)
Result.d = Exp(Number.d)

Natural logarithm, base-10 logarithm, and e to the power of the number.

Sin, Cos, Tan, ASin, ACos, ATan ✅

Result.d = Sin(Angle.d)

Angles are in radians.

ATan2 ✅

Result.d = ATan2(X.d, Y.d)

The angle of the direction (X, Y), from −π to π. The arguments are X first, unlike C's atan2.

SinH, CosH, TanH ✅

Hyperbolic functions.

ASinH, ACosH, ATanH ✅

The inverse hyperbolic functions.

Infinity, NaN, IsInfinity, IsNaN ✅

Result.d = Infinity()
Result.d = NaN()
Result = IsInfinity(Value.d)
Result = IsNaN(Value.d)

Infinity() and NaN() return those special doubles (-Infinity() is negative infinity). IsInfinity returns 1 for either infinity, and IsNaN 1 for "not a number". A NaN is never equal to anything, itself included, so test with IsNaN.

OpenConsole()
big.d = Infinity()
odd.d = NaN()
PrintN(Str(IsInfinity(big)) + Str(IsInfinity(-big)) + Str(IsInfinity(1e300)) + " " + Str(IsNaN(odd)) + Str(IsNaN(big)))
PrintN(StrD(ASinH(1), 6) + " " + StrD(ACosH(2), 6) + " " + StrD(ATanH(0.5), 6))

Output:

110 10
0.881374 1.316958 0.549306

Degree, Radian ✅

Result.d = Degree(Angle.d)
Result.d = Radian(Angle.d)

Convert radians to degrees and back.

OpenConsole()
PrintN(StrD(Sqr(2), 4) + " " + StrD(Pow(2, 10)) + " " + StrD(Abs(-3.5)) + " " + Str(Sign(-7)))
PrintN(Str(Int(7.9)) + " " + Str(Int(-7.9)) + " " + StrD(Round(2.5, #PB_Round_Nearest)) + " " + StrD(Round(2.5, #PB_Round_Down)) + " " + StrD(Round(2.1, #PB_Round_Up)))
PrintN(StrD(Mod(10, 3)) + " " + StrD(Degree(#PI), 1) + " " + StrD(Sin(Radian(30)), 4) + " " + StrD(ATan2(1, 1), 4))

Output:

1.4142 1024 3.5 -1
7 -7 3 2 3
1 180.0 0.5000 0.7854

Random ✅

Result = Random(Maximum [, Minimum])

A whole number from Minimum (default 0) to Maximum, both included.

RandomData ✅

RandomData(*Buffer, Length)

Fills Length bytes at *Buffer with random bytes from the same generator as Random.

RandomSeed ✅

RandomSeed(Value)

Starts the sequence Random gives from a fixed point, so the same seed gives the same numbers. Without it, the sequence starts from the time the program started.

OpenConsole()
RandomSeed(42)
ok = 1
For i = 1 To 1000
  r = Random(6, 1)
  If r < 1 Or r > 6
    ok = 0
  EndIf
Next
PrintN(Str(ok))
RandomSeed(7)
a = Random(1000000)
RandomSeed(7)
PrintN(Str(Bool(a = Random(1000000))))

Output:

1
1

Notes. Random is a fast generator (xorshift64*), fine for games and simulations but not for security. The sequence for a given seed isn't the same as PureBasic's.

Constants

#PI (3.14159…) and #E (2.71828…).

Not yet available 🔜

The Crypt random functions.