Numeric literals in Gosu
Gosu natively supports numeric literals of the most common numeric types, as well as a binary and hexadecimal syntax. Gosu uses the syntax to infer the type of the value.
For example:
- Gosu infers that the following variable has type
BigIntegerbecause the right side of the assignment uses a numeric literal4bi. That literal means “4, as a big integer”.var aBigInt = 4bi - Gosu infers that the following variables have type
Floatbecause the right side of the assignment uses a numeric literal with anfafter the number.var aFloat = 4f var anotherFloat = 4.0f
You can omit the suffix of a numeric literal if the type is declared explicitly such that no type inference is necessary.
Gosu does not support floating point hexadecimal literals.
The following table lists the suffix or prefix for different numeric, binary, and hexadecimal literals.
Type |
Suffix or prefix |
Examples |
|---|---|---|
|
suffix: |
|
|
|
suffix: |
|
|
none |
|
|
suffix: |
|
|
suffix: |
|
|
suffix: |
|
|
suffix: |
|
|
suffix: |
|
|
prefix: |
|
|
prefix: |
|
Scientific notation and floating point
Gosu supports the use of scientific notation to represent large or small numbers. Scientific notation represents a number as a coefficient, which is a number greater than or equal to 1 and less than 10, and a base, which is always 10.
For example, consider the number: 1.23 * 10 to the 11th power.
The number 1.23 is the coefficient. The
number 11 is the exponent, which means the power of 10. The base number
10 is always written in exponent form. Gosu represents the base number
as the letter e, which
stands for exponent.
You can use the scientific notation anywhere
you can use a float literal
or double literal.
Examples
var result1 = 9.2 * 3
var result2 = 2.057e3
print (result1)
print (typeof result1)
print (result2)
print (typeof result2)
This code prints:
27.599999999999998
double
2057.0
double
