aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe-php-shim/src
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2026-06-21 16:48:03 +0900
committernsfisis <nsfisis@gmail.com>2026-06-21 16:50:58 +0900
commit12a7756588a20f1351e6976f0c009de5992514a9 (patch)
tree53ee08e207fc06687d998e9ad749e425e56ee138 /crates/shirabe-php-shim/src
parent657440d01423b50153d07ebc288d9bd2fc982d52 (diff)
downloadphp-shirabe-12a7756588a20f1351e6976f0c009de5992514a9.tar.gz
php-shirabe-12a7756588a20f1351e6976f0c009de5992514a9.tar.zst
php-shirabe-12a7756588a20f1351e6976f0c009de5992514a9.zip
feat(php-shim): implement round() shim
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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
}