Skip to content

Commit

Permalink
core: Fix panic on division by integer 0 (#180)
Browse files Browse the repository at this point in the history
Division by `0isize` now behaves the same as division by `0f64`.
Both produce `+inf.0`.

```scheme
(= (/ 1 0) (/ 1 0.0)) ; => #true
```
  • Loading branch information
sirius94 authored Mar 8, 2024
1 parent 831e8bc commit 87f6646
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions crates/steel-core/src/primitives/numbers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ pub fn divide_primitive(args: &[SteelVal]) -> Result<SteelVal> {
let recip = |x: &SteelVal| -> Result<SteelVal> {
match x {
SteelVal::IntV(n) => match i32::try_from(*n) {
Ok(0) => Ok(SteelVal::NumV(f64::INFINITY)),
Ok(n) => Rational32::new(1, n).into_steelval(),
Err(_) => BigRational::new(BigInt::from(1), BigInt::from(*n)).into_steelval(),
},
Expand Down Expand Up @@ -1086,6 +1087,14 @@ mod num_op_tests {
);
}

#[test]
fn dvision_by_integer_zero_returns_positive_infinity() {
assert_eq!(
divide_primitive(&[IntV(1), IntV(0)]).unwrap().to_string(),
NumV(f64::INFINITY).to_string()
)
}

#[test]
fn division_on_single_integer_returns_reciprocal_rational() {
assert_eq!(
Expand Down

0 comments on commit 87f6646

Please sign in to comment.