Numbers#
Xarpite supports handling numeric values.
Overview#
Numeric Types#
There are two numeric types: 32-bit integers INT and 64-bit floating-point numbers DOUBLE.
Distinction from Strings Representing Numbers#
In Xarpite, numbers and strings representing numbers are strictly distinguished, affecting the behavior of operators and functions.
$ xa '123 + 1'
# 124
$ xa '"123" + 1'
# 1231
To convert a number to a string, use the stringification operator &value.
The reverse is the numeric conversion operator +value.
$ xa '&123 + 1'
# 1231
$ xa '+"123" + 1'
# 124
Numeric Literals#
123: Integer Literal#
A sequence of one or more digits becomes an integer literal.
$ xa '123'
# 123
Even digit sequences starting with 0 are always interpreted as decimal.
$ xa '00123'
# 123
H#123abc: Hexadecimal Integer Literal#
You can write hexadecimal numbers following H#.
The H# part must be uppercase, but the hexadecimal part is case-insensitive.
$ xa 'H#FF'
# 255
1.23: Floating-Point Number Literal#
You can write real numbers in the form “integer part . decimal part”.
Representation and operations with real numbers involve rounding errors.
$ xa '1.5'
# 1.5
Numeric Literal Separators#
All numeric literals can include any number of _ characters at any position except the beginning of the digit sequence.
The _ characters are simply ignored.
This allows large numbers to be written more readably.
$ xa '1_000_000'
# 1000000
$ xa '1__2__3__'
# 123
$ xa 'H#FF_FF'
# 65535
$ xa '1_000.5'
# 1000.5
$ xa '1.5_0_0'
# 1.5
Numeric Conversion#
There are operators and functions to convert values to numbers.
The behavior of numeric conversion varies depending on the type of the value.
| Value Type | Numeric Conversion Result |
|---|---|
| NULL | 0 |
| Number | The number itself |
| Boolean | 1 for TRUE, 0 for FALSE |
| String | Result of parsing as a number |
| Stream | Sum of all elements |
+value: Numeric Conversion#
The prefix + operator performs numeric conversion of a value.
The main purpose is to convert numeric data represented as strings into internal numeric values.
$ xa ' "+123"'
# +123
$ xa '+"+123"'
# 123
Numeric conversion references the +_ method of the value.
You can change the numeric conversion process by overriding the +_ method of an object.
$ xa '+{`+_`: this -> this.value * 2}{value: 100}'
# 200
-value: Negative Numeric Conversion#
The prefix - operator is similar to the numeric conversion operator, but negates the value.
This operator simultaneously performs “converting an arbitrary value to a number” and “negating the number”.
$ xa '-"123"'
# -123
$ xa '-(100 + 20 + 3)'
# -123
TO_NUMBER: Numeric Conversion#
TO_NUMBER(value: VALUE): NUMBER
Converts a value to a number.
It behaves the same as the +value operator.
$ xa 'TO_NUMBER("123")'
# 123
$ xa '1, 2, 3 >> TO_NUMBER'
# 6
$ xa 'TO_NUMBER(NULL)'
# 0
Addition/Subtraction Operators#
Addition/subtraction operators consist of operators that perform addition/subtraction-like operations.
Addition/subtraction operators have left-associative binding.
number + number: Addition#
The addition operator adds two values.
$ xa '1 + 2'
# 3
The addition operator behaves differently depending on the type of the left operand.
| Left Type | Behavior |
|---|---|
| Number | Returns the sum of left and right |
| String | Returns a string concatenating left and right |
| Array | Generates an array concatenating left and right |
| Object | Generates an object with right entries assigned to left object |
Overriding Addition Operation#
You can customize the addition operation of an object by implementing the addition method _+_.
$ xa '
Obj := {
`_+_`: this, other -> this.value + other.value
}
Obj{value: 100} + Obj{value: 23}
'
# 123
number - number: Subtraction#
The subtraction operator subtracts two values.
$ xa '3 - 1'
# 2
Like the addition operator, the subtraction operator can be overridden.
$ xa '
Obj := {
`_-_`: this, other -> this.value - other.value
}
Obj{value: 123} - Obj{value: 23}
'
# 100
Multiplication/Division Operators#
Multiplication/division operators consist of operators that perform multiplication/division-like operations.
Multiplication/division operators have left-associative binding.
number * number: Multiplication#
The multiplication operator multiplies two values.
$ xa '2 * 3'
# 6
Multiplication of strings repeats that string.
$ xa '"abc" * 3'
# abcabcabc
Multiplication of arrays repeats that array.
$ xa '[1, 2, 3] * 3'
# [1;2;3;1;2;3;1;2;3]
number / number: Division#
The division operator divides two values.
$ xa '6 / 3'
# 2.0
Even division of integers returns the result as a floating-point number.
$ xa '7 / 4'
# 1.75
number % number: Modulo#
The modulo operator divides two values and returns the remainder.
$ xa '7 % 4'
# 3
It can also handle decimals.
$ xa '1.75 % 0.5'
# 0.25
integer %% integer: Divisibility#
Returns whether the left value is divisible by the right value.
$ xa '7 %% 4'
# FALSE
It can also handle decimals.
$ xa '1.5 %% 0.5'
# TRUE
$ xa '1.75 %% 0.5'
# FALSE
integer !%% integer: Non-Divisibility#
Returns whether the left value is not divisible by the right value.
This operator is equivalent to the negation of the divisibility operator !(integer %% integer).
$ xa '7 !%% 4'
# TRUE
Exponentiation Operators#
Exponentiation operators consist only of the exponentiation operator.
Exponentiation operators have right-associative binding.
number ^ number: Exponentiation#
The exponentiation operator raises the left value to the power of the right value.
Even the result of raising an integer to an integer power is returned as a floating-point number.
$ xa '2 ^ 3'
# 8.0
There is also a function POW that can do the same thing.
$ xa 'POW(2; 3)'
# 8.0
Absolute Value#
The length operator $#number returns the absolute value of a number.
$ xa '$#10'
# 10
$ xa '$#-10'
# 10
Built-in Constants#
Numeric built-in constants.
| Constant | Meaning |
|---|---|
MATH.PI |
Pi (π) |
MATH.E |
Euler’s number (e) |
Built-in Functions#
ABS: Get Absolute Value#
ABS(value: NUMBER): NUMBER
Returns the absolute value of the first argument.
$ xa 'ABS(-10)'
# 10
FLOOR: Round Down#
FLOOR(number: NUMBER): INT
Rounds the first argument down to the nearest integer toward negative infinity.
$ xa 'FLOOR(1.5)'
# 1
DIV: Integer Part of Division#
DIV(x: NUMBER; y: NUMBER): NUMBER
Divides x by y and returns the integer part of the result.
$ xa 'DIV(10; 3)'
# 3
SQRT: Get Square Root#
SQRT(number: NUMBER): NUMBER
Returns the positive square root of the first argument.
$ xa '"$%.3f( SQRT(100.0) )"'
# 10.000
SIN: Sine#
SIN(number: NUMBER): NUMBER
Interprets the first argument as radians and returns its sine.
$ xa '"$%.3f( SIN(PI / 2) )"'
# 1.000
COS: Cosine#
COS(number: NUMBER): NUMBER
Interprets the first argument as radians and returns its cosine.
$ xa '"$%.3f( COS(0) )"'
# 1.000
TAN: Tangent#
TAN(number: NUMBER): NUMBER
Interprets the first argument as radians and returns its tangent.
$ xa '"$%.3f( TAN(PI / 4) )"'
# 1.000
POW: Power#
POW(base: NUMBER; exponent: NUMBER): NUMBER
Returns the result of raising the first argument to the power of the second argument.
$ xa 'POW(2; 3)'
# 8.0
EXP: Exponential Function#
EXP(number: NUMBER): NUMBER
Returns the exponential function (base e) of the first argument.
$ xa '"$%.3f( EXP(1) )"'
# 2.718
$ xa '"$%.3f( EXP(2) )"'
# 7.389
LOG: Natural Logarithm#
LOG(number: NUMBER): NUMBER
Returns the natural logarithm (base e) of the first argument.
$ xa '"$%.3f( LOG(MATH.E) )"'
# 1.000
RAND: Get Random Number#
RAND(): DOUBLE
RAND([from: NUMBER; ]until: NUMBER): INT
When called with no arguments, returns a decimal number greater than or equal to 0 and less than 1.
When called with one argument, returns an integer greater than or equal to 0 and less than until.
When called with two arguments, returns an integer greater than or equal to from and less than until.