aboutsummaryrefslogtreecommitdiffstats
path: root/src/eval.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/eval.rs')
-rw-r--r--src/eval.rs18
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),