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

Problem set Numbers, Integer Problem 5. Vague questioning. #377

Open
delta549 opened this issue Mar 4, 2023 · 0 comments
Open

Problem set Numbers, Integer Problem 5. Vague questioning. #377

delta549 opened this issue Mar 4, 2023 · 0 comments

Comments

@delta549
Copy link

delta549 commented Mar 4, 2023

Problem set:

// Fix errors and panics to make it work
fn main() {
   let v1 = 251_u8 + 8;
   let v2 = i8::checked_add(251, 8).unwrap();
   println!("{},{}",v1,v2);
}

The vagueness of the question here may be a cause for concern for someone new to this concept. The fundamental issue is that:

the literal `251` does not fit into the type `i8` whose range is `-128..=127`

Resolving this error can be handled by either changing the arithmetic to fit within the bounds of a u8/i8. Or by increasing the type to an i16/u16. The question merely asks you to resolve this.

The solution given is:

fn main() {
    let v1 = 247_u8 + 8;
    let v2 = i8::checked_add(119, 8).unwrap();
    println!("{},{}",v1,v2);
 }

Here the solution is to reduce the unsigned integer u8:

  • From 251 to 247 = 255 (The maximum value of a u8)
  • The signed i8 from (251, 8) to (119, 8) = 127 (The maximum positive value of an i8)

I believe here there should be a specific mention within the comment:
// Fix errors and panics to make it work

To change the values of the integers and not the integer types themselves. Something like:

// Fix errors and panics to make it work. 
// Do this by changing the values to the maximum allowed for an i8 and u8 respectively.

Or

// Fix errors and panics to make it work. Without changing the type!.

Hopefully this will give the learner the specific scope to answer the question and get the solution provided.

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

No branches or pull requests

1 participant