|
|
|
|
|
by owisd
1256 days ago
|
|
My 'favourite' is that the quadratic formula -b±sqrt(b²-4ac)/2a falls apart when you solve for the positive solution using floating point for cases where ε=b²/4ac is small, the workaround being to use the binomial expansion -b/2a*(0.5ε-0.125ε²+O(ε³)) |
|
The way I approach this[1] is to choose the sign of the ± to be the same as -b to compute the first root x1. Then the second root is c/(a * x1). There are a few other checks in the code for things overflowing, but that basically gives you very accurate answers across the range.
This is explained a bit in a good Math Exchange post[2]. Jim Blinn's "How to solve a quadratic equation" also makes good reading.
Wait til you get to cubics and quartics.
[1]: https://docs.rs/kurbo/latest/src/kurbo/common.rs.html#116-16...
[2]: https://math.stackexchange.com/questions/866331/numerically-...