Change language

# Python | Float type and methods

Python floating point values ​​are represented as double 64 bit values. The maximum value that any floating point number can take is approximately 1.8 x 10 308 . Any number greater than this will be indicated by the string ` inf ` in Python.

 ` # Python code to show floating point values. `   ` print ` ` ( ` ` 1.7e308 ` `) ` ` `  ` # more than 1.8 * 10 ^ 308 ` ` # will print & # 39; inf & # 39; ` ` print ` ` (` ` 1.82e308 ` `) `

Exit :

` 1.7e + 308 inf `

Floating point numbers are represented in computer hardware as binary fractions. For example, the decimal fraction 0.125 has the value 1/10 + 2/100 + 5/1000, and similarly the binary fraction 0.001 has the value 0/2 + 0/4 + 1/8. These two fractions have the same meaning, the only real difference is that the first is written in fractional notation with base 10, and the second is — base 2.
Unfortunately, most decimal fractions cannot be represented exactly as binary fractions. A consequence of this is that, as a rule, the floating point decimal numbers entered are approximated only by the binary floating point numbers actually stored in the machine.

The ` float ` type implements ` numbers .Real ` is an abstract base class. Returns an expression that is converted to floating point. ` float ` also has the following additional methods:

as_integer_ratio-python-reduced-fraction-given-rational/ rel = noopener target = _blank> float.as_integer_ratio (): returns a pair of integers whose ratio is exactly equal to the actual floating point value with a positive denominator. In the case of infinity, this causes an overflow error and value errors for Not a Number (NaNs).

 ` # Python3 illustration program ` ` # working of float.as_integer_ratio () `   ` def ` ` frac (d): ` ` `  ` # Using as_integer_ratio ` ` b ` ` = ` ` d.as_integer_ratio () `   ` return ` ` b ` ` # Driver code ` ` if ` ` __ name__ ` ` = ` ` = ` ` ’__main__’ ` `: ` ` ` ` b ` ` = ` ` frac (` ` 3.5 ` `) ` ` print ` ` (b [` ` 0 ` `], ` ` "/" ` `, b [` ` 1 ` `]) `

Exit:

` 7/2 `

float.is_integer (): Returns True if the floating point instance is finite with an integer value, otherwise False.

` `

` # Python3 illustration program # working of float.is_integer ()   def booln ( ):   # using is_integer print (( - 5.0 ). is_integer ()) print (( 4.8 ). is_integer ()) print ( float . is_integer ( 275.0 ))   # Driver code if __ name__ = = ’__main__’ : booln () `

` `

Exit:

` True False True `

float.hex (): returns a hexadecimal string representation of a float.

 ` # Python3 illustration program ` ` # working of float.hex () ` ` `  ` def ` ` frac (a): `   ` # using float.hex () ` ` a ` ` = ` ` float ` `. ` ` hex ` ` (` ` 35.0 ` `) `   ` return ` ` a ` ` # Driver code ` ` if ` ` __ name__ ` ` = ` ` = ` ` ’__main__’ ` `: ` ` b ` ` = ` ` frac (` ` 35.0 ` `) ` ` print ` ` (b) `

Exit :

` ’0x1.1800000000000p + 5’ `

float.fromhex (s): returns a floating point number represented by the hex string s ... S lines can have leading and trailing spaces.

 ` # Python3 illustration program ` ` # working of float.fromhex () `   ` def ` ` frac (a): `   ` # using float.fromhex () ` ` ` ` a ` ` = ` ` float ` `. fromhex (` ` ’0x1.1800000000000p + 5’ ` `) ` ` `  ` return ` ` a ` ` `  ` # Driver code ` ` if ` ` __ name__ ` ` = ` ` = ` ` ’__main__’ ` `: ` ` b ` ` = ` ` frac (` ` ’0x1.1800000000000p + 5’ ` `) ` ` print ` ` (b) `

Output:

` 35.0 `

Note: float.hex () is an instance method, but float.fromhex () is a class method.

## Shop

Best Python online courses for 2022

\$FREE

Best laptop for Excel

\$

Best laptop for Solidworks

\$399+

Best laptop for Roblox

\$399+

Best computer for crypto mining

\$499+

Best laptop for Sims 4

\$

Best laptop for Zoom

\$499

Best laptop for Minecraft

\$590

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

PythonStackOverflow

Check if one list is a subset of another in Python

PythonStackOverflow

How to specify multiple return types using type-hints

PythonStackOverflow

Printing words vertically in Python

PythonStackOverflow

Python Extract words from a given string

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

PythonStackOverflow

Python os.path.join () method

PythonStackOverflow

Flake8: Ignore specific warning for entire file

## Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries