= '0')&&(CHAR <= '9')) ) {
// NUMERICAL CONSTANT---------------------
// POINTER TO POSITION IN STR
STR = CHAR ;
TK = 99 ;
for (IC=1;IC<=15;IC++) {
// GET NEXT CHARACTER
INXT = IP+1 ;
CHAR = INSTR.substr(INXT,1) ;
if ( ! isNaN(CHAR) ) {
// PACK IN NEXT CHARACTER
IP++ ;
STR += CHAR ;
}
else {
// CHECK FOR EXPONENTIAL NOTATION
CHAR2 = STR.substr(STR.length-1,1).toUpperCase() ;
if ( ( CHAR.toUpperCase() == 'E' ) ||
( ( CHAR2 =='E' ) && ( CHAR == '-') ) ||
( CHAR == '.' ) ) {
// PACK IN NEXT CHARACTER
IP++ ;
STR += CHAR ;
continue ;
// end if non-numeric char in numerical constant
}
// NUMBER COMPLETE
else { break } ;
} ; // end if else ! isNaN
} ; // end for IC
// GET NUMERIC VALUE {NOTE ALL NUMBERS TREATED AS REAL}
IXVAR++ ;
XVAR[IXVAR] = parseFloat(STR) ;
return TK ;
} ; // end if constant
// ERROR***
alert(' EXPECTING VARIABLE OR CONSTANT') ;
IERR=1 ;
return ;
} ; // end if VARIABLE, CONSTANT, or FUNCTION
// GET OPERATION @@@@@@@@
if ( (OLDTOK>98)||(OLDTOK==0) ) {
// TOKEN VALUE FOR OPERATION---------------------
// ^^^^^EXPONENTIATION
if ( CHAR == '^' ) { TK=6 ; return TK }
// LOOK AHEAD TO NEXT CHARACTER
INXT=IP+1 ;
if ( (CHAR == '*')&&(INSTR.substr(INXT,1)=='*') ) {
TK=6 ;
IP=IP+1 ;
return TK ;
} ; // end if **
// /////DIVISION
if ( CHAR == '/' ) {
TK=4 ;
return TK ;
} ; // end if /
// *****MULTIPLICATION
if ( CHAR == '*' ) {
TK=3 ;
return TK ;
} ; // end if *
// -----SUBTRACTION
if ( CHAR == '-' ) {
TK=2 ;
return TK ;
} ; // end if -
// +++++ADDITION
if ( CHAR == '+' ) {
TK=1 ;
return TK ;
} ; // end if +
// )))))END
if ( CHAR == ')' ) {
TK=0 ;
return TK ;
} ; // end if )
// ERROR***
alert(' EXPECTING OPERATOR') ;
IERR=1 ;
return ;
} ; // end if GET OPERATION
// CHECK FOR A '(' BEGINNING A FUNCTION ARGUMENT @@@@@@@@
if ( (OLDTOK>6)&&(OLDTOK<16) ) {
if ( CHAR == '(' ) { TK=16 ; return TK }
// ERROR***
alert(' EXPECTING "("') ;
IERR=1 ;
return ;
} ; // end if BEGINNING A FUNCTION ARGUMENT
// ERROR**** @@@@@@@@
alert(' PARSER ERROR') ;
IERR=1 ;
return ;
} ; // end function GETTOK
function CALPAR(TK,OP,IOP) {
// CALCULATE THE RESULT OF AN OPERATION.
// THE OPERATION IS CLEARED FROM THE STACK AND
// THE RESULT IS PLACED BACK ON THE STACK.
// IF IERR=1 ON return AN ERROR HAS OCCURED.
//
// SUBROUTINE FOR PARSER
//
// GLOBALS OR FUNCTION PARAMETERS
// STACK[1:6]=STACK FOR CONSTANTS
// ISTCK=STACK POINTER
// OP[0:7]=STACK FOR OPERATORS
// IOP=OPERATOR STACK POINTER
// TOK=CURRENT TOKEN VALUE
// IT=POINTER TO TOKEN ARRAY, CURRENT TOKEN
// ---(X) HAS OCCURED---
if ( ( OP[IOP] == 0 )&&( TK == 0 ) ) return ;
// CHECK FOR FUNCTION
if ( OP[IOP] <= 6 ) {
// UNARY MINUS
if ( OP[IOP] == 5 ) {
STACK[ISTCK] = -STACK[ISTCK] ;
return } ; // end if UNARY MINUS
// CHECK FOR SYNTAX ERROR****
if ( ISTCK == 0 ) {
alert(' SYNTAX ERROR') ;
IP = IPT[IT] ;
IERR=1 ;
return ;
} ; // end if ISTCK==0 ERROR
// POP CONSTANT OR VARIABLE VALUES OFF STACK
X=STACK[ISTCK-1] ;
Y=STACK[ISTCK] ;
ISTCK=ISTCK-1 ;
// +-*/^
switch( OP[IOP] ) {
// EVALUATE
case 1: Z=X+Y ;
break ;
case 2: Z=X-Y ;
break ;
case 3: Z=X*Y ;
break ;
case 4: Z=X/Y ;
break ;
case 6: Z=Math.pow(X,Y) ;
break ;
default:
// ERROR****
alert(' OPERATOR EVALUATION ERROR') ;
IP = IPT[IT] ;
IERR=1 ;
return ;
} ; // end switch
// PUT RESULT BACK ON STACK
STACK[ISTCK] = Z ;
return ;
}
else {
// FUNCTION EVALUATION-----------------------------
// 'ATN','COS','EXP','LNE','LOG','SIN','SQR','TAN'
X=STACK[ISTCK] ;
IC=OP[IOP]-6 ;
switch ( IC ) {
// EVALUATE
case 1: Z=Math.atan(X) ;
break ;
case 2: Z=Math.cos(X) ;
break ;
case 3: Z=Math.exp(X) ;
break ;
case 4: if ( X > 0.0 ) { Z=Math.log(X) }
else {
alert(' LOG OF ZERO OR NEGATIVE ARGUMENT') ;
IP = IPT[IT] ;
IERR=1 ;
return }
break ;
case 5: if ( X > 0.0 ) { Z=Math.log(X)/Math.log(10.0) }
else {
alert(' LOG OF ZERO OR NEGATIVE ARGUMENT') ;
IP = IPT[IT] ;
IERR=1 ;
return }
break ;
case 6: Z=Math.sin(X) ;
break ;
case 7: if ( X >= 0.0 ) { Z=Math.sqrt(X) }
else {
alert(' SQRT OF NEGATIVE ARGUMENT') ;
IP = IPT[IT] ;
IERR=1 ;
return }
break ;
case 8: Z=Math.tan(X) ;
break ;
default:
// ERROR****
alert(' FUNCTION EVALUATION ERROR') ;
IP = IPT[IT] ;
IERR=1 ;
return ;
} ; // end switch
// PUT RESULT BACK ON STACK
STACK[ISTCK] = Z ;
return ;
} ; // end if else FUNCTION
} ; // end function CALPAR
// Significant figure functions
function ord(x) {
return Math.floor(Math.log(Math.abs(x+1e-35))/2.303)
}
// Truncate to n sign. figures
function trunc(x,n) {
c= Math.floor(x*Math.pow(10,-ord(x)+n-1)+.5)/Math.pow(10,-ord(x)+n-1) ;
c = ( Math.abs(c)<1e-20) ? 0 : c ;
return c
}
// round x to one extra sign. figure based on its standard deviation,s.
function round(x,s) {
c= Math.floor(x*Math.pow(10,-ord(s)+1)+.5)/Math.pow(10,-ord(s)+1) ;
return c
} ; // end function round
function fmt(xfx,format) {
// typical call example fmt(x,'#####.####')
var spc = ' ' ;
var idecfmt = format.indexOf('.',0) ;
var iendfmt = format.length-idecfmt-1 ;
var ipstdecpt = format.length-idecfmt-1 ;
if ( xfx>0 ) { xfx += 0.5*Math.pow(10,-ipstdecpt) }
else { xfx -= 0.5*Math.pow(10,-ipstdecpt) }
xstr = ''+xfx ; // casting
idec = xstr.indexOf('.',0) ;
if ( idec < 0 ) {
xstr = xstr + '.' ;
idec = xstr.length-1 ;
}
ilgth = xstr.length ;
iend = ilgth-idec-1 ;
trun = xstr.length-iend+iendfmt ;
zeros = idecfmt-iend-1 ;
xstr = (iend>iendfmt) ? xstr.substring(0,trun) : xstr+spc.substring(0,zeros) ;
// replace trailing zeros
ilgth = xstr.length ;
for ( i=ilgth-1 ; i>idec+1 ; i--) {
if ( xstr.substring(i,i+1) != "0" ) break ;
xstr = xstr.substring(0,i)+" "+xstr.substring(i+1,ilgth)
} ; // end for i replace
//
str = spc.substring(0,idecfmt-idec)+xstr ;
return str ;
} ; // end fmt
//-->
Uncertainty Calculator
Evaluate an Equation
Help
Type in your equation at the "Equation:" prompt.
Input follows "BASIC" type rules:
-
Exponentiation is indicated by ^ or **.
-
LOG() is base 10, LNe() is base e.
-
Allowed functions are ATN, COS, EXP, LNe, LOG, SIN, SQR, TAN.
-
Variables are one or two characters, e.g. I, X, df, X7.
-
Nested parentheses are useful, e.g. ((X+Y)*Z).
-
No implicit multiplication, e.g. ((X+Y)Z) is not allowed.
-
Variables are not case sensitive: x=X.
-
Scientific notation: 1.23x10-3 is written as 1.23E-3.
-
Enter your equation without an "=" sign.
-----Example: -----
To evaluate K2, knowing K1, H, R, T2,and T1
in the equation:
ln(K2/K1) = - H/R( 1/T2 - 1/T1)
Solve for K2.
You would then enter Equation: K1*EXP(-H/R*(1/T2-1/T1))
Colby College Chemistry, T. W. Shattuck