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

# 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

# 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

Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$
Gifts for programmers

Best laptop for Zoom

$499
Gifts for programmers

Best laptop for Minecraft

$590

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

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

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


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

Python add suffix / add prefix to strings in a list

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

Python - Move item to the end of the list

Python - Print list vertically