Skip to content

Commit b4ba5c5

Browse files
committed
Change output type of Int / Int and Int % Int
Int/Int evaluates to Int if exactly divisible. Int % Int always evaluates to int. Resolves #22.
1 parent 92e0289 commit b4ba5c5

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/value.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,13 @@ impl ops::Div for Number {
131131

132132
fn div(self, other: Number) -> Number {
133133
match (self, other) {
134-
(Number::Integer(x), Number::Integer(y)) => Number::Float((x as f64) / (y as f64)),
134+
(Number::Integer(x), Number::Integer(y)) => {
135+
if x % y == 0 {
136+
Number::Integer(x / y)
137+
} else {
138+
Number::Float((x as f64) / (y as f64))
139+
}
140+
}
135141
(Number::Float(x), Number::Float(y)) => Number::Float(x / y),
136142
(Number::Float(x), Number::Integer(y)) => Number::Float(x / (y as f64)),
137143
(Number::Integer(x), Number::Float(y)) => Number::Float((x as f64) / y),
@@ -144,7 +150,7 @@ impl ops::Rem for Number {
144150

145151
fn rem(self, other: Number) -> Number {
146152
match (self, other) {
147-
(Number::Integer(x), Number::Integer(y)) => Number::Float((x as f64) % (y as f64)),
153+
(Number::Integer(x), Number::Integer(y)) => Number::Integer(x % y),
148154
(Number::Float(x), Number::Float(y)) => Number::Float(x % y),
149155
(Number::Float(x), Number::Integer(y)) => Number::Float(x % (y as f64)),
150156
(Number::Integer(x), Number::Float(y)) => Number::Float((x as f64) % y),

tests/run-pass/int-div-and-mod.bl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# will fail if numbers are converted to float
2+
assert_eq(9223372036854775806 / 2, 4611686018427387903);
3+
assert_eq(9223372036854775806 % 9, 6);

0 commit comments

Comments
 (0)