From 52d74efb296cdcdfe8452995dc9d96b34a0e24ac Mon Sep 17 00:00:00 2001 From: Rose Hogenson Date: Tue, 28 May 2024 18:20:10 -0700 Subject: Implement modulo. --- src/eval.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) (limited to 'src/eval.rs') diff --git a/src/eval.rs b/src/eval.rs index d3b5ddc..e07bec2 100644 --- a/src/eval.rs +++ b/src/eval.rs @@ -1,5 +1,6 @@ use num::pow::Pow; -use num::{BigInt, FromPrimitive, Signed, ToPrimitive}; +use num::{BigInt, FromPrimitive, Integer, Signed, ToPrimitive}; +use std::cmp::Ordering; use std::fmt::{Display, Formatter}; pub enum Num { @@ -83,6 +84,21 @@ impl Num { } } + pub fn modulo(self, other: Num) -> Num { + match (self, other) { + (Num::Int(i1), Num::Int(i2)) => int(i1.mod_floor(&i2)), + (n1, n2) => { + let n1 = n1.as_float(); + let n2 = n2.as_float(); + let r = n1 % n2; + if r < 0. { + return float(r + n2); + } + float(r) + } + } + } + pub fn add(self, other: Num) -> Num { match (self, other) { (Num::Int(i1), Num::Int(i2)) => int(i1 + i2), -- cgit v1.3.1