Skip to content

Ratio::<T>::from_...() - definition of being representable? #113

@Enyium

Description

@Enyium

I use this crate to convert an f64 into an fps value (video framerate) consisting of u32 numerator and denominator. The documentation for Ratio::<u32>::from_f64(), for Ratio<T> and for num_traits::cast::FromPrimitive is not clear enough about what exactly it means that the input value can't "be represented by" a ratio.

I read your crate's approximate_float_unsigned() source code and, based on it, wrote the following in the doc comment of my crate's Fps::from_f64():

Returns None in the following cases (according to approximate_float_unsigned() of num-rational crate):

  • value < 0.0
  • value.is_nan()
  • value > u32::MAX as f64, overextending the numerator
  • The conversion yields a denominator of 0.

In other cases than mine like Ratio::<u64>::from_f32(), your function's line let t_max_f = <F as NumCast>::from(t_max.clone())?; may also return None.

I'd like to be able to be forever sure about when exactly None can be returned without the necessity of "according to".

Questions/suggestions:

  1. Can a denominator of 0 exclusively be yielded when the input f64 is positive/negative infinity?
  2. Would it be possible to clarify in your documentation when exactly None can be returned? (Possibly in the documentation for Ratio<T> with references to it in the other locations.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions