-
Notifications
You must be signed in to change notification settings - Fork 13.8k
Closed
rust-num/num-traits
#326Labels
A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsArea: Documentation for any part of the project, including the compiler, standard library, and toolsT-libsRelevant to the library team, which will review and decide on the PR/issue.Relevant to the library team, which will review and decide on the PR/issue.
Description
Location
float.rs line 1867, in this "example usage" code:
use num_traits::Float;
let num = 2.0f32;
// (8388608, -22, 1)
let (mantissa, exponent, sign) = Float::integer_decode(num);
let sign_f = sign as f32;
let mantissa_f = mantissa as f32;
let exponent_f = num.powf(exponent as f32);
// 1 * 8388608 * 2^(-22) == 2
let abs_difference = (sign_f * mantissa_f * exponent_f - num).abs();
assert!(abs_difference < 1e-10);
Summary
This code only works when num = 2.0
. With num
having almost any other value, the assert!
at the end fails. For example, with let num = 2.5f32;
, instead of let num = 2.0f32;
, the abs_difference is 2.4815533
. I think the problem is not in the function itself, but in the example code provided in the documentation.
Metadata
Metadata
Assignees
Labels
A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsArea: Documentation for any part of the project, including the compiler, standard library, and toolsT-libsRelevant to the library team, which will review and decide on the PR/issue.Relevant to the library team, which will review and decide on the PR/issue.