It's been a bad month for train derailments. Canada and France recently suffered two of the worst railway accidents in each country's history. And Wednesday in northern Spain, a passenger train crashed at the bend of a tight corner, killing more than a third of its 200 passengers. The train was reportedly traveling at twice the 50 mph speed limit for the curve.

Train derailments, especially the dramatic and deadly ones, are fairly uncommon. Although more than 300 derailments happened in the U.S. in 2012, "most derailments are track-caused, and do not indicate by any means failure of a carrier" says Warren Flatau, a spokesperson for the Federal Railroad Administration, which oversees the United States railroads. "Just like roads, it's a harsh environment exposed to the elements."

Only a sliver of all derailments are caused by misjudging speed around turns, he says, and that's largely because railroads have stringent speed limits in place nationwide. To set those limits, railroad engineers have to calculate how fast a speeding train can take a corner.

A train traveling on a straight track can't tip over as long as the train's center of gravity is directly above its wheels (the imaginary line from the center of gravity to the ground is called the line of force). But the line of force changes when a train takes a corner. Depending on three factors—the weight of the train, the sharpness of the curve, and the speed that the train is traveling—the train will encounter a new force, centripetal centrifugal force, which pushes against the side of the train.

Because a new force is now acting on the train alongside gravity, the line of force tilts to become an average between the downward pull of gravity and the sideways centrifugal push. So if a train car slightly turns to the left, the line of force might shift a bit toward the right-side wheels. If the train is too light, the curve is too sharp, or the train is traveling too fast, the centrifugal force can push the line of force out beyond the wheels. In that case, the train is no longer balanced on the tracks and can topple over.

The center of gravity is also subject to change from train car to train car. A bottom-heavy locomotive, for example, will have a harder time derailing than a cargo car with a lot of weight toward the top.

To calculate an appropriate speed limit, railroad engineers have to weigh in all the aforementioned factors. "It's not done on a wholesale basis," Flatau says, "basically what we're looking at is track structure and track geometry." None of this is guesswork: "There are tables and tables that spell out the elevation and curvature to give a maximum allowable curving speed."

The resulting speed limit also includes a safety factor—a margin of error to eliminate the danger of any other unknown forces that could act on the turning train, such as wind, bad track conditions, and so on. "There is a science behind all of this," Flatau says. "It's one that railroad practitioners have been perfecting for the better part of two centuries."

We're still waiting for an investigation to reveal why the conductor of the ill-fated Spanish train took that corner so fast. But by doing so, he was tempting fate—and physics.