Link to home
Start Free TrialLog in
Avatar of billyleo
billyleo

asked on

NaN error when converting vba to java

Hi,

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

vba:
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)))

java:
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
Avatar of RishadanPort
RishadanPort

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.

-->CONSTANTS.setDcr(..)

-->CONSTANTS.getDcrp1()

-->CONSTANTS.getDcrp2()

-->CONSTANTS.getDiff()

Also another difference is this part:

ARRAYS.momARRAY[s0][s1][g0][g1][0][0][serv][l1][l2]

here it is treating momArray directly as a 9 dimensional array, while the VBA code is a function call.
>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
Please check for each of the components return correct values and data type:

CONSTANTS.getDcrp1()
CONSTANTS.getDcrp2()
ARRAYS.MAsy[1]
ARRAYS.MAsy[0]
CONSTANTS.getDiff()
ARRAYS.momARRAY[s0][s1][g0][g1][0][0][serv][l1][l2]

Especially the following:
ARRAYS.MAsy[0] = 0 ?
ARRAYS.MAsy[1]-ARRAYS.MAsy[0] = 0?
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])

when
ARRAYS.MAsy[1] = ARRAYS.MAsy[0] = 0
Avatar of billyleo

ASKER

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!
ASKER CERTIFIED SOLUTION
Avatar of Peter Kwan
Peter Kwan
Flag of Hong Kong image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.

Yippee