0

I am working on project in the university and I am facing a serious problem. In fact, my code is to sum over 2 variables, in order to do some staff after. As I will show below, the list is not empty, and the code extracts the values from the list, but while but it doesn't increment the counter. I will apreciate if someone could give me some help.

        //CHeck if we have already imported data, if yes we use it, unless we import
        if ((this.stockHistory != null) && (!(this.stockHistory.isEmpty())))
          {
            evolution = this.stockHistory;

          }
        else 
          {
            evolution = getHostoricalData(cloned);
          }
        // Here I am sure the list ( evolution ) is not empty, in my case it has 63 elements
        double spot ;
        double sommeTendance = 0 ;
        double sommeVarianceQuotidienne = 0 ;
        for (int i=0;i<evolution.size()-1; i++)
            {
                double i1 =evolution.get(i+1) ;
                System.out.println(" i1 = " + i1);
                double i0 = evolution.get(i) ;
                System.out.println("i0 is " + i0);
                sommeTendance+= Math.log(i1/i0);
                System.out.println("tendance == " +sommeTendance);
                sommeVarianceQuotidienne += Math.pow(Math.log(i1/i0),2);
                System.out.println("variance daily  == " + sommeVarianceQuotidienne);
            }
        System.out.println("size == " + evolution.size());
        System.out.println("total tendance == " + sommeTendance);
        System.out.println("total daily variance == " + sommeVarianceQuotidienne);

And here is the last lines of the output

16:59:52,524 INFO  [stdout] (default task-62)  i1 = 51.619999
16:59:52,524 INFO  [stdout] (default task-62) i0 is 52.73
16:59:52,524 INFO  [stdout] (default task-62) tendance == NaN
16:59:52,524 INFO  [stdout] (default task-62) variance daily  == NaN
16:59:52,524 INFO  [stdout] (default task-62)  i1 = 53.330002
16:59:52,524 INFO  [stdout] (default task-62) i0 is 51.619999
16:59:52,525 INFO  [stdout] (default task-62) tendance == NaN
16:59:52,525 INFO  [stdout] (default task-62) variance daily  == NaN
16:59:52,525 INFO  [stdout] (default task-62) size == 63
16:59:52,525 INFO  [stdout] (default task-62) total tendance == NaN
16:59:52,525 INFO  [stdout] (default task-62) total daily variance == NaN
TwiN
  • 3,554
  • 1
  • 20
  • 31

1 Answers1

0

First, you need to understand what NaN stands for. Nan stands for Not a Number.

How you get NaN?

“Nan” is produced if a floating point operation has some input parameters that cause the operation to produce some undefined result.

What may cause NaN?

For example, 0.0 divided by 0.0 is arithmetically undefined. Finding out the square root of a negative number too is undefined.

Also,

All numeric operations with NaN as an operand produce NaN as a result. Reason behind this is that NaN is unordered, so a numeric comparison operation involving one or two NaNs returns false.

Since we fully understand NaN, now let's work our way around the solution now.


In your case the NaN is produced by Math.log(i1/i0); Logarithmic functions are not defined on (-oo,0]. Hence every negative number entered as parameter Math.log(i1/i0); will return a NaN as result in java.

What you can do?

-> Build a tiny test on i1/i0 before passing it as parameter in Math.log()


Suggestion:

for (int i=0;i<evolution.size()-1; i++)
    {
        double i1 =evolution.get(i+1) ;
        double i0 = evolution.get(i) ;
        if (i1/i0>0)
        {
            //you are good 
            sommeTendance+= Math.log(i1/i0);
            sommeVarianceQuotidienne += Math.pow(Math.log(i1/i0),2);
        }
        else
        {
            //you are not good, the number will yield an undefined result 
            //do something else... like displaying an error....?
            System.out.println("Math.log will yield into an undefined result. Please check your input");
        }
    }

Useful links:

Hope this helps.

Oussama Ben Ghorbel
  • 2,132
  • 4
  • 17
  • 34