Matthew Ke
Posts: 0
Joined: Fri Jan 09, 2015 5:29 pm

Do I need to import a math library to use math.pow and math.exp?

I run my test with no errors but its not outputting the correct values. I know the formula's used are correct because the .html file works perfectly. I believe I've made the necessary adjustments for it to work with appery,io but I could be very wrong! Is there a dependency I need to use or anything of the like? any help would be appreciated! Thank you. Here is an snippet of my code:

while (Math.abs(pTarget - pCalc).toFixed(2) 0.01) {
//
// Set rhoCurrent
//
//
if (pTarget pCalc) {
rhoLow = rhoCurrent;
if (rhoCurrent = rhoHigh) {
rhoCurrent += RHO_C;
rhoHigh = rhoCurrent;
} else {
rhoCurrent += ((rhoHigh - rhoCurrent) / 2);
}
} else {
rhoHigh = rhoCurrent;
rhoCurrent += ((rhoLow - rhoCurrent) / 2);
}

//
// Set rhoR
//
//
rhoPrevious = rhoCurrent;
rhoR = rhoCurrent / RHO_C;

//
// Set differential
//
//
diffAggregate = 0;
for (kIndex = 0; kIndex < NK_VALUES.length; kIndex++) {
diffSegment = NK_VALUES[kIndex];
diffSegment *= Math.pow(rhoR, IK_VALUES[kIndex]);
diffSegment *= Math.pow(tempR, JK_VALUES[kIndex]);
iFactor = 0;
if (LK_VALUES[kIndex] 0) {
diffSegment *= Math.exp(-1 * Math.pow(rhoR, LK_VALUES[kIndex]));
iFactor = LK_VALUES[kIndex] * Math.pow(rhoR, LK_VALUES[kIndex]);
}

Matthew Ke
Posts: 0
Joined: Fri Jan 09, 2015 5:29 pm

Do I need to import a math library to use math.pow and math.exp?

I decided to just share my code entirely in hopes that it'll help me figure out whats wrong. Thanks again!!!

var n2TemperatureInput = request.get("n2TemperatureInput");

var n2PressureInput = request.get("n2PressureInput");

var NK_VALUES = [0.924803575275, - 0.492448489428, 0.661883336938, - 1.92902649201, - 0.0622469309629, 0.349943957581, 0.564857472498, - 1.61720005987, - 0.481395031883, 0.421150636384, - 0.0161962230825, 0.172100994165, 0.00735448924933, 0.0168077305479, - 0.00107626664179, - 0.0137318088513, 0.000635466899859, 0.00304432279419, - 0.0435762336045, - 0.0723174889316, 0.0389644315272, - 0.021220136391, 0.00408822981509, - 0.0000551990017984, - 0.0462016716479, - 0.00300311716011, 0.0368825891208, - 0.0025585684622, 0.00896915264558, - 0.0044151337035, 0.00133722924858, 0.000264832491957, 19.6688194015, - 20.915560073, 0.0167788306989, 2627.67566274];
var IK_VALUES = [1, 1, 2, 2, 3, 3, 1, 1, 1, 3, 3, 4, 6, 6, 7, 7, 8, 8, 1, 2, 3, 4, 5, 8, 4, 5, 5, 8, 3, 5, 6, 9, 1, 1, 3, 2];
var JK_VALUES = [0.25, 0.875, 0.5, 0.875, 0.375, 0.75, 0.5, 0.75, 2, 1.25, 3.5, 1, 0.5, 3, 0, 2.75, 0.75, 2.5, 4, 6, 6, 3, 3, 6, 16, 11, 15, 12, 12, 7, 4, 16, 0, 1, 2, 3];
var LK_VALUES = [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 0, 0, 0, 0];
var PK_VALUES = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 20, 20, 15, 25];
var BK_VALUES = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0325, 325, 300, 275];
var UK_VALUES = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.16, 1.16, 1.13, 1.25];
var R_CONST = 8.31451;
var T_C = 126.192;
var P_C = 3.3958;
var MOLECULAR_WEIGHT = 28.01348;
var DENSITY_STP = 1.1851;
var RHO_C = 11.1839;

var rhoCurrent = 0;
var rhoHigh = 0;
var rhoLow = 0;

// CONVERT TEMPERATURE INPUT TO KELVINS
var tempK = n2TemperatureInput + 273.15;

// CONVERT TEMPERATURE (K) TO RANKIN (R)
var tempR = T_C / tempK;

// CONVERT PRESSURE INPUT TO ABSOLUTE PRESSURE (PA)
var pTargetNonShort = (n2PressureInput * 1000000) + 101325;
var pTarget = pTargetNonShort.toFixed(2);

var diffAggregate = 0;
var diffSegment = 0;
var iFactor = 0;
var bFactor = 0;
var kIndex = 0;
var pCalc = 0;
var zDiff = 0;
var rhoR = 0;

while (Math.abs(pTarget - pCalc) 0.01) {
//
// Set rhoCurrent
//
//
if (pTarget pCalc) {
rhoLow = rhoCurrent;
if (rhoCurrent = rhoHigh) {
rhoCurrent += RHO_C;
rhoHigh = rhoCurrent;
} else {
rhoCurrent += ((rhoHigh - rhoCurrent) / 2);
}
} else {
rhoHigh = rhoCurrent;
rhoCurrent += ((rhoLow - rhoCurrent) / 2);
}

//
// Set rhoR
//
//
rhoPrevious = rhoCurrent;
rhoR = rhoCurrent / RHO_C;

//
// Set differential
//
//
diffAggregate = 0;
for (kIndex = 0; kIndex < NK_VALUES.length; kIndex++) {
diffSegment = NK_VALUES[kIndex];
diffSegment *= Math.pow(rhoR, IK_VALUES[kIndex]);
diffSegment *= Math.pow(tempR, JK_VALUES[kIndex]);
iFactor = 0;
if (LK_VALUES[kIndex] 0) {
diffSegment *= Math.exp(-1 * Math.pow(rhoR, LK_VALUES[kIndex]));
iFactor = LK_VALUES[kIndex] * Math.pow(rhoR, LK_VALUES[kIndex]);
}
if (PK_VALUES[kIndex] 0) {
diffSegment *= Math.exp(-1 * PK_VALUES[kIndex] * Math.pow(rhoR, 2) - BK_VALUES[kIndex] * Math.pow(tempR - UK_VALUES[kIndex], 2));
iFactor = 2 * rhoR * PK_VALUES[kIndex] * (rhoR - 1);
}

Code: Select all

 diffAggregate += (diffSegment * (IK_VALUES[kIndex] - iFactor)); 

}

//
// Set pCalc
//
//
zDiff = 1 + diffAggregate;
pCalc = zDiff * R_CONST * 1000 * tempK * rhoCurrent;
}

bFactor = rhoCurrent * MOLECULAR_WEIGHT / DENSITY_STP;
var bFactorShort = bFactor.toFixed(4);

var bFactorStatement = "The B-Factor is " + bFactorShort + " scm/m3";

response.success(JSON.stringify({
greeting: bFactorStatement
}), "application/json");

Yurii Orishchuk
Posts: 0
Joined: Fri Feb 14, 2014 8:20 am

Do I need to import a math library to use math.pow and math.exp?

Hi Matthew,

It seems you have "Server code"..

If so Math.pow(2, 2) and Math.exp(2) - should work in server script without any dependence...

Please use "trace" tab to understand where your code has to be stoped..

Regards.

Return to “Issues”