float type in Python represents the floating point number. Float is used to represent real numbers and is written with a decimal point dividing the integer and fractional parts. For example, 97.98, 32.3+e18, -32.54e100 all are floating point numbers.
Python float values are represented as 64-bit double-precision values. The maximum value any floating-point number can be is approx 1.8 x 10308. Any number greater than this will be indicated by the string
inf in Python.
Floating-point numbers are represented in computer hardware as base 2 (binary) fractions. For example, the decimal fraction 0.125 has value 1/10 + 2/100 + 5/1000, and in the same way the binary fraction 0.001 has value 0/2 + 0/4 + 1/8. These two fractions have identical values, the only real difference being that the first is written in base 10 fractional notation, and the second in base 2.
Unfortunately, most decimal fractions cannot be represented exactly as binary fractions. A consequence is that, in general, the decimal floating-point numbers you enter are only approximated by the binary floating-point numbers actually stored in the machine.
float type implements the
numbers.Real abstract base class. Returns an expression which is converted into floating point number.
float also has the following additional methods:
float.as_integer_ratio() : Returns a pair of integers whose ratio is exactly equal to the actual float having a positive denominator.In case of infinites, it raises overflow error and value errors on Not a number (NaNs).
7 / 2
float.is_integer() : Returns True in case the float instance is finite with integral value, else, False.
True False True
float.hex() : Returns a representation of a floating-point number as a hexadecimal string.
float.fromhex(s) : Returns the float represented by a hexadecimal string s. String s may have leading and trailing whitespaces.
Note : float.hex() is an instance method, but float.fromhex() is a class method.
- float() in Python
- Python | sympy.Float() method
- Python | Float Layout in Kivy
- Python | Check for float string
- Python | Sort a tuple by its float element
- Python | Ways to sort list of float values
- Python | Filter out integers from float numpy array
- Python program to convert float decimal to Octal number
- List Methods in Python | Set 1 (in, not in, len(), min(), max()...)
- List methods in Python
- Dunder or magic methods in Python
- Accessing Attributes and Methods in Python
- Dictionary Methods in Python | Set 1 (cmp(), len(), items()...)
- Python Input Methods for Competitive Programming
- Python | Implementing 3D Vectors using dunder methods
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.