| 1 | /* complex.cpp |
| 2 | * |
| 3 | * Copyright (C) 1992-2005,2011,2016-2018,2020 Paul Boersma |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or (at |
| 8 | * your option) any later version. |
| 9 | * |
| 10 | * This code is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 | * See the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #include "melder.h"cxx . . $ `<file>/melder/melder.h`/ |
| 20 | |
| 21 | dcomplexcxx . . $ dcomplex# dcomplex_sqrtcxx . . $ dcomplex_sqrt(b85d93c470fe5288). (dcomplexcxx . . $ dcomplex# z) { |
| 22 | dcomplexcxx . . $ dcomplex# resultcxx . . $ std/complex#complex(bc12c9ec15586e6a).; |
| 23 | double x, y, w, r; |
| 24 | if (z.realcxx . . $ std/complex#real(21b6c25e127938e2).() == 0 && z.imagcxx . . $ std/complex#imag(21b6c25e127938e2).() == 0) { |
| 25 | result. realcxx . . $ std/complex#real(ba8836b59fb42830). (0.0); |
| 26 | result. imagcxx . . $ std/complex#imag(ba8836b59fb42830). (0.0); |
| 27 | return resultcxx . . $ std/complex#complex(20ef1611af677bca).; |
| 28 | } |
| 29 | x = fabscxx . . $ fabs(fee4f1d22bc8e617). (z.realcxx . . $ std/complex#real(21b6c25e127938e2).()); |
| 30 | y = fabscxx . . $ fabs(fee4f1d22bc8e617). (z.imagcxx . . $ std/complex#imag(21b6c25e127938e2).()); |
| 31 | if (x >= y) { |
| 32 | r = y / x; |
| 33 | w = sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (x) * sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (0.5 * (1.0 + sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (1.0 + r * r))); |
| 34 | } else { |
| 35 | r = x / y; |
| 36 | w = sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (y) * sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (0.5 * (r + sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (1.0 + r * r))); |
| 37 | } |
| 38 | if (z.realcxx . . $ std/complex#real(21b6c25e127938e2).() >= 0.0) { |
| 39 | result. realcxx . . $ std/complex#real(ba8836b59fb42830). (w); |
| 40 | result. imagcxx . . $ std/complex#imag(ba8836b59fb42830). (z.imagcxx . . $ std/complex#imag(21b6c25e127938e2).() / (2.0 * w)); |
| 41 | } else { |
| 42 | result. imagcxx . . $ std/complex#imag(ba8836b59fb42830). ((z.imagcxx . . $ std/complex#imag(21b6c25e127938e2).() >= 0) ? w : -w); |
| 43 | result. realcxx . . $ std/complex#real(ba8836b59fb42830). (z.imagcxx . . $ std/complex#imag(21b6c25e127938e2).() / (2.0 * result.imagcxx . . $ std/complex#imag(21b6c25e127938e2).())); |
| 44 | } |
| 45 | return resultcxx . . $ std/complex#complex(20ef1611af677bca).; |
| 46 | } |
| 47 | |
| 48 | /* End of file complex.cpp */ |