• Number Type - num

    The num type is a special type that represents a number (both floating point and integer) in the Kipper language. This datatype may always be used with the standard mathematical expressions.

    Order or evaluation

    When working with numbers watch out for the order of evaluations of arithmetic expressions. Kipper will interpret mathematical calculations as defined in the standard math conventions i.e. multiplications, divisions and power-to operations have a higher priority than additions and subtractions.

    Simple example:

    var result: num = 4 + 4 * 5; // 4 + (4 * 5) -> 24

    These are also defined in the table of operator precedence.

    Octal, Hex and Binary numbers

    Besides the regular Base-10 numbers, you may also use Hex, Octal and Binary numbers, as long as they are prefixed with the correct identifier:

    • Binary numbers are prefixed with 0b:
      var bytes: num = 0b11111010; // -> 250
    • Hexadecimal numbers are prefixed with 0x:
      var hex: num = 0xffa2; // -> 65442
    • Octal numbers are prefixed with 0o:
      var octal: num = 0o347; // -> 231

    Note that the numbers stored in the variables are always stored as Base-10 numbers, so converting them to a string will always result in a Base-10 number.

    This also means that you may mix different number types together, as long as they are prefixed with the correct identifier:

    var octal: num = 0o347 + 150 + 0xffa2; // -> 65823

    Safe Number Range

    Due to the fact that Kipper only compiles to TypeScript or JavaScript, the underlying number type will almost always be a 64-bit floating point number. This means that the maximum safe number range is the same as in JavaScript, which is Number.MAX_SAFE_INTEGER.

    Over this limit, the numbers will start to lose precision, as shown in the following example:

    var num1: num = 9007199254740991; // -> 9007199254740991
    var num2: num = 9007199254740992; // -> 9007199254740992
    var num3: num = 9007199254740993; // -> 9007199254740992

    Performing equality checks now causes the following problem:

    var isEqual = num3 === 9007199254740993; // -> true
    print(isEqual as str) // -> true
    
    isEqual: bool = num3 === num2; // -> true (but should be false)
    print(isEqual as str) // -> true
    
    isEqual = num3 === 9007199254740992; // -> true (but should be false)
    print(isEqual as str) // -> false

    This is due to the fact that the numbers are stored as floating point numbers, which are not able to represent the number with full precision.

    Note

    Kipper is at the stage of writing with the current version still under heavy development. This means that the implementation of types such as BigInt may take some time.

    This also includes global functions to check whether a number is within the safe range or not.

    Examples

    Simple integer variable definitions

    var var1: num = 400;

    Simple floating point variable definitions

    var var2: num = 0.43493;

    Calculations using floating point and integer numbers

    var var3: num = var2 + var1; // -> 400.43493

    Arithmetic Expressions

    The following arithmetic expressions are supported (as also shown in arithmetical expressions):

    • Addition:
      var var1: num = 400.3 + 26.3; // -> 426.6
    • Subtraction:
      var var2: num = 87 - 2.5; // -> 84.5
    • Multiplication:
      var var3: num = 2.4 * 5; // -> 12
    • Division:
      var var4: num = 25 / 4; // -> 6.25
    • Power to:
      var var5: num = 2 ** 8; // -> 256
    • Rest of Divide (Modulo):
      var var6: num = 51 % 10; // -> 1