small medium large xlarge

Generic-user-small
03 Oct 2015, 19:07
Phil Yasuhara (1 post)

I have the same question as the previous person to which no one answered.

  1. What does “floor of the result of the division” mean?

  2. Please explain why -17//10 results in 1, and why -17%10 results in 3

Campbell_pragsmall
02 Nov 2015, 18:08
Jennifer Campbell (14 posts)
  1. The floor of a real number is the largest integer that is less than or equal to that number. For example, the floor of 2.75 is 2, 53.1 is 53, and -2.6 is -3 (the last results may look odd, but notice that -3 is less than or equal to -2.6).

By “floor of the result of the division”, we are referring to a two step process that involves evaluating the division operation to yield a number and then taking the floor of that number.

  1. Let’s begin an example involving positive numbers. 7 // 3 is 2 and 7 % 3 is 1. That result can be rewritten as 2 1/3, which is equivalent to the fraction 7 over 3.

Next, you asked why -17 // 10 results in 1. However, the result of evaluating that expression is -2.

Let’s look at floating point division first: -17 / 10 evaluates to -1.7.

Now, let’s look at the integer division: -17 // 10 evaluates to -2. (Following the rule for floor, -2 is the largest integer that is less than or equal to -1.7.)

And the modulo: -17 % 10 evaluates to 3. This is the remainder of the integer division.

As we did before, this can be written as -2 3/10, which is equivalent to the fraction -17 / 10.

You must be logged in to comment