dartfor-loopdoubledouble-pointer

Incorrectly calculated Double values with for loop in Dart


I have a native app and I want to apply same code with Dart. This equation make to calculate FFT window values.

Java code is:

int n = 1024;
double[] window = new double[n];
for(int i = 0; i < window.length; i++) {
  window[i] = 0.42 - 0.5 * Math.cos(2 * Math.PI * i / (n - 1)) + 0.08 * Math.cos(4 * Math.PI * i / (n - 1));
}

Native code adapted for Dart:

int n = 1024;
List<double> window = List<double>.generate(n, (index) => 0);
for(int i = 0; i < window.length; i++) {
  window[i] = 0.42 - 0.5 * math.cos(2* math.pi*i/(n-1)) + 0.08 * math.cos(4* math.pi*i/(n-1));
}

Java output (window each line) for the same example

I/System.out: -1.3877787807814457E-17
+I/System.out: 3.395133803971162E-6
+I/System.out: 1.3581090180514455E-5
+I/System.out: 3.055953389992372E-5
+I/System.out: 5.433323916569088E-5
+I/System.out: 8.490608899498764E-5
+I/System.out: 1.2228307435091512E-4
+I/System.out: 1.6647029302682692E-4
+I/System.out: 2.1747494828386338E-4
+I/System.out: 2.7530534724047595E-4
+I/System.out: 3.399708990153849E-4
+I/System.out: 4.1148211262373446E-4
+I/System.out: 4.898505946268478E-4
+I/System.out: 5.750890465359981E-4
+I/System.out: 6.672112619706683E-4
+I/System.out: 7.662321235716446E-4
+I/System.out: 8.721675996697376E-4
+I/System.out: 9.850347407101001E-4
I/System.out: 0.001104851675433366
I/System.out: 0.00123163760681394
I/System.out: 0.0013654128077558447
I/System.out: 0.001506198616547605
I/System.out: 0.0016540174320758272
I/System.out: 0.0018088927087991091
I/System.out: 0.0019708489514828897
I/System.out: 0.00213991170969606
I/System.out: 0.0023161075720698615
I/System.out: 0.0024994641603207357
I/System.out: 0.0026900101230373785
I/System.out: 0.0028877751292339104
.
.
.

Dart output (window each line) for the same example

flutter: -1.3877787807814457e-17
-flutter: 0.000003395133803971162
-flutter: 0.000013581090180514455
-flutter: 0.00003055953389992372
-flutter: 0.00005433323916569088
-flutter: 0.00008490608899498764
-flutter: 0.00012228307435091512
-flutter: 0.00016647029302682692
-flutter: 0.00021747494828386338
-flutter: 0.00027530534724047595
-flutter: 0.0003399708990153849
-flutter: 0.00041148211262373446
-flutter: 0.0004898505946268478
-flutter: 0.0005750890465359981
-flutter: 0.0006672112619706683
-flutter: 0.0007662321235716446
-flutter: 0.0008721675996697376
-flutter: 0.0009850347407101001
flutter: 0.001104851675433366
flutter: 0.00123163760681394
flutter: 0.0013654128077558447
flutter: 0.001506198616547605
flutter: 0.0016540174320758272
flutter: 0.0018088927087991091
flutter: 0.0019708489514828897
flutter: 0.00213991170969606
flutter: 0.0023161075720698615
flutter: 0.0024994641603207357
flutter: 0.0026900101230373785
flutter: 0.0028877751292339104
.
.
.

Marked with - symbol values is wrong. As you can see codes same but results are different some point. This difference continues in the next lines that are not visible.

By the way, C# generating same results as Java.

Anyway what is the problem, or what I am missing? Do you hava any idea?

Dart SDK version: 2.18.4 (stable) MacOSX

I am generate same function with C#. It is working with dartpad but in the Android Studio results are weird.


Solution

  • Your value are not different, they are just printed in a different format:

    3.395133803971162E-6 is really the same as 0.000003395133803971162

    This first is in scientific notation, the second is in a plain decimal format.