Float Type Complexity
The float type represents floating-point numbers with fixed precision (64-bit IEEE 754 double).
Arithmetic Operations
| Operation |
Time |
Space |
Notes |
x + y |
O(1) |
O(1) |
IEEE 754 addition |
x - y |
O(1) |
O(1) |
Subtraction |
x * y |
O(1) |
O(1) |
Multiplication |
x / y |
O(1) |
O(1) |
Division |
x // y |
O(1) |
O(1) |
Floor division |
x % y |
O(1) |
O(1) |
Modulo |
x ** y |
O(1) |
O(1) |
Exponentiation |
divmod(x, y) |
O(1) |
O(1) |
Combined division |
abs(x) |
O(1) |
O(1) |
Absolute value |
-x |
O(1) |
O(1) |
Negation |
Comparison Operations
| Operation |
Time |
Space |
Notes |
x == y |
O(1) |
O(1) |
Equality check |
x < y |
O(1) |
O(1) |
Less than |
x > y |
O(1) |
O(1) |
Greater than |
x <= y |
O(1) |
O(1) |
Less or equal |
x >= y |
O(1) |
O(1) |
Greater or equal |
x != y |
O(1) |
O(1) |
Not equal |
x is y |
O(1) |
O(1) |
Object identity |
Special Methods
| Operation |
Time |
Space |
Notes |
hash(x) |
O(1) |
O(1) |
Hash value |
str(x) |
O(1) |
O(1) |
String conversion |
repr(x) |
O(1) |
O(1) |
Representation |
int(x) |
O(1) |
O(1) |
Convert to int |
float(x) |
O(1) |
O(1) |
Convert from other |
round(x, n) |
O(1) |
O(1) |
Round to n places |
math.floor(x) |
O(1) |
O(1) |
Floor |
math.ceil(x) |
O(1) |
O(1) |
Ceiling |
math.trunc(x) |
O(1) |
O(1) |
Truncate |
Instance Methods
| Method |
Time |
Space |
Notes |
is_integer() |
O(1) |
O(1) |
Check if float is whole number |
as_integer_ratio() |
O(1) |
O(1) |
Returns exact (numerator, denominator) tuple |
hex() |
O(1) |
O(1) |
Convert to hexadecimal string |
fromhex(s) |
O(n) |
O(1) |
Class method; create float from hex string |
conjugate() |
O(1) |
O(1) |
Returns self; complex number compatibility |
from_number(x) |
O(1) |
O(1) |
Class method; convert number to float (Python 3.14+) |
Numeric Attributes
| Attribute |
Time |
Notes |
real |
O(1) |
Returns self; real part |
imag |
O(1) |
Always 0.0; imaginary part |
Common Operations
Basic Arithmetic
# All operations are constant time (fixed 64-bit precision)
x = 3.14
y = 2.71
sum_result = x + y # O(1)
product = x * y # O(1)
quotient = x / y # O(1)
exponent = x ** y # O(1) - built-in, not iterative
Rounding and Truncation
import math
x = 3.14159
# All O(1)
rounded = round(x, 2) # 3.14
floored = math.floor(x) # 3
ceiled = math.ceil(x) # 4
truncated = math.trunc(x) # 3
Comparison and Equality
# All comparisons are O(1)
x = 1.0
y = 1.0
if x == y: # O(1)
pass
if x < y + 0.001: # O(1)
pass
Fixed-size Operations
# All float operations are hardware-assisted - O(1)
import timeit
# Addition
timeit.timeit('1.5 + 2.5', number=1000000)
# Division
timeit.timeit('10.0 / 3.0', number=1000000)
# Complex computation
timeit.timeit('(1.5 + 2.5) * (10.0 / 3.0)', number=1000000)
Type Conversion Overhead
x = 5
y = 5.0
# int to float - O(1)
z = float(x)
# float to int - O(1)
w = int(y)
# Repeated conversions are still O(1) but can add up
for i in range(1000000):
_ = float(i) # O(1) each, O(n) total for loop
Version Notes
- Python 2.x: Separate
int and float division operators
- Python 3.x: Unified
/ operator always returns float
- All versions: Uses IEEE 754 double precision
- Int - Arbitrary precision integers
- Complex - Complex floating-point numbers
- Decimal - Arbitrary precision decimal
- Fraction - Rational numbers
Further Reading