Quaternions are a number system that extends the familiar complex numbers, and they are the standard mathematical tool for representing rotations in three dimensions. They were discovered by William Rowan Hamilton in 1843, famously carved into a bridge in Dublin when the defining relation struck him during a walk. A quaternion has four components: one real part and three imaginary parts, usually written as q = w + xi + yj + zk, where i, j, and k are imaginary units obeying the relations i² = j² = k² = ijk = −1. That compact definition generates a rich algebra, and its most important application is one that every modern video game, robotics system, and spacecraft attitude controller relies on: rotating objects smoothly and without the failure modes of older methods.
The multiplication rules follow from Hamilton's relations. The units multiply as: i×j = k, j×k = i, k×i = j, but reversing the order flips the sign, so j×i = −k, k×j = −i, and i×k = −j. That non-commutativity, the fact that the order of multiplication matters, is the single most important feature of quaternions, and it mirrors the geometry of rotations themselves: rotating an object about the x-axis and then the y-axis gives a different result from rotating about the y-axis and then the x-axis. Any calculation that assumes quaternion multiplication commutes is wrong, and the order convention must be stated whenever quaternions are used in code, because different libraries adopt different conventions for which side represents the first rotation.
The general multiplication formula for two quaternions q1 = (w1, x1, y1, z1) and q2 = (w2, x2, y2, z2) is: the real part is w1×w2 − x1×x2 − y1×y2 − z1×z2; the i part is w1×x2 + x1×w2 + y1×z2 − z1×y2; the j part is w1×y2 − x1×z2 + y1×w2 + z1×x2; and the k part is w1×z2 + x1×y2 − y1×x2 + z1×w2. It looks formidable, but each component follows from expanding the product term by term and applying the unit rules, and working through one example by hand makes the pattern clear.
Consider a worked example. Let q = (1, 2, 3, 4) and p = (2, 0, 1, 3), where the components are listed in the order (w, x, y, z). The real part is 1×2 − 2×0 − 3×1 − 4×3, which is 2 − 0 − 3 − 12, which is −13. The i part is 1×0 + 2×2 + 3×3 − 4×1, which is 0 + 4 + 9 − 4, which is 9. The j part is 1×1 − 2×3 + 3×2 + 4×0, which is 1 − 6 + 6 + 0, which is 1. The k part is 1×3 + 2×1 − 3×0 + 4×2, which is 3 + 2 − 0 + 8, which is 13. So q × p = (−13, 9, 1, 13). The result can be checked by norms: the squared norm of a quaternion is the sum of the squares of its components. For q, that is 1 + 4 + 9 + 16, which is 30. For p, it is 4 + 0 + 1 + 9, which is 14. The product's squared norm should be 30 × 14, which is 420, and indeed (−13)² + 9² + 1² + 13² is 169 + 81 + 1 + 169, which is 420. The check passes, which strongly suggests the multiplication is correct. If the two orders were swapped, p × q would give a different result, and computing it is a good exercise for confirming that non-commutativity is real rather than theoretical.
The conjugate and the norm complete the basic toolkit. The conjugate of q = (w, x, y, z) is q* = (w, −x, −y, −z), the same real part with the imaginary parts negated. The norm is the square root of the sum of the squared components, and a unit quaternion is one whose norm is one. Unit quaternions are the ones that represent rotations, because multiplying by a unit quaternion preserves lengths. The inverse of a unit quaternion is simply its conjugate, which makes undoing a rotation as easy as conjugating, a convenience that matrix methods do not share so cheaply.
Rotation itself uses a beautifully compact formula. To rotate a vector v by a unit quaternion q, form the quaternion (0, v) with zero real part, compute q × (0, v) × q*, and read off the imaginary part as the rotated vector. A rotation through an angle θ about a unit axis (ax, ay, az) is encoded as q = (cos(θ/2), ax×sin(θ/2), ay×sin(θ/2), az×sin(θ/2)). Note the half angle: a quaternion stores half the rotation angle, which is why two quaternions that differ only by an overall sign, q and −q, represent the same rotation. Consider a concrete rotation: rotating the vector (1, 0, 0) by 90 degrees about the z-axis. Half the angle is 45 degrees, and cos(45°) and sin(45°) are both about 0.7071, so q = (0.7071, 0, 0, 0.7071). Applying the rotation formula gives the vector (0, 1, 0), exactly as geometry predicts: the x-axis rotated a quarter turn about the z-axis points along the y-axis. The formula reproduces the obvious answer, which is the right first test for any rotation method.
Why use quaternions instead of the more familiar Euler angles, the roll, pitch, and yaw that describe most aircraft attitudes? Euler angles suffer from gimbal lock, a configuration where two rotation axes align and one degree of freedom is lost, causing jerky or undefined behaviour. They also interpolate badly: animating smoothly between two orientations by interpolating Euler angles produces unnatural paths with varying speed. Quaternions suffer from neither problem. Spherical linear interpolation between two unit quaternions follows the shortest arc at constant speed, which is why game engines and animation systems use it for camera and character motion. Quaternions are also more compact than rotation matrices, four numbers instead of nine, and composing rotations is a single multiplication rather than a matrix product, though converting to a matrix is still done when a graphics pipeline requires it.
There are practical pitfalls. Normalisation drift: repeated multiplication and interpolation accumulate rounding error, so quaternions that should stay unit slowly drift away from unit length, and periodic renormalisation is standard practice. Sign ambiguity: because q and −q represent the same rotation, interpolation must choose the sign that gives the shorter path, or an animation will spin the long way around. Convention confusion: some systems store the scalar part first, others last, and some define multiplication in the opposite order, so code copied between libraries without checking conventions will rotate objects incorrectly in ways that look plausible but are wrong. And quaternions represent rotations only; they do not represent translations or scaling, so full rigid-body motion combines a quaternion with a separate translation vector.
For most users, the takeaway is not to implement quaternion arithmetic from scratch, but to understand what the tools are doing. When a 3D engine, a robotics library, or a flight controller works with quaternions, it is using them for exactly the reasons above: compact, smooth, gimbal-lock-free rotation. Knowing the multiplication formula and the rotation formula lets you read that code, check a result, and diagnose the common failures of drift, sign, and convention, rather than treating the rotation system as magic.
Try it next: use the Quaternion Calculator to check a multiplication, then verify the arithmetic with the Scientific Calculator and explore related geometry with the Average Calculator. For background reading, see Scientific Notation Explained.