Skip to content

Commit fe61eb2

Browse files
authored
Fix rounding in FFT (fixes #2961) (#2962)
1 parent 8bf7492 commit fe61eb2

File tree

1 file changed

+4
-2
lines changed
  • src/main/java/com/thealgorithms/maths

1 file changed

+4
-2
lines changed

src/main/java/com/thealgorithms/maths/FFT.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,10 @@ public double abs() {
142142
*/
143143
public Complex divide(Complex z) {
144144
Complex temp = new Complex();
145-
temp.real = (this.real * z.real + this.img * z.img) / (z.abs() * z.abs());
146-
temp.img = (this.img * z.real - this.real * z.img) / (z.abs() * z.abs());
145+
double d = z.abs() * z.abs();
146+
d = (double)Math.round(d * 1000000000d) / 1000000000d;
147+
temp.real = (this.real * z.real + this.img * z.img) / (d);
148+
temp.img = (this.img * z.real - this.real * z.img) / (d);
147149
return temp;
148150
}
149151

0 commit comments

Comments
 (0)