From fe61eb2bdf2b3834dd5f05ba7309e9a26f6165dd Mon Sep 17 00:00:00 2001 From: leren1 <98456022+leren1@users.noreply.github.com> Date: Sat, 5 Mar 2022 18:16:34 +0100 Subject: [PATCH] Fix rounding in FFT (fixes #2961) (#2962) --- src/main/java/com/thealgorithms/maths/FFT.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/maths/FFT.java b/src/main/java/com/thealgorithms/maths/FFT.java index 56bb89de..374d3fca 100644 --- a/src/main/java/com/thealgorithms/maths/FFT.java +++ b/src/main/java/com/thealgorithms/maths/FFT.java @@ -142,8 +142,10 @@ public class FFT { */ public Complex divide(Complex z) { Complex temp = new Complex(); - temp.real = (this.real * z.real + this.img * z.img) / (z.abs() * z.abs()); - temp.img = (this.img * z.real - this.real * z.img) / (z.abs() * z.abs()); + double d = z.abs() * z.abs(); + d = (double)Math.round(d * 1000000000d) / 1000000000d; + temp.real = (this.real * z.real + this.img * z.img) / (d); + temp.img = (this.img * z.real - this.real * z.img) / (d); return temp; }