Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarify example for trait method float::Float::integer_decode #326

Conversation

mtilda
Copy link
Contributor

@mtilda mtilda commented Jun 26, 2024

Fixes rust-lang/rust#126610

Problem

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);

@mtilda mtilda force-pushed the mtilda/patch/fix-example-for-float-integer-decoder branch from 2a4ad0b to d3935bf Compare June 26, 2024 20:05
@mtilda mtilda changed the title Clarify example for trait method Float::integer_decode Clarify example for trait method float::Float::integer_decode Jun 26, 2024
src/float.rs Outdated Show resolved Hide resolved
@mtilda mtilda requested a review from cuviper June 27, 2024 15:11
Copy link
Member

@cuviper cuviper left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks!

@cuviper cuviper added this pull request to the merge queue Jun 27, 2024
Merged via the queue into rust-num:master with commit b1f3bda Jun 27, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

integer_decode() for float: example usage in docs doesn't make sense
2 participants