float.conjugate()
An identity function, present for the same reason as int.conjugate — float is registered as a Complex number and must honour the interface.
Common call
x.conjugate()
Returns
x — always, for every float
Replaces
isinstance(v, complex) branching in numeric code
Watch out
on float it does nothing; the meaning only appears on complex
float.conjugate()
→ float
Demo
Live evaluation
Try:
Inputs
nfloata number, e.g. 2.5
Output
float(2.5).conjugate()
2.5
Every case returns its input. Conjugation negates the imaginary part of a complex number, and a float has none, so there is nothing to change. The method exists so that int, float and complex all answer the same call — which lets a formula written for complex numbers stay correct, and free, on real ones.
Common patterns
Magnitude squared for any numeric type
z * z.conjugate() is real for complex and a plain square for float.
magnitude_sq = (z * z.conjugate()).real
Type-agnostic inner product
The conjugate belongs in the formula and costs nothing on real inputs.
dot = sum(a * b.conjugate() for a, b in zip(xs, ys))
Write the maths once
One implementation stays correct for float and complex callers alike.
def norm(z): return abs(z * z.conjugate()) ** 0.5
Examples
1. Returns itself
(2.5).conjugate()
Returns
2.52. Sign is kept
(-1.5).conjugate()
Returns
-1.53. Zero
(0.0).conjugate()
Returns
0.04. Where it matters
(3+4j).conjugate()
Returns
(3-4j)5. int too
(5).conjugate()
Returns
56. Real part is self
(2.5).real
Returns
2.5Pitfalls
1. It is not negation
Conjugation flips the sign of the IMAGINARY part only. Using it to negate a real number hands the value straight back, with no error to signal the mistake.
Expected -2.5
(2.5).conjugate()
2.5
Negate instead
-2.5
-2.5
2. It looks like dead code on floats
A call that always returns its input invites deletion. Removing it quietly breaks the complex case the formula was written to handle.
Tempting to remove
dot = sum(a * b for a, b in zip(xs, ys))
wrong once ys holds complex numbers
Keep it, note why
dot = sum(a * b.conjugate() for a, b in zip(xs, ys)) # correct for complex too
correct for every numeric type
3. Negative zero survives
Conjugating -0.0 returns -0.0, not 0.0. The two compare equal, so the difference only shows in repr or when dividing, where the sign of zero changes the direction of infinity.
Sign preserved
(-0.0).conjugate()
-0.0
Normalise if needed
(-0.0).conjugate() + 0.0
0.0
When to use
Use it
- Numeric code that must accept float and complex through one path
- Inner products, norms and magnitude formulas in their general form
- Implementing numbers.Complex on a custom numeric type
Reach for something else
- You know the value is a float — it returns the input unchanged
- You meant to negate → use -x
- You want the real or imaginary part → the .real and .imag attributes
Notes
Complexity
O(1) — returns the same value
Return
The float itself, sign of zero included
CPython impl
Objects/floatobject.c :: float_conjugate
Memory
No allocation
Thread-safe
Yes — floats are immutable
FAQ
PEP 3141 registers float in the numeric tower as a numbers.Complex, and that interface requires conjugate(), real and imag. float satisfies it the only way a real number can: the conjugate is itself and imag is 0.0.
import numbers isinstance(2.5, numbers.Complex) # True
History
2.6
Added with PEP 3141, which registered float in the numbers ABC tower.