Skip to content

Commit

Permalink
Clarify division truncates toward zero. Fixes #2856.
Browse files Browse the repository at this point in the history
  • Loading branch information
carols10cents committed Oct 20, 2022
1 parent 699adc6 commit e9e0b97
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fn main() {

// division
let quotient = 56.7 / 32.2;
let floored = 2 / 3; // Results in 0
let truncated = -5 / 3; // Results in -1

// remainder
let remainder = 43 % 5;
Expand Down
6 changes: 3 additions & 3 deletions src/ch03-02-data-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,9 @@ Floating-point numbers are represented according to the IEEE-754 standard. The

#### Numeric Operations

Rust supports the basic mathematical operations you’d expect for all of the
number types: addition, subtraction, multiplication, division, and remainder.
Integer division rounds down to the nearest integer. The following code shows
Rust supports the basic mathematical operations you’d expect for all the number
types: addition, subtraction, multiplication, division, and remainder. Integer
division truncates toward zero to the nearest integer. The following code shows
how you’d use each numeric operation in a `let` statement:

<span class="filename">Filename: src/main.rs</span>
Expand Down

1 comment on commit e9e0b97

@MarkDBlackwell
Copy link

Choose a reason for hiding this comment

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

Good!

I would suggest:

Integer division rounds to the nearest integer in the direction of zero. (This is known as "truncation.")

Please sign in to comment.