A quaternion is a number with four components: one real part and three imaginary parts. Written out, it looks like q = w + xi + yj + zk, where w, x, y, and z are ordinary real numbers, and i, j, and k are three distinct “imaginary” units. If that sounds like a souped-up version of a complex number (which has one real and one imaginary part), that’s exactly what it is. Quaternions were invented to extend arithmetic into higher dimensions, and today they’re the standard tool for representing 3D rotations in video games, aerospace systems, and robotics.
The Four Components
You can think of a quaternion as two pieces glued together: a scalar part (w) and a vector part (x, y, z). The scalar is just a single number. The vector part points in some direction in 3D space. Together, these four values let a quaternion encode both an axis and an amount of rotation in a compact package.
The magnitude of a quaternion works like an extension of the Pythagorean theorem into four dimensions: you square all four components, add them up, and take the square root. When that magnitude equals exactly 1, you have a “unit quaternion,” and unit quaternions are the ones used to represent rotations. Any quaternion can be scaled down to unit length by dividing each component by its magnitude, the same way you’d normalize a regular vector.
How Quaternion Multiplication Works
The most important thing about quaternions is how they multiply. The three imaginary units follow specific rules that William Rowan Hamilton defined in 1843:
- i × i = j × j = k × k = −1
- i × j = k, but j × i = −k
- j × k = i, but k × j = −i
- k × i = j, but i × k = −j
Notice that the order matters. Swapping the two terms flips the sign of the result. This property, called non-commutativity, is what makes quaternions different from ordinary numbers and even from complex numbers, where multiplication order doesn’t matter. It’s also what makes them powerful enough to capture 3D rotation, because rotations in three dimensions are themselves non-commutative. Rotating an airplane nose-up then banking right produces a different final orientation than banking right then rotating nose-up.
Where They Came From
Irish mathematician William Rowan Hamilton spent years trying to extend complex numbers into three dimensions and kept failing. On October 16, 1843, while walking along the Royal Canal in Dublin with his wife, the solution hit him: he needed four dimensions, not three. The key equation was i² = j² = k² = ijk = −1. Hamilton was so excited that he carved the formula into the stone of the nearby Broome Bridge with his penknife. The bridge still has a plaque commemorating the moment.
Quaternions fit into a natural hierarchy of number systems. Start with real numbers (one dimension), double the dimension to get complex numbers (two dimensions), double again to get quaternions (four dimensions), and double once more to get octonions (eight dimensions). This sequence, called the Cayley-Dickson construction, produces exactly four types of “normed division algebras,” and no more exist beyond octonions. Each step sacrifices a property: complex numbers lose the ordering of real numbers, quaternions lose commutative multiplication, and octonions lose associative multiplication.
Representing 3D Rotations
The most common use of quaternions is encoding a rotation in three-dimensional space. A unit quaternion represents a rotation of some angle θ around some axis (described by a unit vector v) by storing cos(θ/2) as the scalar part and v × sin(θ/2) as the vector part. The half-angle might seem odd, but it’s what makes the math work cleanly when you combine multiple rotations.
To actually rotate a point, you sandwich it between the quaternion and its inverse. If your quaternion is q and the point is r, the rotated point is q × r × q⁻¹. This “sandwich product” automatically produces the correct rotation without you needing to think about trigonometry or build a matrix. Combining two rotations is just as easy: multiply the two quaternions together, and the result is a single quaternion representing both rotations applied in sequence.
Why Not Just Use Angles?
The most intuitive way to describe an orientation is with three angles: pitch, yaw, and roll (formally called Euler angles). But this representation has a fatal flaw called gimbal lock. It happens when two of the three rotation axes line up with each other, collapsing your three degrees of freedom into two. At that point, infinitely many different angle combinations produce the same orientation, and smooth rotation through that configuration becomes impossible.
The classic example: if you pitch an object exactly 90 degrees, the yaw and roll axes become parallel. You can no longer distinguish between yaw and roll because adjusting either one produces the same motion. This isn’t a software bug or a rounding error. It’s a mathematical certainty built into the Euler angle representation. It happens with every possible combination of axes whenever two rotation axes become parallel.
Quaternions avoid gimbal lock entirely because they don’t decompose rotation into sequential steps around fixed axes. A single quaternion smoothly and uniquely represents any orientation in 3D space (up to a sign flip, since q and −q represent the same rotation). There are no degenerate configurations where the representation breaks down.
Smoother Animations With SLERP
Beyond avoiding gimbal lock, quaternions offer another practical advantage: smooth interpolation between orientations. If you want to animate an object rotating from one orientation to another, you need to calculate all the in-between orientations. With Euler angles, simply blending the three angle values often produces wobbly, uneven motion with sudden jerks.
Quaternions enable a technique called spherical linear interpolation (SLERP), which traces the shortest, most uniform path between two orientations on the surface of a four-dimensional sphere. The result is rotation that looks natural and moves at a constant angular speed, with no singularities or ugly artifacts. This is the standard approach in game engines, 3D animation software, and any system that needs to blend between orientations.
Computational Efficiency
A quaternion stores a rotation using just 4 numbers. A rotation matrix needs 9. That difference matters when you’re tracking the orientation of thousands of objects every frame, as a game engine or physics simulation does.
The savings extend to arithmetic, too. Combining two rotations by multiplying quaternions takes 16 multiplications and 12 additions. Doing the same thing with 3×3 rotation matrices requires 27 multiplications and 18 additions, roughly 70% more work. If you already have a rotation matrix in hand, applying it to a single vector is fast (9 multiplications, 6 additions). But if you’re storing rotations as quaternions and need to rotate many vectors, converting to a matrix first and then applying it is often the best strategy.
Quaternions also stay numerically stable over time. Rotation matrices can gradually drift away from being true rotation matrices due to floating-point rounding errors, requiring periodic correction. Re-normalizing a quaternion (dividing by its magnitude) is a single, cheap operation. Re-orthogonalizing a 3×3 matrix is considerably more involved.
Where Quaternions Show Up Today
Game engines like Unity and Unreal Engine use quaternions as their internal representation for object orientation. When you rotate a character, a camera, or a projectile in a modern game, quaternion math is almost certainly running behind the scenes. Developers can often work with higher-level tools without writing quaternion code directly, but the option is always there when precise rotational control is needed.
Aerospace and satellite systems rely on quaternions to track spacecraft attitude (orientation relative to a reference frame). The International Space Station, GPS satellites, and Mars rovers all use quaternion-based orientation systems. Robotics uses them for controlling joint angles in robotic arms, where smooth, singularity-free rotation is critical for safety and precision. Computer vision and augmented reality systems use quaternions to track the orientation of cameras and devices in real time.
Even outside engineering, quaternions appear in physics. They provide a natural framework for describing spin in quantum mechanics, and they simplify calculations in certain areas of electromagnetism and general relativity.

