Skip to content

integer_decode() for float: example usage in docs doesn't make sense #126610

@AdamSlayer

Description

@AdamSlayer

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

No one assigned

    Labels

    A-docsArea: 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.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions