Skip to content

Numbers

x: Int = 123
y: Float = 5.5

Cara supports the following number types on the language level:

Legend Int64 Int32 Int16 Int8 UInt64 UInt32 UInt16 UInt8 Float32 Float64 Int UInt Float Integral Decimal Defaults

Each of the numbered types denotes how wide it is: eg. UInt64 is 64 bits wide.

UInt types mean "unsigned int": a number without the sign bit.

Floats implement the IEEE 754 spec and specifically the binary32 and binary64 formats.

The types Int, UInt and Float are convenience aliases for Int64, UInt64 and Float64 respectively.

Numbers of different types are automatically promoted to the larger one when used together:

Promotion
x: Int   = -125
y: Float = 0.5
z = x * y       // -> z: Float = -62.5

You can use underscore separators:

Separators
x = 12_345_678
y = 1.250_025

z = 1_23_456_7 // no need to keep them 3 digits apart

Integers can be written in the binary (0b), octal (0o) and hexadecimal (0x) bases:

Other bases
bin = 0b0000_1111_1000_0010 // = 3970 in base 10
oct = 0o777                 // =  511 in base 10
hex = 0x11F8                // = 4600 in base 10

canBeNegative = -0b1101     // =  -13 in base 10

Floats can be written in the scientific notation:

Scientific notation
x =  123.45e5  //  123.45 * 10^5    = 12_345_000
y = -123.45e-5 // -123.45 * 10^(-5) = -0.0012345

Standard library number types

There are more number types in the standard library:

  • Complex numbers (TODO)
  • BigInteger (TODO)
  • BigDecimal (TODO)
  • Fraction (TODO)