Skip to content

Numeric operators

Arithmetic on integers and doubles.

Every program can use these operators. No import is necessary.

Operator Arguments Gives
- left: double, right: double double
- left: int, right: int int
* left: double, right: double double
* left: int, right: int int
/ left: double, right: double double
/ left: int, right: int double
% left: int, right: int int
+ left: double, right: double double
+ left: int, right: int int
+ left: int, right: int int
+ left: int, right: int int
+ left: int, right: <optional>int int
+ left: int, right: <optional>int int
+ left: int, right: <optional>int int
acos value: double double
asin value: double double
atan2 y: double, x: double double
bit_shl value: int, count: int int
bit_shr value: int, count: int int
bit_ushr value: int, count: int int
ceil value: double int
cos value: double double
fabs v: double double
floor value: double int
idiv left: int, right: int int
int2double value: int double
max left: int, right: int int
random min: int, max: int int
sin value: double double
sqrt value: double double
tan v: double double
to_double input: int double
to_int value: double int
unwrap arg: <optional>double double
unwrap arg: <optional>int int

Target support marks

  • Own template
  • Default template
  • No template

The operator works for a target with an own template and for a target with the default template. The default template is the * entry of the definition: it writes the same code for every target that has no template of its own. A target with no template writes no code, and a program that uses the operator does not compile for that target.

-

(- left right) → double
-(left: double, right: double)

numeric - operations

Target support

  • JavaScript: default template
  • TypeScript: default template
  • Go: default template
  • Rust: default template
  • Python: default template
  • Java: default template
  • Kotlin: default template
  • Dart: default template
  • Swift: default template
  • C#: default template
  • C++: default template
  • PHP: default template
  • Scala: default template

Definition: compiler/Lang.rgr, line 4478

-

(- left right) → int
-(left: int, right: int)

Target support

  • JavaScript: default template
  • TypeScript: default template
  • Go: default template
  • Rust: default template
  • Python: default template
  • Java: default template
  • Kotlin: default template
  • Dart: default template
  • Swift: default template
  • C#: default template
  • C++: default template
  • PHP: default template
  • Scala: default template

Integer arithmetic

docs/examples/core/numeric/arithmetic.rgr
class Main {
sfn main:void () {
def a 10
def b 3
print ("sum " + (a + b))
print ("difference " + (a - b))
print ("product " + (a * b))
print ("quotient " + (a / b))
print ("integer quotient " + (idiv a b))
}
}
main function, JavaScript
const a = 10;
const b = 3;
console.log("sum " + (a + b));
console.log("difference " + (a - b));
console.log("product " + a * b);
console.log("quotient " + a / b);
console.log("integer quotient " + ((a / b) | 0));
The complete file
class Main {
constructor() {
}
}
/* static JavaSript main routine at the end of the JS file */
function __js_main() {
const a = 10;
const b = 3;
console.log("sum " + (a + b));
console.log("difference " + (a - b));
console.log("product " + a * b);
console.log("quotient " + a / b);
console.log("integer quotient " + ((a / b) | 0));
}
__js_main();

Definition: compiler/Lang.rgr, line 4479

*

(* left right) → double
*(left: double, right: double)

Target support

  • JavaScript: default template
  • TypeScript: default template
  • Go: default template
  • Rust: default template
  • Python: default template
  • Java: default template
  • Kotlin: default template
  • Dart: default template
  • Swift: default template
  • C#: default template
  • C++: default template
  • PHP: default template
  • Scala: default template

Definition: compiler/Lang.rgr, line 4796

*

(* left right) → int
*(left: int, right: int)

Target support

  • JavaScript: default template
  • TypeScript: default template
  • Go: default template
  • Rust: default template
  • Python: default template
  • Java: default template
  • Kotlin: default template
  • Dart: default template
  • Swift: default template
  • C#: default template
  • C++: default template
  • PHP: default template
  • Scala: default template

Integer arithmetic

