Numbers in Soul represent numeric values using high-precision decimal arithmetic. They can be integers or floating-point numbers and support all standard arithmetic operations.
a = 10b = 3// Basic operationssum = a + b // 13difference = a - b // 7product = a * b // 30quotient = a / b // 3.333...remainder = a % b // 1// Compound operationsresult = (a + b) * 2 - 1 // 25
a = 10b = 20// Comparison operatorsisEqual = (a == b) // falseisNotEqual = (a != b) // trueisLess = (a < b) // trueisGreater = (a > b) // falseisLessOrEqual = (a <= b) // trueisGreaterOrEqual = (a >= b) // false
Use appropriate precision: Choose the right precision for your use case
Validate inputs: Check for null and invalid numbers
Handle edge cases: Consider zero, negative numbers, and infinity
Use constants: Define meaningful constants for magic numbers
Format for display: Format numbers appropriately for user interfaces
Copy
Ask AI
// Good - comprehensive number handlingsoul calculatePrice(quantity, unitPrice, taxRate) { // Validate inputs if (quantity == null || unitPrice == null || taxRate == null) { return { success: false, error: "Missing required parameters" } } if (!isNumber(quantity) || !isNumber(unitPrice) || !isNumber(taxRate)) { return { success: false, error: "All parameters must be numbers" } } if (quantity <= 0) { return { success: false, error: "Quantity must be positive" } } if (unitPrice < 0) { return { success: false, error: "Unit price cannot be negative" } } if (taxRate < 0 || taxRate > 1) { return { success: false, error: "Tax rate must be between 0 and 1" } } // Calculate price subtotal = quantity * unitPrice tax = subtotal * taxRate total = subtotal + tax return { success: true, subtotal: subtotal, tax: tax, total: total }}
Numbers are fundamental to most Soul applications. Use them with proper validation, error handling, and appropriate precision for reliable and accurate calculations.