mirror of
https://github.com/josdejong/mathjs.git
synced 2026-01-25 15:07:57 +00:00
Changed calculation of math.sqrt to avoid numerical problems when re < 0
This commit is contained in:
parent
24cdfb90a7
commit
2ef53ce069
@ -52,17 +52,28 @@ module.exports = function (math) {
|
||||
|
||||
if (isComplex(x)) {
|
||||
var r = Math.sqrt(x.re * x.re + x.im * x.im);
|
||||
if (x.im >= 0) {
|
||||
return new Complex(
|
||||
0.5 * Math.sqrt(2.0 * (r + x.re)),
|
||||
0.5 * Math.sqrt(2.0 * (r - x.re))
|
||||
);
|
||||
|
||||
var re, im;
|
||||
|
||||
if (x.re >= 0) {
|
||||
re = 0.5 * Math.sqrt(2.0 * (r + x.re));
|
||||
}
|
||||
else {
|
||||
return new Complex(
|
||||
0.5 * Math.sqrt(2.0 * (r + x.re)),
|
||||
-0.5 * Math.sqrt(2.0 * (r - x.re))
|
||||
);
|
||||
re = Math.abs(x.im) / Math.sqrt(2 * (r - x.re));
|
||||
}
|
||||
|
||||
if (x.re <= 0) {
|
||||
im = 0.5 * Math.sqrt(2.0 * (r - x.re));
|
||||
}
|
||||
else {
|
||||
im = Math.abs(x.im) / Math.sqrt(2 * (r + x.re));
|
||||
}
|
||||
|
||||
if (x.im >= 0) {
|
||||
return new Complex(re, im);
|
||||
}
|
||||
else {
|
||||
return new Complex(re, -im);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user