docs/examples/core/numeric/arithmetic.rgr
class Main {
sfn main:void () {
def a 10
def b 3
print ("sum " + (a + b))
print ("difference " + (a - b))
print ("product " + (a * b))
print ("quotient " + (a / b))
print ("integer quotient " + (idiv a b))
}
}
main function, JavaScript
const a = 10;
const b = 3;
console.log("sum " + (a + b));
console.log("difference " + (a - b));
console.log("product " + a * b);
console.log("quotient " + a / b);
console.log("integer quotient " + ((a / b) | 0));
The complete file
class Main {
constructor() {
}
}
/* static JavaSript main routine at the end of the JS file */
function __js_main() {
const a = 10;
const b = 3;
console.log("sum " + (a + b));
console.log("difference " + (a - b));
console.log("product " + a * b);
console.log("quotient " + a / b);
console.log("integer quotient " + ((a / b) | 0));
}
__js_main();

Definition: compiler/Lang.rgr, line 4797

/

(/ left right) → double
/(left: double, right: double)

Target support

  • JavaScript: default template
  • TypeScript: default template
  • Go: own template
  • Rust: default template
  • Python: own template
  • Java: default template
  • Kotlin: default template
  • Dart: default template
  • Swift: default template
  • C#: default template
  • C++: own template
  • PHP: default template
  • Scala: default template

Definition: compiler/Lang.rgr, line 4799

/

(/ left right) → double
/(left: int, right: int)

Divides the left integer by the right integer and gives a double.

The result is not a whole number. Use to_int when the program needs a whole number, and the result is then cut towards zero.

Target support

  • JavaScript: default template
  • TypeScript: default template
  • Go: own template
  • Rust: own template
  • Python: own template
  • Java: own template
  • Kotlin: own template
  • Dart: default template
  • Swift: own template
  • C#: own template
  • C++: own template
  • PHP: own template
  • Scala: own template

Integer arithmetic

docs/examples/core/numeric/arithmetic.rgr
class Main {
sfn main:void () {
def a 10
def b 3
print ("sum " + (a + b))
print ("difference " + (a - b))
print ("product " + (a * b))
print ("quotient " + (a / b))
print ("integer quotient " + (idiv a b))
}
}
main function, JavaScript
const a = 10;
const b = 3;
console.log("sum " + (a + b));
console.log("difference " + (a - b));
console.log("product " + a * b);
console.log("quotient " + a / b);
console.log("integer quotient " + ((a / b) | 0));
The complete file
class Main {
constructor() {
}
}
/* static JavaSript main routine at the end of the JS file */
function __js_main() {
const a = 10;
const b = 3;
console.log("sum " + (a + b));
console.log("difference " + (a - b));
console.log("product " + a * b);
console.log("quotient " + a / b);
console.log("integer quotient " + ((a / b) | 0));
}
__js_main();

Definition: compiler/Lang.rgr, line 4838

%

(% left right) → int
%(left: int, right: int)

Gives the remainder of the division of the two integers.

The sign of the result follows the target language. For a negative left operand, the result is negative in C++, C#, Go, Java, JavaScript, Rust and Swift. In Python the result has the sign of the right operand.

Target support

  • JavaScript: default template
  • TypeScript: default template
  • Go: default template
  • Rust: default template
  • Python: default template
  • Java: default template
  • Kotlin: default template
  • Dart: default template
  • Swift: default template
  • C#: default template
  • C++: default template
  • PHP: default template
  • Scala: default template

Integer remainder

docs/examples/core/numeric/mod.rgr
class Main {
sfn main:void () {
def a 7
def b 3
def rem (% a b)
print ("remainder " + rem)
}
}
main function, JavaScript
const a = 7;
const b = 3;
const rem = a % b;
console.log("remainder " + rem);
The complete file
class Main {
constructor() {
}
}
/* static JavaSript main routine at the end of the JS file */
function __js_main() {
const a = 7;
const b = 3;
const rem = a % b;
console.log("remainder " + rem);
}
__js_main();

Definition: compiler/Lang.rgr, line 4595

+

(+ left right) → double
+(left: double, right: double)

numeric + operations

Target support

  • JavaScript: default template
  • TypeScript: default template
  • Go: default template
  • Rust: default template
  • Python: default template
  • Java: default template
  • Kotlin: default template
  • Dart: default template
  • Swift: default template
  • C#: default template
  • C++: default template
  • PHP: default template
  • Scala: default template

Definition: compiler/Lang.rgr, line 4482

+

