Skip to content

Commit

Permalink
Fixed issue #9.
Browse files Browse the repository at this point in the history
  • Loading branch information
mamrhein committed Jan 21, 2024
1 parent f7abf3c commit 4bb5111
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions src/from_float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,8 @@ impl TryFrom<f64> for Decimal {
coeff,
n_frac_digits,
})
} else if exponent >= i128::BITS as i16 {
Err(DecimalError::InternalOverflow)
} else {
let numer = i128::from(sign) * i128::from(significand);
let shift = 1_i128 << exponent as usize;
Expand Down Expand Up @@ -295,12 +297,31 @@ mod tests {
}

#[test]
fn test_fail_overflow() {
let f = 5.839e38_f64;
let res = Decimal::try_from(f);
assert!(res.is_err());
let err = res.unwrap_err();
assert_eq!(err, DecimalError::InternalOverflow);
fn test_fail_overflow_from_f32() {
for f in [
3.401e38_f32,
313078212145816600000000000000000000000.0f32,
300666666666666666666666666666666666662.11,
] {
let res = Decimal::try_from(f);
assert!(res.is_err());
let err = res.unwrap_err();
assert_eq!(err, DecimalError::InternalOverflow);
}
}

#[test]
fn test_fail_overflow_from_f64() {
for f in [
5.839e38_f64,
113078212145816600000000000000000000000000000000000000000000000000000000000.0f64,
16666666666666666666666666666666666666666666666666666666666666666666666666662.11,
] {
let res = Decimal::try_from(f);
assert!(res.is_err());
let err = res.unwrap_err();
assert_eq!(err, DecimalError::InternalOverflow);
}
}

#[test]
Expand Down

0 comments on commit 4bb5111

Please sign in to comment.