/*
* $Id: MathModule.java,v 1.15 2002/09/16 08:05:03 jkl Exp $
*
* Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
*
* Use is subject to license terms, as defined in
* Anvil Sofware License, Version 1.1. See LICENSE
* file, or http://njet.org/license-1.1.txt
*/
package anvil.core.math;
import anvil.core.Any;
import anvil.core.AnyDouble;
import java.util.Random;
///
/// @module anvil.math
/// Standard set of mathematical operations.
///
public class MathModule
{
private static Random _random = new Random();
/// @const MAX_INT
/// Maximum value that int can hold (2^63).
public static final Any MAX_INT = Any.create(Long.MAX_VALUE);
/// @const MIN_INT
/// Minimum value that int can hold (-2^63).
public static final Any MIN_INT = Any.create(Long.MIN_VALUE);
/// @const PI
/// The float value that is closer than any other to pi,
/// the ratio of the circumference of a circle to its diameter.
public static final Any PI = new AnyDouble(Math.PI);
/// @const E
/// The float value that is closer than any other to e,
/// the base of the natural logarithms.
public static final Any E = new AnyDouble(Math.E);
/// @const NaN
/// Not a number, returned by float operations.
public static final Any NaN = new AnyDouble(Double.NaN);
/// @const POS_INF
/// The float value of positive infinity.
public static final Any POS_INF = new AnyDouble(Double.POSITIVE_INFINITY);
/// @const NEG_INF
/// The float value of negative infinity.
public static final Any NEG_INF = new AnyDouble(Double.NEGATIVE_INFINITY);
/// @const MAX_FLOAT
/// The largest positive finite value the float can contain.
public static final Any MAX_FLOAT = new AnyDouble(Double.MAX_VALUE);
/// @const MIN_FLOAT
/// The smallest negative finite value the float can contain.
public static final Any MIN_FLOAT = new AnyDouble(Double.MIN_VALUE);
/// @function floor
/// Returns the largest (closest to positive infinity) float value
/// that is not greater than the argument and is equal to
/// a mathematical integer.
/// @synopsis float floor(float num)
public static final Object[] p_floor = { "num" };
public static final Any floor(Any value)
{
switch(value.typeOf()) {
case Any.IS_INT:
return value;
case Any.IS_DOUBLE:
return Any.create(Math.floor(value.toDouble()));
default:
return value.toAnyInt();
}
}
/// @function ceil
/// Returns the smallest (closest to negative infinity) float
/// value that is not less than the argument and is equal to a
/// mathematical integer.
/// @synopsis float ceil(float num)
public static final Object[] p_ceil = { "num" };
public static final Any ceil(Any value)
{
switch(value.typeOf()) {
case Any.IS_INT:
return value;
case Any.IS_DOUBLE:
return Any.create(Math.ceil(value.toDouble()));
default:
return value.toAnyInt();
}
}
/// @function round
/// Returns the closest int to the argument.
/// @synopsis int round(float num)
public static final Object[] p_round = { "num" };
public static final Any round(Any value)
{
switch(value.typeOf()) {
case Any.IS_INT:
return value;
case Any.IS_DOUBLE:
return Any.create(Math.round(value.toDouble()));
default:
return value.toAnyInt();
}
}
/// @function abs
/// Returns the absolute value of given number.
/// @synopsis int abs(int num)
/// @synopsis float abs(float num)
public static final Object[] p_abs = { "num" };
public static final Any abs(Any value)
{
switch(value.typeOf()) {
case Any.IS_INT:
return Any.create(Math.abs(value.toLong()));
case Any.IS_DOUBLE:
return Any.create(Math.abs(value.toDouble()));
default:
return Any.create(Math.abs(value.toLong()));
}
}
/// @function random
/// Returns pseudorandom integer between given range.
/// @synopsis int random(int lobound, int hibound)
/// @param lobound Low bound, inclusive
/// @param hibound High bound, inclusive
/// @return Pseudorandom number
public static final Object[] p_random = { "lo", "hi" };
public static final Any random(int lo, int hi)
{
int delta;
if (lo > hi) {
delta = lo - hi + 1;
lo = hi;
} else {
delta = hi - lo + 1;
}
if (delta<0) {
delta = -delta;
}
int i = _random.nextInt() % delta;
if (i<0) {
i = -i;
}
return Any.create(lo + i);
}
/// @function randomGaussian
/// Returns pseudorandom, Gaussian ("normally") distributed float
/// value with mean 0.0 and standard deviation 1.0.
/// @synopsis float randomGaussian()
public static final Any randomGaussian()
{
return Any.create(_random.nextGaussian());
}
/// @function randomFloat
/// Returns the next pseudorandom, uniformly distributed float value between
/// 0.0 and 1.0
/// @synopsis float randomFloat()
public static final Any randomFloat()
{
return Any.create(_random.nextFloat());
}
/// @function randomize
/// Sets the seed of random number generator.
/// @synopsis void randomize(int seed)
public static final Object[] p_randomize = { "seed" };
public static final Any randomize(int seed)
{
_random.setSeed(seed);
return Any.NULL;
}
/// @function sin
/// Returns the trigonometric sine of an angle.
/// @synopsis float sin(float angle)
/// @param angle an angle, in radians
public static final Object[] p_sin = { "num" };
public static final Any sin(double value)
{
return Any.create(Math.sin(value));
}
/// @function cos
/// Returns the trigonometric cosine of an angle.
/// @synopsis float cos(float angle)
/// @param angle an angle, in radians
public static final Object[] p_cos = { "num" };
public static final Any cos(double value)
{
return Any.create(Math.cos(value));
}
/// @function tan
/// Returns the trigonometric tangent of an angle.
/// @synopsis float tan(float angle)
/// @param angle an angle, in radians
public static final Object[] p_tan = { "num" };
public static final Any tan(double value)
{
return Any.create(Math.tan(value));
}
/// @function asin
/// Returns the arc sine of an angle, in the range of -pi/2 through pi/2.
/// @synopsis float asin(float a)
public static final Object[] p_asin = { "num" };
public static final Any asin(double value)
{
return Any.create(Math.asin(value));
}
/// @function acos
/// Returns the arc cosine of an angle, in the range of -pi/2 through pi/2.
/// @synopsis float acos(float a)
public static final Object[] p_acos = { "num" };
public static final Any acos(double value)
{
return Any.create(Math.acos(value));
}
/// @function atan
/// Returns the arc tangent of an angle, in the range of -pi/2 through pi/2.
/// @synopsis float atan(float a)
public static final Object[] p_atan = { "num" };
public static final Any atan(double value)
{
return Any.create(Math.atan(value));
}
/// @function atan2
/// Converts rectangular coordinates (b, a) to polar (r, theta). This method computes
/// the phase theta by computing an arc tangent of a/b in the range of -pi to pi.
/// @synopsis float atan2(float a, float b)
public static final Object[] p_atan2 = { "a", "b" };
public static final Any atan2(double a, double b)
{
return Any.create(Math.atan2(a, b));
}
/// @function exp
/// Returns the exponential number e (i.e., 2.718...) raised to the
/// power of a given value.
/// @synopsis float exp(float num)
public static final Object[] p_exp = { "num" };
public static final Any exp(double value)
{
return Any.create(Math.exp(value));
}
/// @function log
/// Returns the natural logarithm (base e) of a float value.
/// @synopsis float log(float num)
public static final Object[] p_log = { "num" };
public static final Any log(double value)
{
return Any.create(Math.log(value));
}
/// @function pow
/// Returns of value of the first argument raised to the power of
/// the second argument.
/// @synopsis float pow(float num, float power)
public static final Object[] p_pow = { "num", "power" };
public static final Any pow(double value, double power)
{
return Any.create(Math.pow(value, power));
}
/// @function sqrt
/// Returns the square root of a given value.
/// @synopsis float sqrt(float num)
public static final Object[] p_sqrt = { "num" };
public static final Any sqrt(double value)
{
return Any.create(Math.sqrt(value));
}
/// @function toDegrees
/// Converts an angle measured in radians to the equivalent angle
/// measured in degrees.
/// @synopsis float toDegrees(float angle)
/// @param angle an angle, in radians
public static final Object[] p_toDegrees = { "num" };
public static final Any toDegrees(double value)
{
return Any.create(Math.toDegrees(value));
}
/// @function toRadians
/// Converts an angle measured in degrees to the equivalent angle
/// measured in radians.
/// @synopsis float toRadians(float angle)
/// @param angle an angle, in degrees
public static final Object[] p_toRadians = { "num" };
public static final Any toRadians(double value)
{
return Any.create(Math.toRadians(value));
}
/// @function min
/// Returns minimum value from parameters.
/// @synopsis object min(object parameters, ...)
/// @return Minimum value
public static final Object[] p_min = { new Integer(2), "first", "second", "rest" };
public static final Any min(Any first, Any second, Any[] rest)
{
Any min;
if (first.compareTo(second) <= 0) {
min = first;
} else {
min = second;
}
if (rest != null) {
int n = rest.length;
for(int i=0; i<n; i++) {
if (rest[i].compareTo(min) < 0) {
min = rest[i];
}
}
}
return min;
}
/// @function max
/// Returns maximum value from parameters.
/// @synopsis object max(object parameters, ...)
/// @return Maximum value
public static final Object[] p_max = { new Integer(2), "first", "second", "rest" };
public static final Any max(Any first, Any second, Any[] rest)
{
Any max;
if (first.compareTo(second) > 0) {
max = first;
} else {
max = second;
}
if (rest != null) {
int n = rest.length;
for(int i=0; i<n; i++) {
if (rest[i].compareTo(max) > 0) {
max = rest[i];
}
}
}
return max;
}
/// @function sign
/// Returns an integer indicating the sign of parameter.
/// -1 for negative, 1 for positive and 0 for zero.
/// @synopsis int sing(int number)
/// @synopsis int sing(float number)
public static final Any sign(Any value)
{
double d = value.toDouble();
if (d == 0.0) {
return Any.ZERO;
} if (d > 0.0) {
return Any.ONE;
} else {
return Any.MINUS_ONE;
}
}
public static final anvil.script.compiler.NativeNamespace __module__ =
new anvil.script.compiler.NativeNamespace(
"math",
MathModule.class,
new Class[] { },
//DOC{{
""+
"\n" +
" @module anvil.math\n" +
" Standard set of mathematical operations.\n" +
"\n" +
" @const MAX_INT\n" +
" Maximum value that int can hold (2^63).\n" +
" @const MIN_INT\n" +
" Minimum value that int can hold (-2^63).\n" +
" @const PI\n" +
" The float value that is closer than any other to pi, \n" +
" the ratio of the circumference of a circle to its diameter.\n" +
" @const E\n" +
" The float value that is closer than any other to e, \n" +
" the base of the natural logarithms.\n" +
" @const NaN\n" +
" Not a number, returned by float operations.\n" +
" @const POS_INF\n" +
" The float value of positive infinity.\n" +
" @const NEG_INF\n" +
" The float value of negative infinity.\n" +
" @const MAX_FLOAT\n" +
" The largest positive finite value the float can contain.\n" +
" @const MIN_FLOAT\n" +
" The smallest negative finite value the float can contain.\n" +
" @function floor\n" +
" Returns the largest (closest to positive infinity) float value\n" +
" that is not greater than the argument and is equal to\n" +
" a mathematical integer.\n" +
" @synopsis float floor(float num)\n" +
" @function ceil\n" +
" Returns the smallest (closest to negative infinity) float \n" +
" value that is not less than the argument and is equal to a \n" +
" mathematical integer.\n" +
" @synopsis float ceil(float num)\n" +
" @function round\n" +
" Returns the closest int to the argument. \n" +
" @synopsis int round(float num) \n" +
" @function abs\n" +
" Returns the absolute value of given number.\n" +
" @synopsis int abs(int num)\n" +
" @synopsis float abs(float num)\n" +
" @function random\n" +
" Returns pseudorandom integer between given range.\n" +
" @synopsis int random(int lobound, int hibound)\n" +
" @param lobound Low bound, inclusive\n" +
" @param hibound High bound, inclusive\n" +
" @return Pseudorandom number\n" +
" @function randomGaussian\n" +
" Returns pseudorandom, Gaussian (\"normally\") distributed float\n" +
" value with mean 0.0 and standard deviation 1.0.\n" +
" @synopsis float randomGaussian()\n" +
" @function randomFloat\n" +
" Returns the next pseudorandom, uniformly distributed float value between\n" +
" 0.0 and 1.0 \n" +
" @synopsis float randomFloat()\n" +
" @function randomize\n" +
" Sets the seed of random number generator.\n" +
" @synopsis void randomize(int seed)\n" +
" @function sin\n" +
" Returns the trigonometric sine of an angle.\n" +
" @synopsis float sin(float angle)\n" +
" @param angle an angle, in radians\n" +
" @function cos\n" +
" Returns the trigonometric cosine of an angle.\n" +
" @synopsis float cos(float angle)\n" +
" @param angle an angle, in radians\n" +
" @function tan\n" +
" Returns the trigonometric tangent of an angle.\n" +
" @synopsis float tan(float angle)\n" +
" @param angle an angle, in radians\n" +
" @function asin\n" +
" Returns the arc sine of an angle, in the range of -pi/2 through pi/2.\n" +
" @synopsis float asin(float a)\n" +
" @function acos\n" +
" Returns the arc cosine of an angle, in the range of -pi/2 through pi/2.\n" +
" @synopsis float acos(float a)\n" +
" @function atan\n" +
" Returns the arc tangent of an angle, in the range of -pi/2 through pi/2.\n" +
" @synopsis float atan(float a)\n" +
" @function atan2\n" +
" Converts rectangular coordinates (b, a) to polar (r, theta). This method computes \n" +
" the phase theta by computing an arc tangent of a/b in the range of -pi to pi.\n" +
" @synopsis float atan2(float a, float b)\n" +
" @function exp\n" +
" Returns the exponential number e (i.e., 2.718...) raised to the \n" +
" power of a given value.\n" +
" @synopsis float exp(float num)\n" +
" @function log \n" +
" Returns the natural logarithm (base e) of a float value.\n" +
" @synopsis float log(float num)\n" +
" @function pow\n" +
" Returns of value of the first argument raised to the power of \n" +
" the second argument.\n" +
" @synopsis float pow(float num, float power)\n" +
" @function sqrt\n" +
" Returns the square root of a given value.\n" +
" @synopsis float sqrt(float num)\n" +
" @function toDegrees\n" +
" Converts an angle measured in radians to the equivalent angle \n" +
" measured in degrees.\n" +
" @synopsis float toDegrees(float angle)\n" +
" @param angle an angle, in radians\n" +
" @function toRadians\n" +
" Converts an angle measured in degrees to the equivalent angle \n" +
" measured in radians.\n" +
" @synopsis float toRadians(float angle)\n" +
" @param angle an angle, in degrees\n" +
" @function min\n" +
" Returns minimum value from parameters.\n" +
" @synopsis object min(object parameters, ...)\n" +
" @return Minimum value\n" +
" @function max\n" +
" Returns maximum value from parameters.\n" +
" @synopsis object max(object parameters, ...)\n" +
" @return Maximum value\n" +
" @function sign\n" +
" Returns an integer indicating the sign of parameter.\n" +
" -1 for negative, 1 for positive and 0 for zero.\n" +
" @synopsis int sing(int number)\n" +
" @synopsis int sing(float number)\n"
//}}DOC
);
}