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

Simplify Fraction and improve Rational. #476

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft

Conversation

maneva3
Copy link
Collaborator

@maneva3 maneva3 commented Jan 15, 2025

No description provided.

@maneva3 maneva3 requested a review from gmanev7 January 15, 2025 10:20
Copy link
Member

@gmanev7 gmanev7 left a comment

Choose a reason for hiding this comment

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

a partial review

pub trait Ratio<U> {
fn parts(&self) -> U;
fn total(&self) -> U;
#[derive(Debug, Serialize, Deserialize, JsonSchema)]
Copy link
Member

Choose a reason for hiding this comment

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

please review their necessity


pub fn total(&self) -> U {
self.total
}
Copy link
Member

Choose a reason for hiding this comment

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

struct Rational<U> deserves its own module

Comment on lines +69 to +80
pub trait CheckedMul<Rhs = Self> {
type Output;

fn checked_mul(self, rhs: Rhs) -> Option<Self::Output>;
}

impl<U, T> Fraction<U> for Rational<T>
pub trait CheckedAdd<Rhs = Self> {
type Output;

fn checked_add(self, rhs: Rhs) -> Option<Self::Output>;
}

Copy link
Member

Choose a reason for hiding this comment

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

these two traits should be moved out into their own checked_ops module

{
#[track_caller]
fn of<A>(&self, whole: A) -> A
// Multiplication of Rational > 1.
Copy link
Member

Choose a reason for hiding this comment

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

the comment is meaningless, please remove

@@ -38,13 +36,10 @@ where
<D as TryInto<DIntermediate>>::Error: Debug,
DIntermediate: Into<T>,
D: Mul<D, Output = D> + Div<D, Output = D>,
U: Zero + PartialEq + Into<D>,
U: Zero + PartialEq + Into<D> + PartialOrd + Copy,
Copy link
Member

Choose a reason for hiding this comment

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

are you sure we need more trait bounds here

Comment on lines +110 to +125
fn checked_add(self, rhs: Self) -> Option<Self::Output> {
self.denominator
.checked_mul(rhs.denominator)
.and_then(|common_denominator| {
self.nominator
.checked_mul(rhs.denominator)
.and_then(|scaled_left_nom| {
rhs.nominator
.checked_mul(self.denominator)
.and_then(|scaled_right_nom| {
scaled_left_nom
.checked_add(scaled_right_nom)
.map(|nominator| Rational::new(nominator, common_denominator))
})
})
})
Copy link
Member

Choose a reason for hiding this comment

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

the good news is that this is already implemented in Price and battle-tested, no need to do it again

Comment on lines +111 to +113
self.denominator
.checked_mul(rhs.denominator)
.and_then(|common_denominator| {
Copy link
Member

Choose a reason for hiding this comment

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

would overflow in many cases when it should not, for example: with U=u16, (1,u16::MAX) + (1,u16::MAX)

Comment on lines +110 to +112
impl From<Percent> for Rational<Units> {
fn from(percent: Percent) -> Self {
Self::new(percent.units(), Percent::HUNDRED.units())
Copy link
Member

Choose a reason for hiding this comment

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

I believe we have discussed another conversion Percent -> Ratio

Comment on lines +116 to +117
impl TryFrom<Rational<Units>> for Percent {
type Error = Error;
Copy link
Member

Choose a reason for hiding this comment

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

not sure we need this conversion

Comment on lines +144 to +147
let lhs: Rational<Units> = self.into();
let rhs: Rational<Units> = rhs.into();

lhs.checked_add(rhs).and_then(|res| res.try_into().ok())
Copy link
Member

Choose a reason for hiding this comment

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

it'd be trivial to implement it through Units::checked_add instead

type Output = Self;

fn div(self, rhs: Self) -> Self::Output {
Percent(self.0 / rhs.0)
Copy link
Member

Choose a reason for hiding this comment

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

let's retain the control in our hands by checking the preconditions ourselves, add a debug_assert!(this != Zero)

type Output = Self;

fn rem(self, rhs: Self) -> Self::Output {
Percent(self.0 % rhs.0)
Copy link
Member

Choose a reason for hiding this comment

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

same as above

Comment on lines -321 to +317
.lossy_mul(&Rational::new(self.amount_quote, rhs.amount))
.lossy_mul(&Ratio::new(self.amount_quote.into(), rhs.amount.into()))
Copy link
Member

Choose a reason for hiding this comment

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

no guarantees that the price is < 1 !!!

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.

2 participants