diff options
| author | Rose Hogenson <rosehogenson@posteo.net> | 2024-05-28 18:20:10 -0700 |
|---|---|---|
| committer | Rose Hogenson <rosehogenson@posteo.net> | 2024-05-28 18:27:21 -0700 |
| commit | 52d74efb296cdcdfe8452995dc9d96b34a0e24ac (patch) | |
| tree | fa08d7eaf090d14e9c360d0bef7fc21496746741 /src/eval.rs | |
| parent | 2df47cf5e375c73917ebcd6690cd16199d57af83 (diff) | |
| download | qc-52d74efb296cdcdfe8452995dc9d96b34a0e24ac.tar.zst | |
Implement modulo.
Diffstat (limited to 'src/eval.rs')
| -rw-r--r-- | src/eval.rs | 18 |
1 files changed, 17 insertions, 1 deletions
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), |
