![]() |
VOOZH | about |
A float (floating-point number) is a data type used to represent real numbers with a fractional component. It is commonly used to store decimal values and perform mathematical calculations that require precision.
Example:
Python provides several built-in methods for float objects.
float Type in Python| Method | Description |
|---|---|
| float.as_integer_ratio() | Returns a tuple representing the float as a ratio of two integers. |
| float.conjugate() | Returns the same float value (useful for compatibility with complex numbers). |
float.fromhex(s) | Converts a hexadecimal string to a float. (Static method) |
| float.hex() | Returns the hexadecimal representation of the float. |
| float.is_integer() | Returns True if the float is an integer (has no decimal part), else False. |
| float.__abs__() | Returns the absolute value of the float. |
| float.__add__(other) | Adds two float values (self + other). |
| float.__sub__(other) | Subtracts two float values (self - other). |
| float.__mul__(other) | Multiplies two float values (self * other). |
| float.__truediv__(other) | Performs true division (self / other). |
| float.__floordiv__(other) | Performs floor division (self // other). |
| float.__mod__(other) | Returns the remainder of division (self % other). |
| float.__pow__(other) | Returns the float raised to the power of other (self ** other). |
| float.__round__(n) | Rounds the float to n decimal places. |
The as_integer_ratio() method returns a tuple of two integers, whose ratio equals the float. This method is useful for precise representation of floating-point numbers as fractions, which can help avoid floating-point precision errors in arithmetic calculations. The returned integers represent the numerator and denominator of the fraction.
(11, 4)
Here, 2.75 is exactly represented as 11/4. The method breaks down the float into an exact fraction by multiplying it by a power of 2 internally and simplifying it into two integers.
float.conjugate()The conjugate() method returns the same float value. This method exists primarily for compatibility with complex numbers, where the conjugate of a complex number negates its imaginary part. Since a float has no imaginary part, calling conjugate() on a float simply returns itself.
5.5
This method does not modify the float; it just returns the same value. It ensures compatibility when working with complex numbers, where the conjugate of a + bi is a - bi.
The fromhex() method converts a hexadecimal string representation of a floating-point number into a float. This is useful when dealing with binary representations or low-level floating-point operations.
3.14
The hexadecimal string represents a floating-point number in scientific notation. The p+1 denotes a power of two exponent. Converting from hexadecimal ensures precise representation of binary floating-point numbers.
The hex() method returns the hexadecimal representation of a float. This is useful for debugging floating-point precision issues and for storing exact binary representations.
0x1.91eb851eb851fp+1
The output is a hexadecimal scientific notation representing the float. The p+1 means multiplying by 2^1. This format is useful for exact floating-point storage and computation.
The is_integer() method checks if a float has no decimal part and returns True if it is equivalent to an integer. This is useful when working with numerical computations where integer-like behavior is required.
True False
Here, 4.0 is equivalent to the integer 4, so is_integer() returns True. However, 4.5 has a decimal part, so it returns False.
The __abs__() method returns the absolute value of a float, which is the non-negative version of the number. It is equivalent to the built-in abs() function.
7.3 7.3
The negative value -7.3 is converted to its positive equivalent 7.3. This is useful when working with distances, magnitudes, or other computations where only the positive value is needed.
The __add__() method performs addition between two float values. This is automatically used when we use the + operator.
7.7 7.7
Here, 5.5 + 2.2 results in 7.7. The + operator internally calls the __add__() method.
The __sub__() method performs subtraction between two float values. It is used when we apply the - operator.
7.3 7.3
Here, 10.5 - 3.2 results in 7.3. The - operator calls __sub__() internally.
The __mul__() method performs multiplication between two float values.
8.4 8.4
Multiplication of 4.2 * 2.0 results in 8.4. The * operator invokes __mul__() internally.
The __truediv__() method performs true division (returns a float even when dividing two integers).
3.0 3.0
The division 7.5 / 2.5 results in 3.0, ensuring a floating-point output.
The __floordiv__() method performs floor division, which returns the largest integer less than or equal to the quotient.
3.0 3.0
The quotient is 3.0 with no remainder, so the floor division is the same as normal division here.
The __mod__() method returns the remainder of division.
2.5 2.5
Here, 10.5 / 4.0 results in 2 with a remainder of 2.5.
The __pow__() method raises the float to the power of another number.
9.0 9.0
The expression 3.0 ** 2.0 calculates 3.0 raised to the power of 2.0, which is 9.0.
The __round__() method rounds the float to n decimal places.
3.14 3.14
The float 3.14159 is rounded to 3.14 when specifying 2 decimal places.