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 BigInteger because the right side of the assignment uses a numeric literal 4bi. That literal means “4, as a big integer”.
    var aBigInt = 4bi
  • Gosu infers that the following variables have type Float because the right side of the assignment uses a numeric literal with an f after 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

byte

suffix: b or B

var aByte = 4b

short

suffix: as short

var aShort = 4 as short

int

none

var anInt = 4

long

suffix: l (lowercase L) or L

var aLong = 4L

float

suffix: f or F

var f1 = 4f

var f2 = 4.0f

var f3 = 4.0e3

double

suffix: d or D

var aDouble = 4d

BigInteger

suffix: bi or BI

var aBigInt = 4bi

BigDecimal

suffix: bd or BD

var aBigD = 4bd

var anotherBigD = 4.0bd

int as binary or mask

prefix: 0b or 0B

var maskVal1 = 0b0001  // 0 and 1 only

var maskVal2 = 0b0010

var maskVal3 = 0b0100

int as hexadecimal

prefix: 0x or 0X

var aColor = 0xFFFF // 0 through F only

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