Skip to content

Commit ddaa922

Browse files
authored
fix: make panic free in transcendental.rs (#21)
* fix: make panic free in transcendental.rs * fix: make it short on checked_add
1 parent 879c58b commit ddaa922

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

src/transcendental.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ where
210210

211211
let operand = D::from(operand);
212212
if operand < D::from_num(1) {
213-
let inverse = D::from_num(1).checked_div(operand).unwrap();
213+
let inverse = D::from_num(1).checked_div(operand).ok_or(())?;
214214
return Ok(-log2_inner::<D, D>(inverse));
215215
};
216216
return Ok(log2_inner::<D, D>(operand));
@@ -240,11 +240,11 @@ where
240240
};
241241
let neg = operand < ZERO;
242242
if neg {
243-
operand = -operand;
243+
operand = operand.checked_neg().ok_or(())?;
244244
};
245245

246246
let operand = D::from(operand);
247-
let mut result = operand + D::from_num(1);
247+
let mut result = operand.checked_add(D::from_num(1)).ok_or(())?;
248248
let mut term = operand;
249249

250250
for i in 2..D::frac_nbits() {
@@ -317,7 +317,7 @@ where
317317
}
318318

319319
/// power with integer exponend
320-
pub fn powi<S,D>(operand: S, exponent: i32) -> Result<D, ()>
320+
pub fn powi<S, D>(operand: S, exponent: i32) -> Result<D, ()>
321321
where
322322
S: Fixed + PartialOrd<ConstType>,
323323
D: Fixed + PartialOrd<ConstType> + From<S> + From<ConstType>,

0 commit comments

Comments
 (0)