Numbers in Soplang
Soplang supports various numeric types and provides a comprehensive set of operations and functions for working with numbers. This guide covers the essential aspects of numeric operations in Soplang.
On This Page
Numeric Types
Soplang supports two main numeric types: integers abn
and floating-point numbers toban
. The names come from the Somali words "abyoone" (number) and "tobanle" (decimal).
Integers (abn)
Integers are whole numbers without a decimal point. They can be positive, negative, or zero.
// Integer declarations abn a = 42 // Positive integer abn b = -7 // Negative integer abn c = 0 // Zero abn d = 1,000,000 // Using commas for readability // Dynamic typing with integers door x = 100 // Automatically inferred as an integer
Floating-Point Numbers (toban)
Floating-point numbers (or floats) represent real numbers with a decimal point.
// Float declarations toban pi = 3.14159 toban e = 2.71828 toban negative = -0.5 toban small = 1.23e-4 // Scientific notation: 0.000123 toban large = 6.022e23 // Scientific notation: 6.022 × 10²³ // Dynamic typing with floats door price = 19.99 // Automatically inferred as a float
Note on Precision
Like most programming languages, Soplang's floating-point numbers follow the IEEE 754 standard, which may lead to small precision errors in certain calculations. For financial calculations or when exact precision is required, consider using specialized libraries or techniques.
Numeric Literals
Soplang supports various formats for representing numeric literals in your code.
Integer Literals
// Decimal (base 10) - default door decimal = 42 // Binary (base 2) - prefix with 0b door binary = 0b101010 // 42 in binary // Octal (base 8) - prefix with 0o door octal = 0o52 // 42 in octal // Hexadecimal (base 16) - prefix with 0x door hex = 0x2A // 42 in hexadecimal // Using commas for readability door million = 1,000,000 door billion = 1,000,000,000 door binary_readable = 0b1010,1010,1010
Floating-Point Literals
// Standard decimal notation door pi = 3.14159 door negative = -2.5 // Scientific notation door avogadro = 6.022e23 // 6.022 × 10²³ door plancks_constant = 6.626e-34 // 6.626 × 10⁻³⁴
Basic Operations
Soplang supports all standard arithmetic operations for working with numbers.
Arithmetic Operators
// Addition door sum = 5 + 3 // 8 // Subtraction door difference = 10 - 4 // 6 // Multiplication door product = 6 * 7 // 42 // Division door quotient = 20 / 4 // 5 door decimal_result = 10 / 3 // 3.33333... // Integer division (floor division) door int_division = 10 // 3 // 3 // Modulo (remainder) door remainder = 10 % 3 // 1 // Exponentiation door power = 2 ** 8 // 256 // Negation door x = 5 door negative = -x // -5
Compound Assignment Operators
Compound assignment operators combine an arithmetic operation with assignment.
// Addition assignment door a = 5 a += 3 // a = a + 3, now a is 8 // Subtraction assignment door b = 10 b -= 4 // b = b - 4, now b is 6 // Multiplication assignment door c = 6 c *= 7 // c = c * 7, now c is 42 // Division assignment door d = 20 d /= 4 // d = d / 4, now d is 5 // Integer division assignment door e = 10 e //= 3 // e = e // 3, now e is 3 // Modulo assignment door f = 10 f %= 3 // f = f % 3, now f is 1 // Exponentiation assignment door g = 2 g **= 8 // g = g ** 8, now g is 256