NaN error when converting vba to java

Posted on 2008-11-04
Last Modified: 2010-04-21

Im finally running the project that was converted from vba to java unfortunately i'm not receiving the proper output. This is because vba seems alot more tolerant of using '0's in equations than java does. eg

dcr = 1 / (1 + dcrp1 * Exp(dcrp2 * Exp(0.5 * (MAsy(1) - MAsy(0)) / MAsy(0)) _
    * diff * mom(s0, s1, g0, g1, 0, 0, serv, l1, l2)))

CONSTANTS.setDcr(1 / (1 + CONSTANTS.getDcrp1() * Math.exp(CONSTANTS.getDcrp2() * Math.exp(0.5 * (ARRAYS.MAsy[1] - ARRAYS.MAsy[0]) / ARRAYS.MAsy[0])
                * CONSTANTS.getDiff() * ARRAYS.momARRAY[s0][s1][g0][g1][0][0][serv][l1][l2])));

in both sitautions the array 'mom' for the given indexs is 0, however the vba version assigns a value to dcr whereas the java versions makes dcr 'NaN'

I understand what the problem is but cant (in my situation) think of a solution (except rewriting the whole code which is pretty much a nono because its not my logic and i dont really understand it and there's thousands of lines)

Any suggestions much appreciated
Question by:billyleo
    LVL 6

    Expert Comment

    Are you sure there isn't a problem in one of the function calls to retreive certain variables? Unlike in your VBA code where you reference the variable directly, in your Java code, you are making function calls to retreives the data.





    Also another difference is this part:


    here it is treating momArray directly as a 9 dimensional array, while the VBA code is a function call.
    LVL 6

    Expert Comment

    >Im finally running the project that was converted from vba to java unfortunately i'm not receiving the >proper output. This is because vba seems alot more tolerant of using '0's in equations than java does.

    I highly doubt that this is the case. I am quite sure that your conversation that has been translated into java has some errors in it
    LVL 16

    Expert Comment

    by:Peter Kwan
    Please check for each of the components return correct values and data type:


    Especially the following:
    ARRAYS.MAsy[0] = 0 ?
    ARRAYS.MAsy[1]-ARRAYS.MAsy[0] = 0?
    LVL 16

    Expert Comment

    by:Peter Kwan
    Since one of the way that would cause your value of dcr is NaN is this expression:
    Math.exp(0.5 * (ARRAYS.MAsy[1] - ARRAYS.MAsy[0]) / ARRAYS.MAsy[0])

    ARRAYS.MAsy[1] = ARRAYS.MAsy[0] = 0

    Author Comment

    CONSTANTS.getDcrp1() returns correct value - in the current case - 0.074881

    CONSTANTS.getDcrp2() - 3.830579999999994

    CONSTANTS.getDiff()  - 0.1

    ARRAYS.MAsy[1] - 2
    ARRAYS.MAsy[0] - 2

    ARRAYS.momARRAY[s0][s1][g0][g1][0][0][serv][l1][l2]  is usually 0 but i changed it to 1^-9

    but these are the same values in the vba version and a definite value is produced!
    LVL 16

    Accepted Solution

    I have tested your values, it should be outputting a value (0.93...).

    Since your expression is quite complex, you may try to print out the values of :

    Math.exp(0.5 * (ARRAYS.MAsy[1] - ARRAYS.MAsy[0]) / ARRAYS.MAsy[0])
     Math.exp(CONSTANTS.getDcrp2() * Math.exp(0.5 * (ARRAYS.MAsy[1] - ARRAYS.MAsy[0]) / ARRAYS.MAsy[0])
                    * CONSTANTS.getDiff() * ARRAYS.momARRAY[s0][s1][g0][g1][0][0][serv][l1][l2])

    to debug yourself. If you still have problems, please post your complete code.

    Author Closing Comment

    Thanks for the help - you were right the Nan reference is coming from some other part of the code - now to trawl through 6000+ lines and 3000 + iterations to find it.


    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    Suggested Solutions

    Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    This video teaches viewers about errors in exception handling.
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now