(+ left right) → int
+(left: int, right: int)

Target support

  • JavaScript: default template
  • TypeScript: default template
  • Go: default template
  • Rust: default template
  • Python: default template
  • Java: default template
  • Kotlin: default template
  • Dart: default template
  • Swift: default template
  • C#: default template
  • C++: default template
  • PHP: default template
  • Scala: default template

Definition: compiler/Lang.rgr, line 4500

+

(+ left right) → int
+(left: int, right: int)

Target support

  • JavaScript: default template
  • TypeScript: default template
  • Go: default template
  • Rust: default template
  • Python: default template
  • Java: default template
  • Kotlin: default template
  • Dart: default template
  • Swift: default template
  • C#: default template
  • C++: default template
  • PHP: default template
  • Scala: default template

Definition: compiler/Lang.rgr, line 4501

+

(+ left right) → int
+(left: int, right: int)

random tests end

Target support

  • JavaScript: default template
  • TypeScript: default template
  • Go: default template
  • Rust: default template
  • Python: default template
  • Java: default template
  • Kotlin: default template
  • Dart: default template
  • Swift: default template
  • C#: default template
  • C++: default template
  • PHP: default template
  • Scala: default template

Definition: compiler/Lang.rgr, line 4504

+

(+ left right) → int
+(left: int, right: <optional>int)

random tests, remove these later:

Target support

  • JavaScript: no template
  • TypeScript: no template
  • Go: no template
  • Rust: no template
  • Python: no template
  • Java: no template
  • Kotlin: no template
  • Dart: no template
  • Swift: no template
  • C#: no template
  • C++: no template
  • PHP: no template
  • Scala: no template

Integer arithmetic

docs/examples/core/numeric/arithmetic.rgr
class Main {
sfn main:void () {
def a 10
def b 3
print ("sum " + (a + b))
print ("difference " + (a - b))
print ("product " + (a * b))
print ("quotient " + (a / b))
print ("integer quotient " + (idiv a b))
}
}
main function, JavaScript
const a = 10;
const b = 3;
console.log("sum " + (a + b));
console.log("difference " + (a - b));
console.log("product " + a * b);
console.log("quotient " + a / b);
console.log("integer quotient " + ((a / b) | 0));
The complete file
class Main {
constructor() {
}
}
/* static JavaSript main routine at the end of the JS file */
function __js_main() {
const a = 10;
const b = 3;
console.log("sum " + (a + b));
console.log("difference " + (a - b));
console.log("product " + a * b);
console.log("quotient " + a / b);
console.log("integer quotient " + ((a / b) | 0));
}
__js_main();

Definition: compiler/Lang.rgr, line 4485

+

(+ left right) → int
+(left: int, right: <optional>int)

Target support

  • JavaScript: default template
  • TypeScript: default template
  • Go: default template
  • Rust: default template
  • Python: default template
  • Java: default template
  • Kotlin: default template
  • Dart: default template
  • Swift: default template
  • C#: default template
  • C++: default template
  • PHP: default template
  • Scala: default template

Definition: compiler/Lang.rgr, line 4493

+

(+ left right) → int
+(left: int, right: <optional>int)

Target support

  • JavaScript: default template
  • TypeScript: default template
  • Go: default template
  • Rust: default template
  • Python: default template
  • Java: default template
  • Kotlin: default template
  • Dart: default template
  • Swift: default template
  • C#: default template
  • C++: default template
  • PHP: default template
  • Scala: default template

Definition: compiler/Lang.rgr, line 4497

acos

(acos value) → double
acos(value: double)

Target support

  • JavaScript: default template
  • TypeScript: default template
  • Go: own template
  • Rust: own template
  • Python: own template
  • Java: own template
  • Kotlin: own template
  • Dart: own template
  • Swift: own template
  • C#: own template
  • C++: own template
  • PHP: own template
  • Scala: own template

Definition: compiler/Lang.rgr, line 5021

asin

(asin value) → double
asin(value: double)

Target support

  • JavaScript: default template
  • TypeScript: default template
  • Go: own template
  • Rust: own template
  • Python: own template
  • Java: own template
  • Kotlin: own template
  • Dart: own template
  • Swift: own template
  • C#: own template
  • C++: own template
  • PHP: own template
  • Scala: own template

