Skip to content

Commit

Permalink
add modulo function
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwparas committed Oct 23, 2024
1 parent e336b69 commit 2be891e
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
11 changes: 6 additions & 5 deletions cogs/r5rs.scm
Original file line number Diff line number Diff line change
Expand Up @@ -294,13 +294,14 @@

;; TODO: Adjust the below check-equals

(skip-compile (check-equal 1 (modulo 13 4))
(check-equal 1 (remainder 13 4))
(check-equal 3 (modulo -13 4))
(check-equal? "modulo positive numbers" 1 (modulo 13 4))
(check-equal? "modulo negative and positive" 3 (modulo -13 4))
(check-equal? "modulo positive and negative" -3 (modulo 13 -4))
(check-equal? "modulo both negative" -1 (modulo -13 -4))

(skip-compile (check-equal 1 (remainder 13 4))
(check-equal -1 (remainder -13 4))
(check-equal -3 (modulo 13 -4))
(check-equal 1 (remainder 13 -4))
(check-equal -1 (modulo -13 -4))
(check-equal -1 (remainder -13 -4))
(check-equal 4 (gcd 32 -36))
(check-equal 288 (lcm 32 -36)))
Expand Down
25 changes: 25 additions & 0 deletions crates/steel-core/src/primitives/numbers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,31 @@ pub fn quotient(args: &[SteelVal]) -> Result<SteelVal> {
}
}

/// Returns the remainder of the division of the first number by the second
///
/// (modulo n m) -> integer?
///
/// * n : integer?
/// * m : integer?
///
/// # Examples
/// ```scheme
/// > (modulo 10 3) ;; => 1
/// > (modulo -10 3) ;; => 2
/// > (modulo 10 -3) ;; => -2
/// > (module -10 -3) ;; => -1
/// ```
#[steel_derive::native(name = "modulo", constant = true, arity = "Exact(2)")]
pub fn modulo(args: &[SteelVal]) -> Result<SteelVal> {
match &args {
[l, r] => match (l, r) {
(SteelVal::IntV(l), SteelVal::IntV(r)) => ((l % r + r) % r).into_steelval(),
_ => steelerr!(TypeMismatch => "quotient only supports integers"),
},
_ => steelerr!(ArityMismatch => "quotient requires 2 arguments"),
}
}

/// Divides the given numbers.
///
/// (/ . nums) -> number?
Expand Down
1 change: 1 addition & 0 deletions crates/steel-core/src/steel_vm/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1501,6 +1501,7 @@ impl Engine {
})
}

// TODO: Remove duplicates!
pub fn readable_globals(&self, after_offset: usize) -> Vec<InternedString> {
self.virtual_machine
.compiler
Expand Down
1 change: 1 addition & 0 deletions crates/steel-core/src/steel_vm/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1070,6 +1070,7 @@ fn number_module() -> BuiltInModule {
.register_native_fn_definition(numbers::MAGNITUDE_DEFINITION)
.register_native_fn_definition(numbers::NUMERATOR_DEFINITION)
.register_native_fn_definition(numbers::QUOTIENT_DEFINITION)
.register_native_fn_definition(numbers::MODULO_DEFINITION)
.register_native_fn_definition(numbers::ROUND_DEFINITION)
.register_native_fn_definition(numbers::SQUARE_DEFINITION)
.register_native_fn_definition(numbers::SQRT_DEFINITION);
Expand Down

0 comments on commit 2be891e

Please sign in to comment.