297. signum — The -1 / 0 / +1 You Keep Building With an If-Else Ladder
Every “which direction?” check ends up as the same three-branch ladder: greater, less, equal. signum is that ladder as one method call — with a float twist you need to know about.
The ladder
You want the direction of a difference — for a comparator, a game step, a sort key:
| |
One call
| |
It shines whenever you need to move one step toward a target, whatever the distance:
| |
No overshoot, no separate branches for “target is behind me” — the sign does the steering.
The float trap
f64::signum is not the same function. It reports the sign bit, so zero is never 0.0:
| |
If you want the integer-style three-way answer for floats, be explicit about zero:
| |
Reach for signum on integers without a second thought; on floats, remember it answers “which sign bit?” — not “is this positive, negative, or zero?”.