Methods on Float type in Python

Methods on Float Type in Python 

In Python, Float data types (float) have several built-in methods and operations that you can use to manipulate and perform various operations on floating-point values. There are three distinct numeric types : integers  , floating point number and complex numbers. The float type in Python represents the floating point number.

float type in python

Methods on Float Type in Python

Float is used to represent real numbers. It is written with a decimal point which divides integer and fractional parts.

  • Example:
    • 87.18, 72.4+e18 

Floating-point numbers are represented in computer  as base 2  fraction.

  • Example:
    • the decimal fraction 0.375 has value 3/10 + 7/100 + 5/1000, 
    • binary fraction 0.011 has value 0/2 + 1/4 + 1/8. 

The float type implements the number.real abstract base class.Returns an expression which is converted into floating point number. 

methods on float type in python

Operations on Float : 

  • Add
    • n1 + n2 
  • Difference
    • n1 – n2 
  • Multiplication
    • n1 *  n2
  • Division
    • n1 / n2 (quotient)
  • Floored quotient
    • n1 // n2
  • Remainder of n1 / n2
    • n1 % n2
  • Absolute value
    • abs ( n )
  • Divmod
    • divmod( n1, n2)
      • ( n1//n2, n1 % n2 )
  • Power of Integer
    • pow ( n1 , n2) or n1**n2 

Binary Operations : 

The Python implementation in Floating point bitwise operations (Python recipe) of floating point bitwise operations works by representing numbers in binary that extends infinitely to the left as well as to the right from the fractional point. Because floating point numbers have a signed zero on most architectures it uses one’s complement  for representing negative numbers. which is why you can’t apply bitwise operations to them.

Additional Methods on Float :

  • float.as_integer_ratio() : Returns pair of integer whose ratio is equal to the floating point number. (Raises overflowError on infinites or valueError on NaNs.)
  • float.is_integer() : Returns True if the float instance is finite with integral value and False otherwise.
  • float.hex() : Return a representation of a floating-point number as a hexadecimal string. For finite floating-point numbers, this representation will always include a leading 0x and a trailing p and exponent.
  • float.fromhex(s) : Returns the float represented by a hexadecimal string s. String s may have leading and trailing whitespaces.
  • float.fromhex(s) : Class method to return the float represented by a hexadecimal string s. The string s may have leading and trailing whitespace.

Example :

Run
n1 = 2.6
n2 =5.2
#quotient 
print('Quotient :' , n2 / n1)
#floored quotient
print('Floored quotient :', n2 // n1)
#Remainder
print('Remainder :',n2 % n1)
#power
print('Power 2.6**5.2 :' , 2.6**5.2)
#float.as_integer_ratio()
d=3.5
print('float.as_integer_ratio() of 3.5 :' , d.as_integer_ratio())
#float.is_integer()
print('float.is_integer() of 5.2 :' ,(n2).is_integer())
#float.hex()
print('float.hex() of 5.2' , float.hex(n2))
#float.fromhex()
print('float.fromhex() of ' , float.hex(n2) ,':' , float.fromhex(float.hex(n2)))
Output :

Quotient : 2.0
Floored quotient : 2.0
Remainder : 0.0
Power 2.6**5.2 : 143.8339507086478
float.as_integer_ratio() of 3.5 : (7, 2)
float.is_integer() of 5.2 : False
float.hex() of 5.2 0x1.4cccccccccccdp+2
float.fromhex() of 0x1.4cccccccccccdp+2 : 5.2

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription