![]() ![]() The original code would compute \(0\) for the real part, but the new version did not have this error. Then, a square root is taken (leaving approximately \(\sqrt + i 10^5\). So, the value of \(x² + y²\) is rounded, and if \(x\) is large and \(y\) is small, \(y\) is ignored or truncated. But when this expression is evaluated with floating-point operators and values, it can yield wildly inaccurate results.Īs I discussed in Floating Point Guarantees, every time a basic floating point operation (like addition, multiplication, or square root) is executed, the ideal real number result has to be rounded back to a value representable as a float. This is a mathematician’s definition, and works when \(x\) and \(y\) are infinite-precision real numbers. Var r = Math.sqrt(x.re * x.re + x.im * x.im) ![]() With this fix, Herbie found an improved way of calculating complex square roots. Simplification rearranges expressions to cancel like terms (turning a sums like \(a + b - a + c - b\) into the simpler \(c\)) the old version couldn’t cancel pairs like \(-x Discouraged, I went on to other things, but a few weeks later Alex, who’s been doing some fantastic heavy lifting in the Herbie internals, made a big improvement to our simplification engine. Herbie is designed to require minimal expert knowledge, so there aren’t knobs to tweak or prompts to respond to.Īt first, Herbie wasn’t finding any expressions that it could improve (though it did find several to be inaccurate). All told, I extracted 28 floating-point expressions.Īfter the tedious work of translating Javascript to Scheme, Herbie runs with little manual intervention. Each math.js function resulted in multiple Herbie expressions: functions of a complex number separately return a real and an imaginary part, two separate Herbie expressions. I manually translated from math.js’s arithmetic functions into this format the functions which include complex number multiplication, exponentiation, and roots. For now, Herbie’s input must be a single expression, written in a stilted Scheme dialect. Math.js contains many functions, and I’m far from passing them all through Herbie. In this post, I’m going to dissect the bug, describe how Herbie fixed it, and talk a bit about the process of using Herbie. We perform the derivation in a motivated way. I submitted a patch, and it’s now been merged into math.js proper. We show how to explicity compute the square roots of a complex number that is given in rectangular form. The semantics of this function are intended to be consistent with the C function csqrt.After spending the last few months working on Herbie, a tool for automatically fixing rounding errors in floating-point code, my colleagues and I have had a small triumph: Herbie found a bug 1, and a corresponding fix, in math.js, an open-source mathematics library. If z is (NaN,NaN), the result is (NaN,NaN).If z is (NaN,y), the result is (NaN,NaN) and FE_INVALID may be raised.If z is (+∞,NaN), the result is (+∞,NaN).If z is (-∞,NaN), the result is (NaN,∞) (sign of imaginary part unspecified).If z is (+∞,y), the result is (+∞,+0) for finite positive y.If z is (-∞,y), the result is (+0,+∞) for finite positive y.If z is (x,NaN), the result is (NaN,NaN) (unless x is ±∞) and FE_INVALID may be raised.If z is (x,+∞), the result is (+∞,+∞) even if x is NaN.std:: sqrt ( std:: conj (z ) ) = std:: conj ( std:: sqrt (z ) ).The function is continuous onto the branch cut taking into account the sign of imaginary part.If the implementation supports IEEE floating-point arithmetic, Error handling and special valuesĮrrors are reported consistent with math_errhandling. If no errors occur, returns the square root of z, in the range of the right half-plane, including the imaginary axis ( [0 +∞) along the real axis and (−∞ +∞) along the imaginary axis). ![]() Complex number to take the square root of ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |