Arithmetic

basic arithmetic operations (addition, subtraction, multiplication, etc) for integers and floating-point numbers


Functions


The Arithmetic module provides basic mathematical operations that work seamlessly across different numeric types - integers, floating-point numbers, complex numbers, and rationals.

Key Concepts

  • Operations work with multiple numeric types
  • Support for in-place modifications
  • Complex and rational number operations
  • Automatic type promotion when needed
  • All operators available as infix symbols

Basic Usage

Basic Operations

; addition
print 2 + 3             ; 5
print add 2 3           ; 5

; subtraction
print 5 - 3             ; 2
print sub 5 3           ; 2

; multiplication
print 4 * 3             ; 12
print mul 4 3           ; 12

; division
print 10 / 3            ; 3 (integer division)
print 10 // 3           ; 3.333333333333333 (floating division)
print div 10 3          ; 3
print fdiv 10 3         ; 3.333333333333333

Working with Different Types

; integer + floating
print 2 + 3.81          ; 5.81

; with complex numbers
z: to :complex [3 4]    ; 3.0+4.0i
print z + 2             ; 5.0+4.0i
print z * 3             ; 9.0+12.0i

; with rationals
a: 1:2                  ; 1/2
b: 3:4                  ; 3/4
print a + b             ; 5/4

In-place Modifications

x: 5

; increment
inc 'x                   ; x is now 6
print x                  ; 6

; decrement
dec 'x                   ; x is now 5
print x                  ; 5

; other operations
add 'x 3                 ; x is now 8
mul 'x 2                 ; x is now 16
print x                  ; 16

Note
Using literals (e.g., 'x) performs operations in-place, modifying the original value.

Common Patterns

Division and Remainder

; integer division with remainder
[quotient remainder]: divmod 17 5
print ["17 ÷ 5 =" quotient "remainder" remainder]    
; 17 ÷ 5 = 3 remainder 2

; floating division
print fdiv 17 5             ; 3.4

Working with Quantities

distance: 100`m         ; 100 meters
time: 5`s               ; 5 seconds

speed: fdiv distance time
print speed             ; 20 m/s

Type Conversions & Promotions

; integer → floating
a: 5 + 3.41             ; 8.41 (floating)
b: 2 * 3.5              ; 7.0 (floating)

; integer → rational
x: 3 + 1:2              ; 7/2 (rational)
y: 2 3:4                ; 3/2 (rational)

; integer → complex
z: 2 + to :complex [1 1]  
; 3.0+1.0i (complex)

; quantity +/- amount
q: 3`m + 10             ; 3 meters (quantity)     
r: 3`m - 2              ; 1 meter (quantity)

; quantity * times
s: 3`km * 3             ; 9 kilometers (quantity)
t: 3 * 3`km             ; 9 kilometers (quantity)

; quantity * quantity
u: 2`m * 3`m            ; 6 sq.meters (quantity)

Note
When operating with different numeric types, Arturo automatically promotes to the most precise type needed.