Rounding integer division (instead of truncating)
int a = 59.0f / 4.0f + 0.5f;
This only works when assigning to an int as it discards anything after the '.'
Edit:
This solution will only work in the simplest of cases. A more robust solution would be:
unsigned int round_closest(unsigned int dividend, unsigned int divisor)
{
return (dividend + (divisor / 2)) / divisor;
}
Why does the division get rounded to an integer?
You're using Python 2.x, where integer divisions will truncate instead of becoming a floating point number.
>>> 1 / 2
0
You should make one of them a float
:
>>> float(10 - 20) / (100 - 10)
-0.1111111111111111
or from __future__ import division
, which the forces /
to adopt Python 3.x's behavior that always returns a float.
>>> from __future__ import division
>>> (10 - 20) / (100 - 10)
-0.1111111111111111
What is the behavior of integer division?
Will result always be the floor of the division? What is the defined behavior?
Not quite. It rounds toward 0, rather than flooring.
6.5.5 Multiplicative operators
6 When integers are divided, the result of the / operator is the algebraic quotient with any
fractional part discarded.88) If the quotient a/b is representable, the expression
(a/b)*b + a%b shall equal a.
and the corresponding footnote:
- This is often called ‘‘truncation toward zero’’.
Of course two points to note are:
3 The usual arithmetic conversions are performed on the operands.
and:
5 The result of the / operator is the
quotient from the division of the
first operand by the second; the
result of the % operator is the
remainder. In both operations, if the
value of the second operand is zero,
the behavior is undefined.
[Note: Emphasis mine]
Why does SQL Server round off results of dividing two integers?
When you do integer division (integer divided by integer) you always get an integer answer. 50/100 = .50, which is 0 in integer-speak.
Have you tried dividing MY_COLUMN by 100.0?
Why does integer division round down in many scripting languages?
Floating-point operations are defined by IEEE754 with numeric applications in mind and, by default, round to the nearest representable value in a very strictly-defined manner.
Integer operations in computers are not defined by general international standards. The operations granted by languages (especially those of the C family) tend to follow whatever the underlying computer provides. Some languages define certain operations more robustly than others, but to avoid excessively difficult or slow implementations on the available (and popular) computers of their time, will choose a definition that follows its behaviour quite closely.
For this reason, integer operations tend to wrap around on overflow (for addition, multiplication, and shifting-left), and round towards negative infinity when producing an inexact result (for division, and shifting-right). Both of these are simple truncation at their respective end of the integer in two's-complement binary arithmetic; the simplest way to handle a corner-case.
Other answers discuss the relationship with the remainder or modulus operator that a language might provide alongside division. Unfortunately they have it backwards. Remainder depends on the definition of division, not the other way around, while modulus can be defined independently of division - if both arguments happen to be positive and division rounds down, they work out to be the same, so people rarely notice.
Most modern languages provide either a remainder operator or a modulus operator, rarely both. A library function may provide the other operation for people who care about the difference, which is that remainder retains the sign of the dividend, while modulus retains the sign of the divisor.
Why does integer division in C# return an integer and not a float?
While it is common for new programmer to make this mistake of performing integer division when they actually meant to use floating point division, in actual practice integer division is a very common operation. If you are assuming that people rarely use it, and that every time you do division you'll always need to remember to cast to floating points, you are mistaken.
First off, integer division is quite a bit faster, so if you only need a whole number result, one would want to use the more efficient algorithm.
Secondly, there are a number of algorithms that use integer division, and if the result of division was always a floating point number you would be forced to round the result every time. One example off of the top of my head is changing the base of a number. Calculating each digit involves the integer division of a number along with the remainder, rather than the floating point division of the number.
Because of these (and other related) reasons, integer division results in an integer. If you want to get the floating point division of two integers you'll just need to remember to cast one to a double
/float
/decimal
.
What's the mathematical reason behind Python choosing to round integer division toward negative infinity?
But why Python
//
choose to round towards negative infinity?
I'm not sure if the reason why this choice was originally made is documented anywhere (although, for all I know, it could be explained in great length in some PEP somewhere), but we can certainly come up with various reasons why it makes sense.
One reason is simply that rounding towards negative (or positive!) infinity means that all numbers get rounded the same way, whereas rounding towards zero makes zero special. The mathematical way of saying this is that rounding down towards −∞ is translation invariant, i.e. it satisfies the equation:
round_down(x + k) == round_down(x) + k
for all real numbers x
and all integers k
. Rounding towards zero does not, since, for example:
round_to_zero(0.5 - 1) != round_to_zero(0.5) - 1
Of course, other arguments exist too, such as the argument you quote based on compatibility with (how we would like) the %
operator (to behave) — more on that below.
Indeed, I would say the real question here is why Python's int()
function is not defined to round floating point arguments towards negative infinity, so that m // n
would equal int(m / n)
. (I suspect "historical reasons".) Then again, it's not that big of a deal, since Python does at least have math.floor()
that does satisfy m // n == math.floor(m / n)
.
But I don't see C++ 's
/
not being compatible with the modulo function. In C++,(m/n)*n + m%n == m
also applies.
True, but retaining that identity while having /
round towards zero requires defining %
in an awkward way for negative numbers. In particular, we lose both of the following useful mathematical properties of Python's %
:
0 <= m % n < n
for allm
and all positiven
; and(m + k * n) % n == m % n
for all integersm
,n
andk
.
These properties are useful because one of the main uses of %
is "wrapping around" a number m
to a limited range of length n
.
For example, let's say we're trying to calculate directions: let's say heading
is our current compass heading in degrees (counted clockwise from due north, with 0 <= heading < 360
) and that we want to calculate our new heading after turning angle
degrees (where angle > 0
if we turn clockwise, or angle < 0
if we turn counterclockwise). Using Python's %
operator, we can calculate our new heading simply as:
heading = (heading + angle) % 360
and this will simply work in all cases.
However, if we try to to use this formula in C++, with its different rounding rules and correspondingly different %
operator, we'll find that the wrap-around doesn't always work as expected! For example, if we start facing northwest (heading = 315
) and turn 90° clockwise (angle = 90
), we'll indeed end up facing northeast (heading = 45
). But if then try to turn back 90° counterclockwise (angle = -90
), with C++'s %
operator we won't end up back at heading = 315
as expected, but instead at heading = -45
!
To get the correct wrap-around behavior using the C++ %
operator, we'll instead need to write the formula as something like:
heading = (heading + angle) % 360;
if (heading < 0) heading += 360;
or as:
heading = ((heading + angle) % 360) + 360) % 360;
(The simpler formula heading = (heading + angle + 360) % 360
will only work if we can always guarantee that heading + angle >= -360
.)
This is the price you pay for having a non-translation-invariant rounding rule for division, and consequently a non-translation-invariant %
operator.
Round with integer division
You can do this quite simply:
(n + d // 2) // d
, where n
is the dividend and d
is the divisor.
Alternatives like (((n << 1) // d) + 1) >> 1
or the equivalent (((n * 2) // d) + 1) // 2
may be SLOWER in recent CPythons, where an int
is implemented like the old long
.
The simple method does 3 variable accesses, 1 constant load, and 3 integer operations. The complicated methods do 2 variable accesses, 3 constant loads, and 4 integer operations. Integer operations are likely to take time which depends on the sizes of the numbers involved. Variable accesses of function locals don't involve "lookups".
If you are really desparate for speed, do benchmarks. Otherwise, KISS.
Dividing two integers and rounding up result to nearest integer
Since a
and b
are integers, a/b
will use integer division, and only return the "whole" part of the result. Instead, you should multiply a
by 100.0
(note the .0
, which makes it a double
literal!) to use floating-point division, and then ceil
the result, and truncate it to an int
:
c = (int) Math.ceil(100.0 * a / b);
Related Topics
Pycharm and Sys.Argv Arguments
How to Deal With Settingwithcopywarning in Pandas
How to Make a Python Script Run Like a Service or Daemon in Linux
Python Pysftp Get_R from Linux Works Fine on Linux But Not on Windows
Why Is Printing to Stdout So Slow? Can It Be Sped Up
Python Ftp Get the Most Recent File by Date
What Do I Need to Read Microsoft Access Databases Using Python
How to Activate Virtualenv in Linux
Find Size and Free Space of the Filesystem Containing a Given File
What Do I Use on Linux to Make a Python Program Executable
Does Python Support Short-Circuiting
What Does If _Name_ == "_Main_": Do
Multiprocessing VS Threading Python