(&mut self, a: Val, b: Val)
| 247 | } |
| 248 | |
| 249 | fn exec_floordiv(&mut self, a: Val, b: Val) -> Result<Val, VmErr> { |
| 250 | if a.is_float() || b.is_float() { |
| 251 | let af = self.to_f64_coerce(a).map_err(|_| cold_type("// requires numeric operands"))?; |
| 252 | let bf = self.to_f64_coerce(b).map_err(|_| cold_type("// requires numeric operands"))?; |
| 253 | if bf == 0.0 { return Err(VmErr::ZeroDiv); } |
| 254 | // ffloor() handles all magnitudes; `as i64` would overflow for large floats. |
| 255 | return Ok(Val::float(ffloor(af / bf))); |
| 256 | } |
| 257 | let (Some(ai), Some(bi)) = (self.as_i128(a), self.as_i128(b)) else { return Err(cold_type("// requires numeric operands")); }; |
| 258 | if bi == 0 { return Err(VmErr::ZeroDiv); } |
| 259 | // Floor-div on i128: round toward negative infinity. checked_div guards i128::MIN / -1 overflow. |
| 260 | let q = ai.checked_div(bi).ok_or(cold_overflow())?; |
| 261 | let r = ai - q * bi; |
| 262 | let q = if (r != 0) && ((r < 0) != (bi < 0)) { q - 1 } else { q }; |
| 263 | self.int_to_val(Some(q)) |
| 264 | } |
| 265 | |
| 266 | fn exec_pow(&mut self, a: Val, b: Val) -> Result<Val, VmErr> { |
| 267 | self.pow_vals(a, b, "** requires numeric operands") |
no test coverage detected