Skip to content

Add explicit and unified interface for approximation #100

@cmpute

Description

@cmpute

I propose to add a trait to explicitly allow rational approximation for various types. This is motivated by my crate for irrational numbers where I would like to have a unified interface for approximation (see #35 (comment)).

Here's a draft:

pub enum Approximation<T> {
    Approximated(T),
    Exact(T)
}

pub trait RationalApproximation<T: Integer> {
    // Here `limit` determines the (absolute?) maximum of the numerator and denumerator 
    fn approx_rational(&self, limit: &T) -> Approximation<Ratio<T>>;
}

There're several benefits to define this interface:

  1. If we implement RationalApproximation for Ratio<T> itself, then we can have an explicit method to limit the precision of the fraction. (related to Feature request: Limiting precision #99)
  2. Other number types can use this interface to provide approximation ability (such as my irrational number types)
  3. The existing from_float and approximate_float functions seem messy to me. I would suggest to keep the from_float method, while implements RationalApproximation for f32 and f64.

Thanks!

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