-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Closed
rust-lang/rust
#103543Milestone
Description
- I have checked the latest
main
branch to see if this has already been fixed - I have searched existing issues and pull requests for duplicates
URL to the section(s) of the book with this problem:
https://doc.rust-lang.org/book/ch03-02-data-types.html#numeric-operations (but only in a newer version than on the web site)
Description of the problem:
With the fix for #2248, the book states:
Integer division rounds down to the nearest integer.
and has the example:
let floored = 2 / 3; // Results in 0
However, on godbolt, the behavior I observe is instead truncation toward zero.
fn main()
{
println!("{}", (-9) / (4));
println!("{}", (9) / (-4));
println!("{}", (-9) / (-4));
}
outputs
-2
-2
2
I believe this is true on all architectures supported by Rust: the Rust Reference states "Integer division rounds towards zero."
Suggested fix:
- "Integer division rounds toward zero" or "Integer division truncates toward zero"
- Change
floored
totruncated
orrounded_toward_zero
TheNeverK, DJDuque, mateusz834 and ibeauregard
Metadata
Metadata
Assignees
Labels
No labels