## Question

Asked By – Boaz

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?

**Now we will see solution for issue: Safest way to convert float to integer in python? **

## Answer

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 2

^{24}can be exactly represented in the single precision format, and any integer with absolute value less than or equal to 2^{53}can be exactly represented in the double precision format.

This question is answered By – Philipp

**This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 **