aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe-php-shim/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/shirabe-php-shim/src')
-rw-r--r--crates/shirabe-php-shim/src/math.rs29
1 files changed, 13 insertions, 16 deletions
diff --git a/crates/shirabe-php-shim/src/math.rs b/crates/shirabe-php-shim/src/math.rs
index fffe2d6..ee9a76d 100644
--- a/crates/shirabe-php-shim/src/math.rs
+++ b/crates/shirabe-php-shim/src/math.rs
@@ -1,33 +1,30 @@
-pub fn max_i64(_a: i64, _b: i64) -> i64 {
- _a.max(_b)
+pub fn max(a: i64, b: i64) -> i64 {
+ a.max(b)
}
-pub fn max(_a: i64, _b: i64) -> i64 {
- _a.max(_b)
+pub fn min(a: i64, b: i64) -> i64 {
+ a.min(b)
}
-pub fn min(_a: i64, _b: i64) -> i64 {
- _a.min(_b)
+pub fn abs(v: i64) -> i64 {
+ v.abs()
}
-pub fn abs(_value: i64) -> i64 {
- _value.abs()
-}
-
-pub fn floor(_value: f64) -> f64 {
- _value.floor()
+pub fn floor(v: f64) -> f64 {
+ v.floor()
}
pub fn ceil(v: f64) -> f64 {
v.ceil()
}
-pub fn round(_value: f64, _precision: i64) -> f64 {
- todo!()
+pub fn round(v: f64, precision: i64) -> f64 {
+ // PHP's default mode is PHP_ROUND_HALF_UP (round half away from zero),
+ // which matches Rust's f64::round.
+ let factor = 10f64.powi(precision as i32);
+ (v * factor).round() / factor
}
pub fn intdiv(a: i64, b: i64) -> i64 {
- // PHP intdiv() throws DivisionByZeroError on a zero divisor and ArithmeticError
- // for PHP_INT_MIN / -1; Rust's `/` likewise panics in both cases.
a / b
}