Float Precision in Python

Hello, pycoders! I hope you are having a good time learning to code in the Python universe. So let us learn about a few basic ‘Python Datatypes’ and deep dive mainly on the concept of  ‘float precision in python’ later in the post.

Ways to perform float precision in Python:

Python has a few built-in data types. Few of the data types most commonly used are categorized according to their use cases as follows:

  1. Text Data type: str
  2. Numeric Data Types: int, float, complex
  3. Mapping Data Type: dict
  4. Sequence Data Types: list, tuple, range
  5. Boolean Data Type: bool

Let us dive into the numeric data type float. And later into the setting of precision for the subsequent float values.

FLOAT: This data type holds the real number values.

Example Of Float Number:

>>>num=3.898

>>>print(type(num))

Output:

<class'float'>

 

Float Precision in Python is performed in numerous ways using different methods as below:

  • Using the modulus operator %:

The modulus operator is used in setting the desired float precision value in the ‘printf ‘ statement before printing the values.

Example On Floating Point Precision using Modulus Operator:

x=4.7968524130
# the following line will print the value of var a till 2 decimal places
print('%.2f'%x)

Output:

4.80

For instance ‘x’ be any number, place x in the position of ‘2’  in the code. The x value you put will produce output until that decimal places.

x=4.7968524130
# the following line will print the value of var a till 3 decimal places
print('%.3f'%x)

# the following line will print the value of var a till 6 decimal places
print('%.6f'%x)

Output:

4.797

4.796852

  • Using the format() function :

We can use the format() function in python for string precision.

format() function is used using { } .format(value) syntax.

Example On Float Precision using Format() function:

 

x=4.7968524130

# The following line will print the float precision values using format() function:

print ("{0:.3f}".format(x))

 

Output:

4.797
  • Using trunc() function:

The truncate function uses a ‘math’ module and so we need to import the math module first.

The truncate function in Python ‘truncates all the values from the decimal (floating) point’.

It removes the floating part of the number and returns an integer value.

Example On Float Precision using trunc() function:

 

import math     #importing math module for float point precision function

x=4.7968524130

# The following line will print the float precision values using trunc() function:

print (math.trunc(x)) 

 

Output:

4

  • Using round(a,b) function:

The round function takes 2 arguments. It is a built-in function of python. This function gives a round of value of the given float number.

The first argument  ‘a’ in the function is the number that can be passed directly or through a variable. And the second argument  ‘b’ is the number of decimal places you want the function to round off.

Example On Float Precision using round(a,b) function:

 

x=4.7968524130

# The following line will print the float precision values using round() function:

print(round(x,4)) 

#in the above statement the first argument x is the variable containing the float number and the second argument 4 will round off the till 4 places.

 

Output:

4.7969

So finally concluding the post I would like to say that try and run these floating-point precision methods practically in your system. Which in turn will give you a better insight into the floating precision concept and practicing practically will only make these concepts more strongly embedded in your cody fingers and Pythonista brains. Also, think about use cases where you can apply each of these methods. And keep coding, my pycoders and have a bug free day!!!

Also read:

Leave a Reply

Your email address will not be published. Required fields are marked *