Numbers
Ghost has a single numeric type, number. There are no separate integer, float, or double types to declare, convert between, or keep straight.
0
1234
-5678
0.001
3.14159
12.34
-1.76
Whole Numbers and Decimals
Underneath the single type, a number holds either a 64-bit integer or a 64-bit float, and Ghost chooses as it goes:
- A literal without a decimal point or exponent is an integer.
- Arithmetic between two integers stays an integer — exactly, with no rounding, anywhere in the 64-bit range.
- Anything involving a decimal produces a decimal.
- Division always produces a decimal, even when it divides evenly:
10 / 4is2.5, and10 / 5is2.
Both are the same number type to your code — type() reports number for either, and they mix freely in arithmetic and comparisons.
console.log(2 + 2) // >> 4
console.log(2 + 2.5) // >> 4.5
console.log(10 / 4) // >> 2.5
Precision
Decimals are IEEE 754 binary floats, the standard 64-bit representation. It cannot express fractions like 0.1 exactly, and the tiny errors accumulate when you add many of them together:
value = 0
for (i = 0; i < 1000; i++) {
value = value + 0.01
}
console.log(value)
// >> 9.999999999999831
The same caution applies to comparing decimals. 0.1 + 0.2 == 0.3 is false; compare the difference against a small tolerance instead.
Scientific Notation
Numeric values can be represented in scientific notation by using e. This returns a value multiplied by the specified power of 10, and always produces a decimal.
1.1 // expected value: 1.1
1.1e0 // expected value: 1.1
1.1e1 // expected value: 11
1.1e2 // expected value: 110
1.1e3 // expected value: 1100
8e-2 // expected value: 0.08
Methods
Each of these mirrors its counterpart on the math module exactly, so n.pow(2) and math.pow(n, 2) agree for every input. Use whichever reads better where you are.
abs()
The abs() method returns the number's distance from zero. A whole number stays whole.
value = (-5).abs()
// expected value: 5
ceil()
The ceil() method returns the smallest whole number greater than or equal to the given number.
value = 3.2.ceil()
// expected value: 4
clamp()
The clamp() method pulls the number inside a range, returning the nearest of the two bounds when it falls outside.
console.log(7.clamp(1, 5)) // >> 5
console.log(3.clamp(1, 5)) // >> 3
It answers with one of the three values you gave it rather than a computed one, so clamping whole numbers leaves them whole. A low bound above the high bound is a value error.
floor()
The floor() method returns the largest whole number less than or equal to the given number.
value = 3.7.floor()
// expected value: 3
isEven()
The isEven() method reports whether the number is a whole, even one. A number with a fractional part is neither even nor odd.
console.log(4.isEven()) // >> true
isFinite()
The isFinite() method reports whether the number is neither infinite nor NaN. A whole number always is.
isInfinite()
The isInfinite() method reports whether the number is positive or negative infinity.
import "ghost:math"
console.log(math.infinity.isInfinite()) // >> true
isInteger()
The isInteger() method reports whether the number has no fractional part — true for every whole number, and for a decimal that happens to land on one.
console.log(4.isInteger()) // >> true
console.log(4.5.isInteger()) // >> false
isNaN()
The isNaN() method reports whether the number is the "not a number" value. Only a decimal can be.
import "ghost:math"
console.log(math.nan.isNaN()) // >> true
isNegative()
The isNegative() method reports whether the number is below zero.
isOdd()
The isOdd() method reports whether the number is a whole, odd one.
isPositive()
The isPositive() method reports whether the number is above zero.
isZero()
The isZero() method reports whether the number is exactly zero. Worth reaching for given that 0 is truthy in Ghost, so if (n) does not test this.
console.log(0.isZero()) // >> true
pow()
The pow() method raises the number to an exponent.
console.log(2.pow(10)) // >> 1024
console.log(2.5.pow(2)) // >> 6.25
A whole number raised to a whole, non-negative exponent stays exact and whole, so the result can be used as a list index.
round()
The round() method rounds the given number to the nearest whole number, or to the number of decimal places given as an argument.
value = 123.4.round()
// expected value: 123
value = 123.456.round(1)
// expected value: 123.5
toString()
The toString() method returns the given number as a string. Ghost will not concatenate a number onto a string on its own, so this is what you reach for when building a message.
value = 3.141592.toString()
// expected value: "3.141592"
console.log("pi is " + 3.14.toString())
// >> pi is 3.14
A template literal does the same job without the call, and is usually the tidier way to write it:
console.log(`pi is ${3.14}`)
// >> pi is 3.14
sqrt, lerp, the trigonometric functions, the statistics — lives on the math module and has to be imported: math.sqrt(16), with no 16.sqrt() counterpart.