The example for trait method float::Float::integer_decode fails if num is changed to most any value other than 2.
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);
Changes
The equation used to recover the original number, num, from the outputs of Float::integer_decode(num) is given in the doc comment as sign * mantissa * 2 ^ exponent. However, in the example, num was being used as the exponent's base, as in the equation sign * mantissa * num ^ exponent. This PR fixes that mistake.
It follows logically that when num is 2, the example behaves correctly even with the bug. To reduce confusion, this PR changes the value of num in the example to be 42.
For readability, this PR also moves the invocation of .powf(exponent as f32), so it is directly underneath the comment // 1 * 11010048 * 2^(-18) == 42.
Updated example
use num_traits::Float;
let num = 42_f32;
// (11010048, -18, 1)
let (mantissa, exponent, sign) = Float::integer_decode(num);
let sign_f = sign as f32;
let mantissa_f = mantissa as f32;
let exponent_f = exponent as f32;
// 1 * 11010048 * 2^(-18) == 42
let abs_difference = (sign_f * mantissa_f * exponent_f.exp2() - num).abs();
assert!(abs_difference < 1e-10);
Fixes https://github.com/rust-lang/rust/issues/126610
Problem
The example for trait method float::Float::integer_decode fails if
num
is changed to most any value other than 2.Changes
The equation used to recover the original number,
num
, from the outputs ofFloat::integer_decode(num)
is given in the doc comment assign * mantissa * 2 ^ exponent
. However, in the example,num
was being used as the exponent's base, as in the equationsign * mantissa * num ^ exponent
. This PR fixes that mistake.It follows logically that when
num
is 2, the example behaves correctly even with the bug. To reduce confusion, this PR changes the value ofnum
in the example to be 42.For readability, this PR also moves the invocation of
.powf(exponent as f32)
, so it is directly underneath the comment// 1 * 11010048 * 2^(-18) == 42
.Updated example