Definition: compiler/Lang.rgr, line 5000

atan2

(atan2 y x) → double
atan2(y: double, x: double)

Target support

  • JavaScript: own template
  • TypeScript: own template
  • Go: own template
  • Rust: own template
  • Python: own template
  • Java: own template
  • Kotlin: own template
  • Dart: own template
  • Swift: own template
  • C#: own template
  • C++: own template
  • PHP: own template
  • Scala: own template

Definition: compiler/Lang.rgr, line 1386

bit_shl

(bit_shl value count) → int
bit_shl(value: int, count: int)

Bit shift left

Target support

  • JavaScript: own template
  • TypeScript: own template
  • Go: own template
  • Rust: own template
  • Python: own template
  • Java: own template
  • Kotlin: own template
  • Dart: default template
  • Swift: own template
  • C#: own template
  • C++: own template
  • PHP: default template
  • Scala: default template

Definition: compiler/Lang.rgr, line 4694

bit_shr

(bit_shr value count) → int
bit_shr(value: int, count: int)

Bit shift right (arithmetic/signed)

Target support

  • JavaScript: own template
  • TypeScript: own template
  • Go: own template
  • Rust: own template
  • Python: own template
  • Java: own template
  • Kotlin: own template
  • Dart: default template
  • Swift: own template
  • C#: own template
  • C++: own template
  • PHP: default template
  • Scala: default template

Definition: compiler/Lang.rgr, line 4710

bit_ushr

(bit_ushr value count) → int
bit_ushr(value: int, count: int)

Bit shift right unsigned (logical)

Target support

  • JavaScript: own template
  • TypeScript: own template
  • Go: own template
  • Rust: own template
  • Python: own template
  • Java: own template
  • Kotlin: own template
  • Dart: own template
  • Swift: own template
  • C#: own template
  • C++: own template
  • PHP: own template
  • Scala: default template

Definition: compiler/Lang.rgr, line 4726

ceil

(ceil value) → int
ceil(value: double)

