| 1 |
define([ |
| 2 |
"dojo/_base/kernel", |
| 3 |
"dojo/_base/declare", |
| 4 |
"dojo/_base/lang", |
| 5 |
"dojo/number", |
| 6 |
"dijit/_base/manager", |
| 7 |
"dijit/_WidgetBase", |
| 8 |
"dijit/_TemplatedMixin", |
| 9 |
"dojox/math/_base" |
| 10 |
], function(kernel, declare, lang, number, dijit, WidgetBase, TemplatedMixin, math){ |
| 11 |
|
| 12 |
kernel.experimental("dojox.calc"); |
| 13 |
|
| 14 |
var calcEnv, calc; |
| 15 |
|
| 16 |
var magicBigInt = (1 << 30) - 35; |
| 17 |
|
| 18 |
var Executor = declare( |
| 19 |
"dojox.calc._Executor", |
| 20 |
[WidgetBase, TemplatedMixin], |
| 21 |
{ |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
templateString: '<iframe src="' + require.toUrl("dojox/calc/_ExecutorIframe.html") + |
| 27 |
'" style="display:none;" onload="if(arguments[0] && arguments[0].Function)'+dijit._scopeName+'.byNode(this)._onLoad(arguments[0])"></iframe>', |
| 28 |
|
| 29 |
_onLoad: function(env){ |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
calcEnv = env; |
| 34 |
env.outerPrompt = window.prompt; |
| 35 |
|
| 36 |
env.dojox = {math: {}}; |
| 37 |
for(var f in math){ env.dojox.math[f] = lang.hitch(math, f); } |
| 38 |
if("toFrac" in calc){ |
| 39 |
env.toFracCall = lang.hitch(calc, 'toFrac'); |
| 40 |
this.Function('toFrac', 'x', "return toFracCall(x)"); |
| 41 |
} |
| 42 |
|
| 43 |
env.isJavaScriptLanguage = number.format(1.5, {pattern:'#.#'}) == "1.5"; |
| 44 |
env.Ans = 0; |
| 45 |
env.pi = Math.PI; |
| 46 |
env.eps = Math.E; |
| 47 |
|
| 48 |
env.powCall = lang.hitch(calc, 'pow'); |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
this.normalizedFunction('sqrt', 'x', "return Math.sqrt(x)"); |
| 55 |
this.normalizedFunction('sin', 'x', "return Math.sin(x)"); |
| 56 |
this.normalizedFunction('cos', 'x', "return Math.cos(x)"); |
| 57 |
this.normalizedFunction('tan', 'x', "return Math.tan(x)"); |
| 58 |
this.normalizedFunction('asin', 'x', "return Math.asin(x)"); |
| 59 |
this.normalizedFunction('acos', 'x', "return Math.acos(x)"); |
| 60 |
this.normalizedFunction('atan', 'x', "return Math.atan(x)"); |
| 61 |
this.normalizedFunction('atan2', 'y, x', "return Math.atan2(y, x)"); |
| 62 |
this.normalizedFunction('Round', 'x', "return Math.round(x)"); |
| 63 |
this.normalizedFunction('Int', 'x', "return Math.floor(x)"); |
| 64 |
this.normalizedFunction('Ceil', 'x', "return Math.ceil(x)"); |
| 65 |
this.normalizedFunction('ln', 'x', "return Math.log(x)"); |
| 66 |
this.normalizedFunction('log', 'x', "return Math.log(x)/Math.log(10)"); |
| 67 |
this.normalizedFunction('pow', 'x, y', "return powCall(x,y)"); |
| 68 |
this.normalizedFunction('permutations', 'n, r', "return dojox.math.permutations(n, r);"); |
| 69 |
this.normalizedFunction('P', 'n, r', "return dojox.math.permutations(n, r);"); |
| 70 |
this.normalizedFunction('combinations', 'n, r', "return dojox.math.combinations(n, r);"); |
| 71 |
this.normalizedFunction('C', 'n, r', "return dojox.math.combinations(n, r)"); |
| 72 |
|
| 73 |
this.normalizedFunction('toRadix', 'number, baseOut', "if(!baseOut){ baseOut = 10; } if(typeof number == 'string'){ number = parseFloat(number); }return number.toString(baseOut);"); |
| 74 |
this.normalizedFunction('toBin', 'number', "return toRadix(number, 2)"); |
| 75 |
this.normalizedFunction('toOct', 'number', "return toRadix(number, 8)"); |
| 76 |
this.normalizedFunction('toHex', 'number', "return toRadix(number, 16)"); |
| 77 |
this.onLoad(); |
| 78 |
}, |
| 79 |
|
| 80 |
onLoad: function(){ |
| 81 |
|
| 82 |
|
| 83 |
}, |
| 84 |
Function: function(name, args, body){ |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
return lang.hitch(calcEnv, calcEnv.Function.apply(calcEnv, arguments)); |
| 94 |
}, |
| 95 |
normalizedFunction: function(name, args, body){ |
| 96 |
return lang.hitch(calcEnv, calcEnv.normalizedFunction.apply(calcEnv, arguments)); |
| 97 |
}, |
| 98 |
deleteFunction: function(name){ |
| 99 |
calcEnv[name] = undefined; |
| 100 |
delete calcEnv[name]; |
| 101 |
}, |
| 102 |
eval: function(text){ |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
return calcEnv.eval.apply(calcEnv, arguments); |
| 108 |
}, |
| 109 |
destroy: function(){ |
| 110 |
this.inherited(arguments); |
| 111 |
calcEnv = null; |
| 112 |
} |
| 113 |
}); |
| 114 |
|
| 115 |
return calc = { |
| 116 |
pow: function( base, exponent){ |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
function isInt(n){ |
| 122 |
return Math.floor(n) == n; |
| 123 |
} |
| 124 |
|
| 125 |
if(base >= 0 || isInt(exponent)){ |
| 126 |
return Math.pow(base, exponent); |
| 127 |
}else{ |
| 128 |
var inv = 1 / exponent; |
| 129 |
|
| 130 |
return (isInt(inv) && (inv & 1)) ? -Math.pow(-base, exponent) : NaN; |
| 131 |
} |
| 132 |
}, |
| 133 |
approx: function( r){ |
| 134 |
|
| 135 |
|
| 136 |
if(typeof r == "number"){ |
| 137 |
return Math.round(r * magicBigInt) / magicBigInt; |
| 138 |
} |
| 139 |
return r; |
| 140 |
}, |
| 141 |
_Executor: Executor |
| 142 |
}; |
| 143 |
}); |