279

Python's math module contain handy functions like floor & ceil. These functions take a floating point number and return the nearest integer below or above it. However these functions return the answer as a floating point number. For example:

import math
f=math.floor(2.3)

Now f returns:

2.0

What is the safest way to get an integer out of this float, without running the risk of rounding errors (for example if the float is the equivalent of 1.99999) or perhaps I should use another function altogether?

Mark Dickinson
  • 29,088
  • 9
  • 83
  • 120
Boaz
  • 25,331
  • 21
  • 69
  • 77
  • 8
    `math.floor` [returns a float in v2.6](https://docs.python.org/2/library/math.html), but [it returns an integer in v3](https://docs.python.org/3.0/library/math.html). At this point (almost six years after the OP), this issue might show up rarely. – sancho.s ReinstateMonicaCellio Apr 21 '16 at 12:34
  • however numpy still returns float, so the question is valid. – Vincenzooo Oct 29 '18 at 16:43
  • The title could be improved to "Safest way to round down and convert float to integer in python?" – WurmD Sep 18 '20 at 10:33

9 Answers9

210

All integers that can be represented by floating point numbers have an exact representation. So you can safely use int on the result. Inexact representations occur only if you are trying to represent a rational number with a denominator that is not a power of two.

That this works is not trivial at all! It's a property of the IEEE floating point representation that int∘floor = ⌊⋅⌋ if the magnitude of the numbers in question is small enough, but different representations are possible where int(floor(2.3)) might be 1.

To quote from Wikipedia,

Any integer with absolute value less than or equal to 224 can be exactly represented in the single precision format, and any integer with absolute value less than or equal to 253 can be exactly represented in the double precision format.

Philipp
  • 48,066
  • 12
  • 84
  • 109
  • 9
    +1 for going a bit deeper. You could also throw in a brief explanation as to why: http://en.wikipedia.org/wiki/Floating_point :D – Gordon Gustafson Aug 02 '10 at 12:27
  • In Python 2, an "int" is the same as a C "int". In Python 3, it appears there is no limit to the size of an "int, https://stackoverflow.com/questions/13795758/what-is-sys-maxint-in-python-3. The meaning of "int" is also dependent on the operating system and underlying hardware. See https://en.wikipedia.org/wiki/64-bit_computing#64-bit_data_models. If you are programming with the C-API, python 3 you have to be very careful in what the definition of long and size_t is on your platform. https://docs.python.org/3/c-api/long.html – Juan Sep 27 '17 at 14:21
154

Use int(your non integer number) will nail it.

print int(2.3) # "2"
print int(math.sqrt(5)) # "2"
Jens Erat
  • 37,523
  • 16
  • 80
  • 96
srodriguex
  • 2,900
  • 3
  • 18
  • 28
  • 4
    This won't work for negative numbers: `floor` rounds down whereas `int` rounds towards 0. – jochen Jul 15 '14 at 20:35
  • 1
    @jochen I tested `int(-2.3)` in Python distribution Canopy 2.7.6 and got `-2` as expected. Integer numbers can be negative, the same way in formal Math definition. – srodriguex Jul 20 '14 at 16:26
  • 5
    I agree, `int(-2.3)` gives `-2` as you say, because it rounds towards `0`, *i.e.* up in this case. In contrast, the original question used `math.floor`, which always rounds down: `math.floor(-2.3)` gives `-3.0`. – jochen Jul 21 '14 at 10:40
  • 2
    That is not really a problem. OP just wants an integer out of the result of `math.floor`, and this answer shows how to convert a float into an integer. Take the float from `math.floor` and pipe it through `int`, problem solved: `int(math.floor(2.3))` – JMTyler Aug 19 '15 at 04:40
  • 4
    Did you even read the question? He is aware of the **int()** function, but has asked if you may run into trouble with 1.9999 instead of 2.0. Your answer is not even close to an answer at all, you missed the whole point... – Mayou36 Oct 01 '16 at 17:02
  • @Mayou36 It seems I missed the point, but it's what I wrote with the knowledge I had at the time, so let it be. The community will vote up or down as they see fit. But, I really think it holds: `int(-999999999980/99999999999) == -9 # True` – srodriguex Oct 05 '16 at 18:14
59

You could use the round function. If you use no second parameter (# of significant digits) then I think you will get the behavior you want.

IDLE output.

>>> round(2.99999999999)
3
>>> round(2.6)
3
>>> round(2.5)
3
>>> round(2.4)
2
Yarin
  • 173,523
  • 149
  • 402
  • 512
Wade73
  • 4,359
  • 3
  • 30
  • 46
  • 31
    `round` returns a float number as well, at least in Python 2.6. – Philipp Aug 02 '10 at 12:32
  • 9
    In Python 3.1.2, round returns an int. – robert Aug 02 '10 at 12:34
  • 3
    Indeed, both `round` and `floor` return integers in Python 3.x. So I suppose that the question concerns Python 2.x. – Philipp Aug 02 '10 at 12:40
  • and since (it appears that) the OP is using IPython which isn't available for py3k, that's probably a valid supposition, Phillip. – Wayne Werner Aug 02 '10 at 13:14
  • 5
    so maybe `int(round(2.65))` ? – teewuane Jan 13 '15 at 00:26
  • 1
    why does `round(6.5)` gives 6? It seems to kinda `ceil()` the float when there's an immediate 5 (or greater up to 9) after the decimal in all other cases. Why is this not working in this case? or any other case when the number ends with a six and there's a 5 right after the decimal... – candh Jul 22 '17 at 09:02
  • 1
    candh I believe it uses "banker's rounding"/"rounding half to even" method– rather than rounding 0.5 and higher up, and 0.4 and lower down, it rounds 0.5 to the nearest even number. – Nirvana Jul 01 '21 at 06:22
49

Combining two of the previous results, we have:

int(round(some_float))

This converts a float to an integer fairly dependably.

Matt
  • 3,079
  • 4
  • 30
  • 36
user3763109
  • 501
  • 4
  • 2
  • What happens if you try to round a very long float? Will this at least raise an exception? – Agostino May 01 '15 at 00:04
  • @Agostino What do you mean "very long float"? – kralyk Mar 21 '16 at 11:41
  • @kralyk I mean a `float` representing a number bigger than what a normal `int` can hold. In Python 2, are there `float` values that you can only represent using a `long` (after rounding)? – Agostino Mar 21 '16 at 20:12
  • @kralyk you mean, after the round? So, would casting them to int raise an exception, or just truncate them? – Agostino Mar 22 '16 at 13:59
  • @Agostino No, the `int()` function produces either an `int` or a `long` based on what is needed... – kralyk Mar 23 '16 at 10:25
  • @Agostino https://docs.python.org/2/library/functions.html#int , it says _"If x is a number, it can be a plain integer, a long integer, or a floating point number."_ which is what applies in this situation. – kralyk Mar 23 '16 at 10:50
  • @kralyk thanks, maybe we can clean up the comments and add a note to the answer? – Agostino Mar 23 '16 at 10:54
  • @Agostino I suppose we can yeah – kralyk Mar 23 '16 at 11:07
  • int(float('inf')) raises an Overflow Error. – Tatarize Oct 27 '21 at 20:30
20

That this works is not trivial at all! It's a property of the IEEE floating point representation that int∘floor = ⌊⋅⌋ if the magnitude of the numbers in question is small enough, but different representations are possible where int(floor(2.3)) might be 1.

This post explains why it works in that range.

In a double, you can represent 32bit integers without any problems. There cannot be any rounding issues. More precisely, doubles can represent all integers between and including 253 and -253.

Short explanation: A double can store up to 53 binary digits. When you require more, the number is padded with zeroes on the right.

It follows that 53 ones is the largest number that can be stored without padding. Naturally, all (integer) numbers requiring less digits can be stored accurately.

Adding one to 111(omitted)111 (53 ones) yields 100...000, (53 zeroes). As we know, we can store 53 digits, that makes the rightmost zero padding.

This is where 253 comes from.


More detail: We need to consider how IEEE-754 floating point works.

  1 bit    11 / 8     52 / 23      # bits double/single precision
[ sign |  exponent | mantissa ]

The number is then calculated as follows (excluding special cases that are irrelevant here):

-1sign × 1.mantissa ×2exponent - bias

where bias = 2exponent - 1 - 1, i.e. 1023 and 127 for double/single precision respectively.

Knowing that multiplying by 2X simply shifts all bits X places to the left, it's easy to see that any integer must have all bits in the mantissa that end up right of the decimal point to zero.

Any integer except zero has the following form in binary:

1x...x where the x-es represent the bits to the right of the MSB (most significant bit).

Because we excluded zero, there will always be a MSB that is one—which is why it's not stored. To store the integer, we must bring it into the aforementioned form: -1sign × 1.mantissa ×2exponent - bias.

That's saying the same as shifting the bits over the decimal point until there's only the MSB towards the left of the MSB. All the bits right of the decimal point are then stored in the mantissa.

From this, we can see that we can store at most 52 binary digits apart from the MSB.

It follows that the highest number where all bits are explicitly stored is

111(omitted)111.   that's 53 ones (52 + implicit 1) in the case of doubles.

For this, we need to set the exponent, such that the decimal point will be shifted 52 places. If we were to increase the exponent by one, we cannot know the digit right to the left after the decimal point.

111(omitted)111x.

By convention, it's 0. Setting the entire mantissa to zero, we receive the following number:

100(omitted)00x. = 100(omitted)000.

That's a 1 followed by 53 zeroes, 52 stored and 1 added due to the exponent.

It represents 253, which marks the boundary (both negative and positive) between which we can accurately represent all integers. If we wanted to add one to 253, we would have to set the implicit zero (denoted by the x) to one, but that's impossible.

phant0m
  • 16,595
  • 5
  • 50
  • 82
13

If you need to convert a string float to an int you can use this method.

Example: '38.0' to 38

In order to convert this to an int you can cast it as a float then an int. This will also work for float strings or integer strings.

>>> int(float('38.0'))
38
>>> int(float('38'))
38

Note: This will strip any numbers after the decimal.

>>> int(float('38.2'))
38
brandonbanks
  • 1,125
  • 1
  • 14
  • 21
9

math.floor will always return an integer number and thus int(math.floor(some_float)) will never introduce rounding errors.

The rounding error might already be introduced in math.floor(some_large_float), though, or even when storing a large number in a float in the first place. (Large numbers may lose precision when stored in floats.)

  • 7
    From: http://docs.python.org/2/library/math.html - math.floor(x) - Return the floor of x as a float, the largest integer value less than or equal to x. – Bill Rosmus Jul 31 '13 at 18:24
  • Why do you need to do call math.floor when int already does the same thing? – Alex Aug 25 '17 at 19:23
  • 1
    @Alex: `int` and `floor` return different values for negative numbers, of course. –  Aug 26 '17 at 18:41
2

Another code sample to convert a real/float to an integer using variables. "vel" is a real/float number and converted to the next highest INTEGER, "newvel".

import arcpy.math, os, sys, arcpy.da
.
.
with arcpy.da.SearchCursor(densifybkp,[floseg,vel,Length]) as cursor:
 for row in cursor:
    curvel = float(row[1])
    newvel = int(math.ceil(curvel))
mrichey56
  • 21
  • 1
2

Since you're asking for the 'safest' way, I'll provide another answer other than the top answer.

An easy way to make sure you don't lose any precision is to check if the values would be equal after you convert them.

if int(some_value) == some_value:
     some_value = int(some_value)

If the float is 1.0 for example, 1.0 is equal to 1. So the conversion to int will execute. And if the float is 1.1, int(1.1) equates to 1, and 1.1 != 1. So the value will remain a float and you won't lose any precision.

Troy H
  • 21
  • 2