Math Methods
Math operations in Oxide use Rust's standard numeric methods directly. Oxide's case conversion automatically translates camelCase method calls to Rust's snake_case equivalents.
Integer Methods
Methods available on Int and other integer types:
| Method | Rust Equivalent | Description |
|---|---|---|
abs() | abs() | Absolute value |
min(other) | min(other) | Minimum of two values |
max(other) | max(other) | Maximum of two values |
clamp(min, max) | clamp(min, max) | Clamp to range |
pow(exp) | pow(exp) | Power |
Integer Examples
// Absolute value (requires type annotation for literals)
let negFive: Int = -5
negFive.abs() // 5
let n: Int = -7
n.abs() // 7
// Min/max using Ord trait methods
let three: Int = 3
let seven: Int = 7
three.min(seven) // 3
three.max(seven) // 7
// Clamping
let ten: Int = 10
ten.clamp(0, 5) // 5 (clamped to max)
let neg: Int = -5
neg.clamp(0, 10) // 0 (clamped to min)
// Power
let two: Int = 2
two.pow(10) // 1024
// Even/odd checks (use modulo)
6 % 2 == 0 // true (even)
7 % 2 != 0 // true (odd)
Float Methods
Methods available on Float64 (f64) and Float32 (f32):
| Method | Rust Equivalent | Description |
|---|---|---|
abs() | abs() | Absolute value |
sqrt() | sqrt() | Square root |
floor() | floor() | Round down |
ceil() | ceil() | Round up |
round() | round() | Round to nearest |
sin() | sin() | Sine (radians) |
cos() | cos() | Cosine (radians) |
tan() | tan() | Tangent (radians) |
min(other) | min(other) | Minimum of two values |
max(other) | max(other) | Maximum of two values |
clamp(min, max) | clamp(min, max) | Clamp to range |
Float Examples
// Square root
let f: Float64 = 16.0
f.sqrt() // 4.0
let two: Float64 = 2.0
two.sqrt() // 1.4142135...
// Absolute value
let neg: Float64 = -3.14
neg.abs() // 3.14
// Rounding
let f37: Float64 = 3.7
let f32: Float64 = 3.2
let f35: Float64 = 3.5
f37.floor() // 3.0
f32.ceil() // 4.0
f35.round() // 4.0
// Trigonometry (input in radians)
let zero: Float64 = 0.0
zero.sin() // 0.0
zero.cos() // 1.0
let pi: Float64 = 3.14159265359
(pi / 2.0).sin() // ~1.0
(pi / 4.0).tan() // ~1.0
Practical Examples
Distance Calculation
fn distance(x1: Float64, y1: Float64, x2: Float64, y2: Float64): Float64 {
let dx = x2 - x1
let dy = y2 - y1
(dx * dx + dy * dy).sqrt()
}
let d = distance(0.0, 0.0, 3.0, 4.0) // 5.0
Clamping Values
fn normalizePercent(value: Int): Int {
value.clamp(0, 100)
}
normalizePercent(150) // 100
normalizePercent(-20) // 0
normalizePercent(75) // 75
Temperature Conversion
fn celsiusToFahrenheit(c: Float64): Float64 {
c * 9.0 / 5.0 + 32.0
}
fn roundToDecimal(value: Float64, places: UInt32): Float64 {
let factor = 10.0_f64.pow(places)
(value * factor).round() / factor
}
let temp = celsiusToFahrenheit(20.0)
let rounded = roundToDecimal(temp, 1) // 68.0
Statistical Functions
fn mean(values: &[Float64]): Float64? {
if values.isEmpty() {
return null
}
let sum = values.iter().cloned().reduce { a, b -> a + b } ?? 0.0
sum / values.len() as Float64
}
fn variance(values: &[Float64]): Float64? {
let m = mean(values)?
let sumSquares = values.iter()
.map { (it - m) * (it - m) }
.reduce { a, b -> a + b }
?? 0.0
sumSquares / values.len() as Float64
}
fn stddev(values: &[Float64]): Float64? {
variance(values)?.sqrt()
}
Comparison with Rust
| Oxide | Rust |
|---|---|
n.abs() | n.abs() |
n.min(m) | n.min(m) |
n.max(m) | n.max(m) |
n.clamp(lo, hi) | n.clamp(lo, hi) |
f.sqrt() | f.sqrt() |
f.floor() | f.floor() |
f.ceil() | f.ceil() |
f.round() | f.round() |
f.sin() | f.sin() |
f.cos() | f.cos() |
f.tan() | f.tan() |
n.pow(e) | n.pow(e) |
n % 2 == 0 | n % 2 == 0 (even check) |
n % 2 != 0 | n % 2 != 0 (odd check) |
Additional Float Methods
Since Oxide uses Rust's numeric methods directly, you can use any method from Rust's standard library:
let f: Float64 = 2.0
// Exponential and logarithmic
f.exp() // e^2
f.ln() // natural log
f.log10() // log base 10
f.log2() // log base 2
// More trigonometry
f.asin() // arc sine
f.acos() // arc cosine
f.atan() // arc tangent
f.atan2(1.0) // two-argument arc tangent
// Hyperbolic functions
f.sinh() // hyperbolic sine
f.cosh() // hyperbolic cosine
f.tanh() // hyperbolic tangent
// Special functions
f.powi(3) // integer power (faster)
f.powf(2.5) // float power
f.cbrt() // cube root
f.hypot(3.0) // sqrt(self^2 + other^2)
// Classification
f.isNan() // is_nan()
f.isInfinite() // is_infinite()
f.isFinite() // is_finite()
See Also
- Types - Numeric types
- Operators - Arithmetic operators
- Standard Library Overview