TODO: add the rest ....(case ("sin" "cos" "tan" "atan" "log" "abs" "acos" "asin" "floor" "round" "sqrt") "<cmath>"

Target support

  • JavaScript: default template
  • TypeScript: default template
  • Go: own template
  • Rust: own template
  • Python: own template
  • Java: own template
  • Kotlin: own template
  • Dart: own template
  • Swift: own template
  • C#: own template
  • C++: own template
  • PHP: own template
  • Scala: own template

Double precision mathematics

docs/examples/core/numeric/double-math.rgr
class Main {
sfn main:void () {
def value 7.35
print ("square root " + (sqrt value))
print ("floor " + (floor value))
print ("ceiling " + (ceil value))
}
}
main function, JavaScript
const value = 7.35;
console.log("square root " + Math.sqrt(value));
console.log("floor " + Math.floor(value));
console.log("ceiling " + Math.ceil(value));
The complete file
class Main {
constructor() {
}
}
/* static JavaSript main routine at the end of the JS file */
function __js_main() {
const value = 7.35;
console.log("square root " + Math.sqrt(value));
console.log("floor " + Math.floor(value));
console.log("ceiling " + Math.ceil(value));
}
__js_main();

Definition: compiler/Lang.rgr, line 4961

cos

(cos value) → double
cos(value: double)

Target support

  • JavaScript: default template
  • TypeScript: default template
  • Go: own template
  • Rust: own template
  • Python: own template
  • Java: own template
  • Kotlin: own template
  • Dart: own template
  • Swift: own template
  • C#: own template
  • C++: own template
  • PHP: own template
  • Scala: own template

Definition: compiler/Lang.rgr, line 5040

fabs

(fabs v) → double
fabs(v: double)

Target support

  • JavaScript: own template
  • TypeScript: own template
  • Go: own template
  • Rust: own template
  • Python: own template
  • Java: own template
  • Kotlin: own template
  • Dart: own template
  • Swift: own template
  • C#: own template
  • C++: own template
  • PHP: own template
  • Scala: own template

Definition: compiler/Lang.rgr, line 1350

floor

(floor value) → int
floor(value: double)

Target support

  • JavaScript: default template
  • TypeScript: default template
  • Go: own template
  • Rust: own template
  • Python: own template
  • Java: own template
  • Kotlin: own template
  • Dart: own template
  • Swift: own template
  • C#: own template
  • C++: own template
  • PHP: own template
  • Scala: own template

Double precision mathematics

docs/examples/core/numeric/double-math.rgr
class Main {
sfn main:void () {
def value 7.35
print ("square root " + (sqrt value))
print ("floor " + (floor value))
print ("ceiling " + (ceil value))
}
}
main function, JavaScript
const value = 7.35;
console.log("square root " + Math.sqrt(value));
console.log("floor " + Math.floor(value));
console.log("ceiling " + Math.ceil(value));
The complete file
class Main {
constructor() {
}
}
/* static JavaSript main routine at the end of the JS file */
function __js_main() {
const value = 7.35;
console.log("square root " + Math.sqrt(value));
console.log("floor " + Math.floor(value));
console.log("ceiling " + Math.ceil(value));
}
__js_main();

Definition: compiler/Lang.rgr, line 4980

idiv

(idiv left right) → int
idiv(left: int, right: int)

Gives the integer quotient of two integers. The result truncates toward zero.

The operator / on two integers gives a double. Use idiv when the program needs a whole number.

Target support

  • JavaScript: own template
  • TypeScript: own template
  • Go: own template
  • Rust: own template
  • Python: own template
  • Java: own template
  • Kotlin: own template
  • Dart: own template
  • Swift: default template
  • C#: default template
  • C++: own template
  • PHP: own template
  • Scala: default template

Integer arithmetic

docs/examples/core/numeric/arithmetic.rgr
class Main {
sfn main:void () {
def a 10
def b 3
print ("sum " + (a + b))
print ("difference " + (a - b))
print ("product " + (a * b))
print ("quotient " + (a / b))
print ("integer quotient " + (idiv a b))
}
}
main function, JavaScript
const a = 10;
const b = 3;
console.log("sum " + (a + b));
console.log("difference " + (a - b));
console.log("product " + a * b);
console.log("quotient " + a / b);
console.log("integer quotient " + ((a / b) | 0));
The complete file
class Main {
constructor() {
}
}
/* static JavaSript main routine at the end of the JS file */
function __js_main() {
const a = 10;
const b = 3;
console.log("sum " + (a + b));
console.log("difference " + (a - b));
console.log("product " + a * b);
console.log("quotient " + a / b);
console.log("integer quotient " + ((a / b) | 0));
}
__js_main();

Definition: compiler/Lang.rgr, line 4603

int2double

(int2double value) → double
int2double(value: int)

Target support

  • JavaScript: default template
  • TypeScript: default template
  • Go: own template
  • Rust: own template
  • Python: own template
  • Java: own template
  • Kotlin: own template
  • Dart: default template
  • Swift: own template
  • C#: own template
  • C++: own template
  • PHP: own template
  • Scala: own template

Definition: compiler/Lang.rgr, line 4931

max

(max left right) → int
max(left: int, right: int)

Target support

  • JavaScript: default template
  • TypeScript: default template
  • Go: default template
  • Rust: default template
  • Python: default template
  • Java: default template
  • Kotlin: default template
  • Dart: default template
  • Swift: default template
  • C#: default template
  • C++: default template
  • PHP: default template
  • Scala: default template

Definition: compiler/Lang.rgr, line 7429

random

(random min max) → int
random(min: int, max: int)

Target support

  • JavaScript: own template
  • TypeScript: own template
  • Go: no template
  • Rust: no template
  • Python: own template
  • Java: own template
  • Kotlin: own template
  • Dart: own template
  • Swift: own template
  • C#: own template
  • C++: no template
  • PHP: own template
  • Scala: own template

Definition: compiler/Lang.rgr, line 819

sin

(sin value) → double
sin(value: double)

Target support

  • JavaScript: default template
  • TypeScript: default template
  • Go: own template
  • Rust: own template
  • Python: own template
  • Java: own template
  • Kotlin: own template
  • Dart: own template
  • Swift: own template
  • C#: own template
  • C++: own template
  • PHP: own template
  • Scala: own template

Definition: compiler/Lang.rgr, line 5059

sqrt

(sqrt value) → double
sqrt(value: double)

Target support

  • JavaScript: default template
  • TypeScript: default template
  • Go: own template
  • Rust: own template
  • Python: own template
  • Java: own template
  • Kotlin: own template
  • Dart: own template
  • Swift: own template
  • C#: own template
  • C++: own template
  • PHP: own template
  • Scala: own template

Double precision mathematics

docs/examples/core/numeric/double-math.rgr
class Main {
sfn main:void () {
def value 7.35
print ("square root " + (sqrt value))
print ("floor " + (floor value))
print ("ceiling " + (ceil value))
}
}
main function, JavaScript
const value = 7.35;
console.log("square root " + Math.sqrt(value));
console.log("floor " + Math.floor(value));
console.log("ceiling " + Math.ceil(value));
The complete file
class Main {
constructor() {
}
}
/* static JavaSript main routine at the end of the JS file */
function __js_main() {
const value = 7.35;
console.log("square root " + Math.sqrt(value));
console.log("floor " + Math.floor(value));
console.log("ceiling " + Math.ceil(value));
}
__js_main();

Definition: compiler/Lang.rgr, line 5078

tan

(tan v) → double
tan(v: double)

Target support

  • JavaScript: own template
  • TypeScript: own template
  • Go: own template
  • Rust: own template
  • Python: own template
  • Java: own template
  • Kotlin: own template
  • Dart: own template
  • Swift: own template
  • C#: own template
  • C++: own template
  • PHP: own template
  • Scala: own template

Definition: compiler/Lang.rgr, line 1367

to_double

(to_double input) → double
to_double(input: int)

conversions

Target support

  • JavaScript: own template
  • TypeScript: own template
  • Go: own template
  • Rust: own template
  • Python: own template
  • Java: own template
  • Kotlin: own template
  • Dart: own template
  • Swift: own template
  • C#: own template
  • C++: own template
  • PHP: own template
  • Scala: own template

Definition: compiler/Lang.rgr, line 11341

to_int

(to_int value) → int
to_int(value: double)

TODO: double_buffer_from requires variadic argument support in Ranger operators Create double_buffer from literal values - for cleaner initialization of lookup tables Usage: def table:double_buffer (double_buffer_from 1.0 2.0 3.0 4.0) double_buffer_from cmdDoubleBufferFrom:double_buffer ( listOf:expression ) { templates { ranger ( "(double_buffer_from " (list 1) ")" ) es6 ( "new Float64Array([" (comma 1) "])" ) go ( "[]float64{" (comma 1) "}" ) rust ( "vec![" (comma 1) "]" ) cpp ( "std::vector<double>{" (comma 1) "}" (imp "<vector>") ) java7 ( "new double[]{" (comma 1) "}" ) python ( "[" (comma 1) "]" ) swift6 ( "[" (comma 1) "] as [Double]" ) csharp ( "new double[]{" (comma 1) "}" ) } }

Target support

  • JavaScript: default template
  • TypeScript: default template
  • Go: own template
  • Rust: own template
  • Python: own template
  • Java: own template
  • Kotlin: own template
  • Dart: own template
  • Swift: own template
  • C#: own template
  • C++: own template
  • PHP: own template
  • Scala: own template

Definition: compiler/Lang.rgr, line 7357

unwrap

(unwrap arg) → double
unwrap(arg: <optional>double)

Target support

  • JavaScript: no template
  • TypeScript: no template
  • Go: no template
  • Rust: no template
  • Python: no template
  • Java: no template
  • Kotlin: no template
  • Dart: no template
  • Swift: no template
  • C#: own template
  • C++: own template
  • PHP: no template
  • Scala: no template

Definition: compiler/Lang.rgr, line 4315

unwrap

(unwrap arg) → int
unwrap(arg: <optional>int)

Target support

  • JavaScript: no template
  • TypeScript: no template
  • Go: no template
  • Rust: no template
  • Python: no template
  • Java: no template
  • Kotlin: no template
  • Dart: no template
  • Swift: no template
  • C#: own template
  • C++: own template
  • PHP: no template
  • Scala: no template

Definition: compiler/Lang.rgr, line 4307

Ranger 3.5.1 · commit f323fd4 · development build