MCPcopy Create free account
hub / github.com/dylan-sutton-chavez/edge-python / exec_floordiv

Method exec_floordiv

compiler/src/modules/vm/handlers/arith.rs:249–264  ·  view source on GitHub ↗
(&mut self, a: Val, b: Val)

Source from the content-addressed store, hash-verified

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")

Callers 1

handle_arithMethod · 0.80

Calls 7

cold_typeFunction · 0.85
ffloorFunction · 0.85
cold_overflowFunction · 0.85
is_floatMethod · 0.80
to_f64_coerceMethod · 0.80
as_i128Method · 0.80
int_to_valMethod · 0.80

Tested by

no test coverage detected