Math源码java_nativemath.java 源代码在线查看 - java中比较著名的js引擎当属mozilla开源的rhino 资源下载 虫虫电子下载站...

/* -*- Mode: java; tab-width: 4; indent-tabs-mode: 1; c-basic-offset: 4 -*- * * ***** BEGIN LICENSE BLOCK ***** * Version: MPL 1.1/GPL 2.0 * * The contents of this file are subject to the Mozilla Public License Version * 1.1 (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * The Original Code is Rhino code, released * May 6, 1999. * * The Initial Developer of the Original Code is * Netscape Communications Corporation. * Portions created by the Initial Developer are Copyright (C) 1997-1999 * the Initial Developer. All Rights Reserved. * * Contributor(s): * Norris Boyd * Igor Bukanov * * Alternatively, the contents of this file may be used under the terms of * the GNU General Public License Version 2 or later (the "GPL"), in which * case the provisions of the GPL are applicable instead of those above. If * you wish to allow use of your version of this file only under the terms of * the GPL and not to allow others to use your version of this file under the * MPL, indicate your decision by deleting the provisions above and replacing * them with the notice and other provisions required by the GPL. If you do * not delete the provisions above, a recipient may use your version of this * file under either the MPL or the GPL. * * ***** END LICENSE BLOCK ***** */package org.mozilla.javascript;/** * This class implements the Math native object. * See ECMA 15.8. * @author Norris Boyd */final class NativeMath extends IdScriptableObject{ static final long serialVersionUID = -8838847185801131569L; private static final Object MATH_TAG = new Object(); static void init(Scriptable scope, boolean sealed) { NativeMath obj = new NativeMath(); obj.activatePrototypeMap(MAX_ID); obj.setPrototype(getObjectPrototype(scope)); obj.setParentScope(scope); if (sealed) { obj.sealObject(); } ScriptableObject.defineProperty(scope, "Math", obj, ScriptableObject.DONTENUM); } private NativeMath() { } public String getClassName() { return "Math"; } protected void initPrototypeId(int id) { if (id String name; int arity; switch (id) { case Id_toSource: arity = 0; name = "toSource"; break; case Id_abs: arity = 1; name = "abs"; break; case Id_acos: arity = 1; name = "acos"; break; case Id_asin: arity = 1; name = "asin"; break; case Id_atan: arity = 1; name = "atan"; break; case Id_atan2: arity = 2; name = "atan2"; break; case Id_ceil: arity = 1; name = "ceil"; break; case Id_cos: arity = 1; name = "cos"; break; case Id_exp: arity = 1; name = "exp"; break; case Id_floor: arity = 1; name = "floor"; break; case Id_log: arity = 1; name = "log"; break; case Id_max: arity = 2; name = "max"; break; case Id_min: arity = 2; name = "min"; break; case Id_pow: arity = 2; name = "pow"; break; case Id_random: arity = 0; name = "random"; break; case Id_round: arity = 1; name = "round"; break; case Id_sin: arity = 1; name = "sin"; break; case Id_sqrt: arity = 1; name = "sqrt"; break; case Id_tan: arity = 1; name = "tan"; break; default: throw new IllegalStateException(String.valueOf(id)); } initPrototypeMethod(MATH_TAG, id, name, arity); } else { String name; double x; switch (id) { case Id_E: x = Math.E; name = "E"; break; case Id_PI: x = Math.PI; name = "PI"; break; case Id_LN10: x = 2.302585092994046; name = "LN10"; break; case Id_LN2: x = 0.6931471805599453; name = "LN2"; break; case Id_LOG2E: x = 1.4426950408889634; name = "LOG2E"; break; case Id_LOG10E: x = 0.4342944819032518; name = "LOG10E"; break; case Id_SQRT1_2: x = 0.7071067811865476; name = "SQRT1_2"; break; case Id_SQRT2: x = 1.4142135623730951; name = "SQRT2"; break; default: throw new IllegalStateException(String.valueOf(id)); } initPrototypeValue(id, name, ScriptRuntime.wrapNumber(x), DONTENUM | READONLY | PERMANENT); } } public Object execIdCall(IdFunctionObject f, Context cx, Scriptable scope, Scriptable thisObj, Object[] args) { if (!f.hasTag(MATH_TAG)) { return super.execIdCall(f, cx, scope, thisObj, args); } double x; int methodId = f.methodId(); switch (methodId) { case Id_toSource: return "Math"; case Id_abs: x = ScriptRuntime.toNumber(args, 0); // abs(-0.0) should be 0.0, but -0.0 < 0.0 == false x = (x == 0.0) ? 0.0 : (x < 0.0) ? -x : x; break; case Id_acos: case Id_asin: x = ScriptRuntime.toNumber(args, 0); if (x == x && -1.0 x = (methodId == Id_acos) ? Math.acos(x) : Math.asin(x); } else { x = Double.NaN; } break; case Id_atan: x = ScriptRuntime.toNumber(args, 0); x = Math.atan(x); break; case Id_atan2: x = ScriptRuntime.toNumber(args, 0); x = Math.atan2(x, ScriptRuntime.toNumber(args, 1)); break; case Id_ceil: x = ScriptRuntime.toNumber(args, 0); x = Math.ceil(x); break; case Id_cos: x = ScriptRuntime.toNumber(args, 0); x = (x == Double.POSITIVE_INFINITY || x == Double.NEGATIVE_INFINITY) ? Double.NaN : Math.cos(x); break; case Id_exp: x = ScriptRuntime.toNumber(args, 0); x = (x == Double.POSITIVE_INFINITY) ? x : (x == Double.NEGATIVE_INFINITY) ? 0.0 : Math.exp(x); break; case Id_floor: x = ScriptRuntime.toNumber(args, 0); x = Math.floor(x); break; case Id_log: x = ScriptRuntime.toNumber(args, 0); // Java's log( x = (x < 0) ? Double.NaN : Math.log(x); break; case Id_max: case Id_min: x = (methodId == Id_max) ? Double.NEGATIVE_INFINITY : Double.POSITIVE_INFINITY; for (int i = 0; i != args.length; ++i) { double d = ScriptRuntime.toNumber(args[i]); if (d != d) { x = d; // NaN break; } if (methodId == Id_max) { // if (x < d) x = d; does not work due to -0.0 >= +0.0 x = Math.max(x, d); } else { x = Math.min(x, d);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值