Mathematical operations result is not correct

Sorry if my grammar is incorrect.

Here is the code I had, what the code does is that it returns the surface of a ball.

fn main() {
    let surball = |r: u32| {
        return 4 * 22 / 7 * (r * r);
    };

    println!("{}", surball(7));
}

r is diameter halfed (1/2). Instead of the correct answer returned which is 616 the function returns 588. Is there something wrong with my code?

And previously I tried using r ^ 2 instead of r * r and the answer are very different 4 * 22/7 * r ^ 2 equals 86 and 4 * 22/7 * r * r equals 588.

  • Integer division truncates (as usual in most systems programming languages); and
  • ^ is bitwise XOR, not exponentiation.

But all of this is in the reference. You should read it.

1 Like

Thanks, I was misread about the ^ symbol. But how do I resolve the integer division truncation?

By not using integers.

1 Like

Alright, thanks. It works!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.