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

fix : Use of integer for prices, therefore also for weight #433

Merged
merged 1 commit into from
Jun 21, 2020
Merged

fix : Use of integer for prices, therefore also for weight #433

merged 1 commit into from
Jun 21, 2020

Conversation

SebastienCaunes
Copy link
Contributor

rename confusing "from" and "to" to sender_country and recipient_country as suggested

rename confusing "from" and "to" to sender_country and recipient_country as suggested
@SebastienCaunes
Copy link
Contributor Author

Issue 432 #432

}
}

fn is_international(&self) -> ??? {
// Something goes here...
}

fn get_fees(&self, cost_per_kg: f32) -> ??? {
// Something goes here...
fn get_fees(&self, cents_per_kg: i32) -> ??? {
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps this should also be cents_per_gram. Otherwise, the output may have to be a float, which we're trying to avoid. For example, a package weighing 150 grams -- with a cost of 10 cents per kg -- would have a cost of 1.5 cents.

Copy link
Contributor Author

@SebastienCaunes SebastienCaunes Jun 11, 2020

Choose a reason for hiding this comment

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

1.5 cent will be rounded and that's what we want because we can't pay 0.5 cents anyway.
Cents per gram is not precise enough as a price is usually less than one cent per gram.
Yes it require to divide by 1000. I think that is not hard to grasp. I also think it would be nice to add a link to a good explanation why float are bad for money.
Like this: https://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency

Copy link
Contributor

Choose a reason for hiding this comment

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

Currently, it asks the user to supply the return type. Many users likely will reason that it should be a float, since otherwise information is lost (and as-is it is unclear whether they should round or truncate).

So, to avoid another issue, it might be good to either:

  1. Force the return type to be an integer, and add a comment asking the user to truncate the result. -or-
  2. Use cents_per_gram, where we will not have to deal with fractions.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is no need to truncate or round, integers operations do it by themselves.
Relevant information is lost with floats, not with integer.
I think your option 1 is the good one, no need to ask for any truncate or rounding. It is done naturally by integers operation.
Option 2 makes no sense as even the smallest integer price for 1 gram is out of scale ($1000 for 1kg).

Copy link
Contributor

@danwilhelm danwilhelm Jun 11, 2020

Choose a reason for hiding this comment

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

Thinking through this as a student, I would notice that 1254 / 1000 == 1. I would be hesitant to lose the extra 254 grams via an integer division.

Since Rustlings did not specify how to handle excess grams, I would likely decide the safest approach is to pro-rate cents_per_kg, maybe like this:

    fn get_fees(&self, cents_per_kg: i32) -> i32 {
        let weight_in_kgs = (self.weight_in_grams as f32) / 1000.;
        
        (weight_in_kgs * (cents_per_kg as f32)) as i32
    }

Yet even now, I am unsure whether I wrote the function as Rustlings intended -- should I have rounded or truncated the result? (Money is often rounded.)

So, this is why I recommend including a comment explaining exactly how the fees should be computed (or just making it easy with cents_per_kg). I am just trying to avoid another Issue later on 😀

Copy link
Contributor Author

@SebastienCaunes SebastienCaunes Jun 11, 2020

Choose a reason for hiding this comment

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

We could also simply store weight in kg and integer, ignoring grams for the example it makes everything more simple and is more correct than using floats for money.
Should I do another pull request this way ?

Copy link
Contributor

@danwilhelm danwilhelm Jun 11, 2020

Choose a reason for hiding this comment

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

😀 Yes, that is a much better method and (except with large numbers) yields the same result! So perhaps a specification would be something like:

// Fees are pro-rated. Disregard any final fractions of a cent.
// Try to compute the fees without using floats!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As you want as long there's no float used for money, I'm happy.

I just did the clippy1 exercice with the beautifull advice :

  if y != x {
   |        ^^^^^^ help: consider comparing them within some error: `(y - x).abs() > error`

That's how floats must be handled !

Copy link
Contributor

@danwilhelm danwilhelm Jun 12, 2020

Choose a reason for hiding this comment

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

We could also simply store weight in kg and integer, ignoring grams for the example it makes everything more simple and is more correct than using floats for money. Should I do another pull request this way ?

It might be easiest just to make them all the same units, but either way is probably fine as long as it's explained how to compute the fees. Other than this, I think it looks great!

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm a bit unsure about the units puzzle: it risks losing focus, but a bit of non-Rust-related challenge makes the exercises more amenable. No strong opinion anyway.

@shadows-withal shadows-withal requested a review from jrvidal June 20, 2020 14:18
Copy link
Contributor

@jrvidal jrvidal left a comment

Choose a reason for hiding this comment

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

LGTM 🚀

}
}

fn is_international(&self) -> ??? {
// Something goes here...
}

fn get_fees(&self, cost_per_kg: f32) -> ??? {
// Something goes here...
fn get_fees(&self, cents_per_kg: i32) -> ??? {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm a bit unsure about the units puzzle: it risks losing focus, but a bit of non-Rust-related challenge makes the exercises more amenable. No strong opinion anyway.


let country_fee = ???;
let cents_per_kg = ???;
Copy link
Contributor

Choose a reason for hiding this comment

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

Random thought, totally out of scope for this PR: it feels weird to me that we have learners guess backwards the appropriate input to obtain a predetermined output.

Also, I'm starting to think that forcing miscompilations through invalid code like ??? is something we should dial back a bit.

@shadows-withal shadows-withal merged commit 75c0053 into rust-lang:master Jun 21, 2020
@SebastienCaunes SebastienCaunes deleted the fix#432 branch June 30, 2020 17:08
ppp3 pushed a commit to ppp3/rustlings that referenced this pull request May 23, 2022
fix : Use of integer for prices, therefore also for weight
dmoore04 pushed a commit to dmoore04/rustlings that referenced this pull request Sep 11, 2022
fix : Use of integer for prices, therefore also for weight
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.

4 participants