Learn the Basics of Float in Python - Part 3
Understand Python Float Basics - Part 3
In this article, building upon the foundation laid in Part 1 and Part 2, we will delve deeper into the process of coercing a floating-point number into an integer. This involves taking a floating-point number, extracting only the integer portion, and consequently discarding the fractional part, which inevitably leads to data loss. For instance, when converting a floating-point number like 10.4 into an integer, we have the option to round it up to 11 or down to 10, depending on the specific use case. To accomplish this, we can employ various methods at our disposal, each with its own unique approach.
Truncation
Floor
Ceiling
Rounding
It is crucial to note that, regardless of the method employed, data loss is an inherent consequence of converting a floating-point number to an integer, as the fractional part is always discarded in the process.
Truncation
function to truncate a floating-point number by removing its decimal part and returning the integer part.
>>> import math
>>> math.trunc(10.4)
10
>>> math.trunc(10.5)
10
>>> math.trunc(10.6)
10
>>> math.trunc(-10.4)
-10
>>> math.trunc(-10.5)
-10
>>> math.trunc(-10.6)
-10
The math.trunc()
function takes a single argument, which is the floating-point number you want to truncate, and returns the integer part of that number. It effectively discards the decimal portion of the number and returns the largest integer not greater than the input value.
int Constructor
we can use the int()
constructor to convert a floating-point number to an integer. When you use int()
to convert a float, it effectively truncates the decimal part and returns the integer part of the number. Here's how you can use it:
>>> int(10.4)
10
>>> int(10.5)
10
>>> int(10.6)
10
>>> int(-10.4)
-10
>>> int(-10.5)
-10
>>> int(-10.6)
-10
In the example above, int(float_number)
takes the floating-point number 10.4, -10.4
and converts it to an integer, truncating the decimal part and retaining only the integer portion. Keep in mind that using int()
to convert a float will discard the decimal portion of the number.
Floor
The floor of a number denoted as ⌊x⌋ (the greatest integer less than or equal to x), is the largest integer that is less than or equal to the given number. In mathematical notation, it is defined as:
⌊x⌋ = max {n ∈ ℤ : n ≤ x}
For positive numbers, floor and truncation are equivalent but not for negative numbers. Recall our discussion on integer division also known as floor division //
we defined floor division in combination with mod operation n = d * (n // d) + ( n % d)
But in fact, floor division defined that the way yields the same result as taking the floor of the floating point a // b == floor(a/b)
>>> import math
>>> math.floor(10.4)
10
>>> math.floor(10.5)
10
>>> math.floor(-10.4)
-11
>>> math.floor(-10.5)
-11
The floor function is useful in various mathematical and programming contexts when you need to work with integers and round down values to the nearest integer.
Ceiling
The ceiling of a number denoted as ⌈x⌉ (the smallest integer greater than or equal to x), is the smallest integer that is greater than or equal to the given number. In mathematical notation, it is defined as:
⌈x⌉ = min {n ∈ ℤ : n ≥ x}
>>> import math
>>> math.ceil(10.4)
11
>>> math.ceil(10.5)
11
>>> math.ceil(-10.4)
-10
>>> math.ceil(-10.5)
-10
The ceiling function is useful in various mathematical and programming contexts when you need to work with integers and round up values to the nearest integer.
Rounding
Rounding a floating-point number in Python involves changing the number to the nearest integer or a specified number of decimal places. This will round the number of X to the closest multiple of. In addition to the truncate , floor, and ceil we can therefore also use rounding ( with n = 0 ) to coerce a float to an integer number.
If N is not specified, then it defaults to zero and round(x) will therefore return an int
round(x) --> int
round(x,n) --> same type as x
round(x,0) --> same type as x
if n = 0
if n > 0
in n < 0
>>> round(1.23) # n = 0
1
>>> round(1.23,1) # n > 0
1.2
>>> round(18.2,-1) # n < 0
20.0
Banker's Rounding
Banker's Rounding, also known as "round half to even" or "tie-breaking rounding," is a method of rounding numbers where numbers that are exactly halfway between two possible rounded values are rounded to the nearest even number. This method is commonly used in financial calculations and statistics to minimize rounding bias.
Here's how Banker's Rounding works:
If the digit to the right of the rounding position (the digit being rounded) is less than 5, the number is rounded down (towards zero).
If the digit to the right of the rounding position is greater than 5, the number is rounded up (away from zero).
If the digit to the right of the rounding position is exactly 5, the number is rounded to the nearest even number. If the digit to the left of the rounding position is odd, it is rounded up; if it is even, it is rounded down.
>>> round(1.25,1)
1.2
>>> round(1.35,1)
1.4
>>> round(15,-1)
20
>>> round(25,-1)
20
This method of rounding helps to reduce bias when rounding numbers in statistical calculations and is commonly used in financial applications where consistency in rounding is essential.
Rounding away from zero
If we insist on rounding away from zero then one common way to round to the nearest unit that comes up is the below method but this partially incorrect
>>> int( x + 0.5)
>>> int(10.3 + 0.5)
10
>>> int(10.9 + 0.5)
11
>>> int(10.5 + 0.5)
11
but this method doesn't work for a negative number
>>> int(-10.3 + 0.5)
-9
>>> int(-10.9 + 0.5)
-10
>>> int(-10.5 + 0.5)
-10
Technically, this is also an acceptable rounding method referred to as rounding towards + infinity but this is not rounding towards zero.
The correct way to do it is sign(x) * int (abs(x) + 0.5)
but Python doesn't have sign function!. we can however use math.copysign()
function to achieve our goal. copysign(x, y)
return the magnitude of x with the sign of x.
sign(x) = copysign(1,x)
>>> from math import fabs,copysign
>>> def round_up(x):
return copysign(1,x) * int(fabs(x) + 0.5)
>>> print(round_up(1.5))
2.0
>>> print(round_up(-10.5))
-11.0
Each rounding method has its use cases depending on the specific requirements of your application. Consider which method best suits your needs for rounding floating-point numbers.
In conclusion, understanding various methods of coercing floating-point numbers into integers, such as truncation, floor, ceiling, and rounding, is crucial for precise calculations in Python. Each method offers a unique approach to handling data loss that occurs when discarding the fractional part. By selecting the appropriate method based on your use case, you can minimize rounding bias and ensure accurate results in your programs.