Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is integer division always equal to the floor of regular division?

For large quotients, integer division (//) doesn't seem to be necessarily equal to the floor of regular division (math.floor(a/b)).

According to Python docs (https://docs.python.org/3/reference/expressions.html - 6.7),

floor division of integers results in an integer; the result is that of mathematical division with the ‘floor’ function applied to the result.

However,

math.floor(648705536316023400 / 7) = 92672219473717632

648705536316023400 // 7 = 92672219473717628

'{0:.10f}'.format(648705536316023400 / 7) yields '92672219473717632.0000000000', but the last two digits of the decimal part should be 28 and not 32.

like image 700
Aditya Chanana Avatar asked Dec 21 '18 22:12

Aditya Chanana


People also ask

Is integer division the same as floor division?

floor(x/y) , is equal to the integer division.

Are integers division floor?

Floor division is a normal division operation except that it returns the largest possible integer. This integer is either less than or equal to the normal division result. Floor function is mathematically denoted by this ⌊ ⌋ symbol.

Is integer division the same as floor C++?

Since in C and C++, as others have said, / is integer division, it will return an int. in particular, it will return the floor of the double answer...

What is the rule regarding integer division?

DIVISION. RULE 1: The quotient of a positive integer and a negative integer is negative. RULE 2: The quotient of two positive integers is positive. RULE 3: The quotient of two negative integers is positive.

What is floor division in programming?

This integer is either less than or equal to the normal division result. Floor function is mathematically denoted by this ⌊ ⌋ symbol. Several programming languages have a specific built-in function or operator for calculating floor division.

Which direction does integer division round in?

The answer lies in the direction in which the result of an integer respectively floor division is rounded. Integer division rounds towards zero. This means, for example, that the result of the integer division 7 / 5 would have the real number 1.4 as an intermediate result and rounded in the direction of 0 would result in 1.

What are the units of Division in Algebra?

In a ring the elements by which division is always possible are called the units (for example, 1 and −1 in the ring of integers). Another generalization of division to algebraic structures is the quotient group, in which the result of "division" is a group rather than a number.

Does floor division lose information in Python?

However, with the floor division (//) Python uses its unlimited integer range, and so that result is correct. Note: For int and long arguments, true division (/) may lose information; this is in the nature of true division (as long as rationals are not in the language).


2 Answers

The reason the quotients in your test case are not equal is that in the math.floor(a/b) case, the result is calculated with floating point arithmetic (IEEE-754 64-bit), which means there is a maximum precision. The quotient you have there is larger than the 253 limit above which floating point is no longer accurate up to the unit.

With the integer division however, Python uses its unlimited integer range, and so that result is correct.

See also "Semantics of True Division" in PEP 238:

Note that for int and long arguments, true division may lose information; this is in the nature of true division (as long as rationals are not in the language). Algorithms that consciously use longs should consider using //, as true division of longs retains no more than 53 bits of precision (on most platforms).

like image 143
trincot Avatar answered Oct 14 '22 05:10

trincot


You may be dealing with integral values that are too large to express exactly as floats. Your number is significantly larger than 2^53, which is where the gaps between adjacent floating point doubles start to get bigger than 1. So you lose some precision when doing the floating point division.

The integer division, on the other hand, is computed exactly.

like image 19
interfect Avatar answered Oct 14 '22 07:10

interfect