- SetPrecision 0.2.0
- Навигация
- Ссылки проекта
- Статистика
- Метаданные
- Сопровождающие
- Классификаторы
- Описание проекта
- SetPrecision
- About
- Installation
- Using PIP (Recommended)
- Manual Installation
- Usage
- Contact the Author
- Float Precision in Python
- Ways to perform float precision in Python:
- Float Precision in Python is performed in numerous ways using different methods as below:
SetPrecision 0.2.0
Python module providing an easy way to set the precision of a floating-point number/decimal to the desired amount of significant figures.
Навигация
Ссылки проекта
Статистика
Метаданные
Лицензия: GNU General Public License v3 (GPLv3)
Метки SetPrecision, float, decimal, precision, sig, figs, significant, digits, scientific, notation, math, conversion, script, point, floating, scientific-notation
Сопровождающие
Классификаторы
- Development Status
- 3 — Alpha
- Developers
- End Users/Desktop
- OSI Approved :: GNU General Public License v3 (GPLv3)
- English
- Microsoft :: Windows
- Python :: 3
- Python :: 3.9
- Python :: 3.10
- Scientific/Engineering :: Mathematics
- Software Development :: Libraries :: Python Modules
- Utilities
Описание проекта
SetPrecision
About
- SetPrecision is a small module providing a simple way to set the precision of a floating point number or decimal to the desired amount of digits following the decimal point.
Installation
Using PIP (Recommended)
Easiest method. Highly recommended over manual installation.
Manual Installation
- Before use, navigate to intended installation location, and create a new directory.
- Clone repository with the git client of your preference using the following command:
git clone https://github.com/schlopp96/SetPrecision/releases/latest
pip install -r requirements.txt
- (Optional): move installation directory to «path/to/Python/Libs/site_packages» to be able to import this package to a Python program like any other importable package.
- Done!
Usage
- In order to use SetPrecision, start by importing the module to your Python environment:
Note that the output is automatically rounded up when number >= 5 , and down when number < 5 .
- Both params can be entered in string format, and will output successfully assuming that both parameters can be cast to their appropriate types.
- This is done automatically.
Contact the Author
- If you have any questions, comments, or concerns that cannot be alleviated through the project’s GitHub repository, please feel free to contact me through my email address:
- schloppdaddy@gmail.com
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:
- Text Data type: str
- Numeric Data Types: int, float, complex
- Mapping Data Type: dict
- Sequence Data Types: list, tuple, range
- 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:
Float Precision in Python is performed in numerous ways using different methods as below:
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)
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)
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 ("".format(x))
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))
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.
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.