Skip to content

Chapter 3.2: Integer division rounding is described incorrectly #2856

@jruderman

Description

@jruderman
  • 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 to truncated or rounded_toward_zero

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions