Skip to content

Commit

Permalink
fix: make panic free in transcendental.rs (#21)
Browse files Browse the repository at this point in the history
* fix: make panic free in transcendental.rs

* fix: make it short on checked_add
  • Loading branch information
XuJiandong authored Dec 5, 2024
1 parent 879c58b commit ddaa922
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/transcendental.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ where

let operand = D::from(operand);
if operand < D::from_num(1) {
let inverse = D::from_num(1).checked_div(operand).unwrap();
let inverse = D::from_num(1).checked_div(operand).ok_or(())?;
return Ok(-log2_inner::<D, D>(inverse));
};
return Ok(log2_inner::<D, D>(operand));
Expand Down Expand Up @@ -240,11 +240,11 @@ where
};
let neg = operand < ZERO;
if neg {
operand = -operand;
operand = operand.checked_neg().ok_or(())?;
};

let operand = D::from(operand);
let mut result = operand + D::from_num(1);
let mut result = operand.checked_add(D::from_num(1)).ok_or(())?;
let mut term = operand;

for i in 2..D::frac_nbits() {
Expand Down Expand Up @@ -317,7 +317,7 @@ where
}

/// power with integer exponend
pub fn powi<S,D>(operand: S, exponent: i32) -> Result<D, ()>
pub fn powi<S, D>(operand: S, exponent: i32) -> Result<D, ()>
where
S: Fixed + PartialOrd<ConstType>,
D: Fixed + PartialOrd<ConstType> + From<S> + From<ConstType>,
Expand Down

0 comments on commit ddaa922

Please sign in to comment.