Vikari Language Documentation


Available since:  v0.1.1.

Numeric Types

The Number type is the base type for all numeric types in Vikari. Number inherits from Value. Vikari supports the following six concrete numeric types:

Type NameExample Literal ValuesExplanation
Integer3, 22i, 314IA 32-bit integer.
Long3i, 22IA 64-bit integer.
BigInteger3b, 22BAn integer of arbitrarily large precision.
Float3.14f, 6.28FA 32-bit floating point number.
Double3.14, 6.18d, 1.618DA 64-bit floating point number.
BigDecimal3.14b, 6.28BA floating point number of arbitrarily large precision.

Integer literals without a postscript character denoting its type default to the Integer type. For example: 5. Floating-point literals without a postscript character denoting its type default to the Double type. For example: 5.0. Postscript characters of I and L denote a Integer and Long literal respectively. While postscript characters of F and D denote Float and Double respectively. While B without a decimal portion denotes a BigInteger literal, and B with a decimal portion denotes a BigDecimal.

Postscript characters of numeric literals can be either uppercase or lowercase.

Calculations of numeric values in arithmetic expressions are automatically promoted to the smallest possible enclosing type. This is based on both the types of the operands, and the resulting value. So if an Integer and a Long are added together, the result will be a Long value. Unless the result is too large or small to be encompassed by the type Long, in which case the result will then be a BigInteger.

The same is the case for floating point numbers. Also, all floating point numbers are considered to be a larger enclosing type then integer type numbers. So a BigInteger and a Float added together will result in a Float. Unless the result is too large or small for a Float type, in which case the result will be either Double or BigDecimal, respectively. But the upper and lower bounds of values for the Float and the Double types backing the type system used by Vikari are exceedingly difficult to reach by most ordinary calculations, in comparison to integer type limits.

The result of assigning numeric values to a variable depends on that variable's declared type. Variables of type AtonementCrystal, Value, or Number will always hold the exact result of the assigned value. But specifically typed variables of one of the six concrete types will automatically promote or demote the value to the appropriate type before assignment. This will truncate the value, if necessary.

Examples

The following demonstrates usage and properties of numeric types in Vikari.

~:Non concrete typed numeric variables.:~
variable << 0
crystal:AtonementCrystal << 1
value:Value << 2
number:Number << 3

~:Default literal values (without postfix characters).:~
foo:Integer << 42
bar:Double << 3.14

~:Literal values with postfix characters.:~
int:Integer << 4I
long:Long << 5L
bigInt:BigInteger << 6B
float:Float << 7.0F
double:Double << 8.0D
bigDec:BigDecimal << 9.0B

~:Literal values can use lowercase postfix characters.:~
int << 10i
long << 11l
bigInt << 12b
float << 13.0f
double << 14.0d
bigDec << 15.0b

~:BigInteger and BigDecimal literals can be of arbitrary size.:~
largeInteger:Number << 314159265358979323846264338327950288419716939937510B
largeDecimal:Number << 3.14159265358979323846264338327950288419716939937510B

~:Arithmetic promotes the result to the largest referenced type.:~
value << int + int ~:Result has type Integer.:~
value << int + long ~:Result has type Long.:~
value << int + float ~:Result has type Float.:~
value << float + double ~:Result has type Double.:~

~:Assignment uses the the variable's declared type.:~
v1:Integer << long ~:Result has type Integer.:~
v2:Integer << float ~:Result has type Integer.:~
v3:Long << double ~:Result has type Long.:~
v4:Double << long ~:Result has type Double.:~
v5:BigDecimal << bigDec ~:Result has type BigInteger:~
v6:BigInteger << bigInt ~:Result has type BigDecimal:~