001: // Copyright (c) Corporation for National Research Initiatives
002: package org.python.modules;
003:
004: import org.python.core.*;
005: import java.lang.Math;
006:
007: public class math implements ClassDictInit {
008: public static PyFloat pi = new PyFloat(Math.PI);
009: public static PyFloat e = new PyFloat(Math.E);
010:
011: public static void classDictInit(PyObject dict) {
012: }
013:
014: private static double check(double v) {
015: if (Double.isNaN(v))
016: throw Py.ValueError("math domain error");
017: if (Double.isInfinite(v))
018: throw Py.OverflowError("math range error");
019: return v;
020: }
021:
022: public static double acos(double v) {
023: return check(Math.acos(v));
024: }
025:
026: public static double asin(double v) {
027: return check(Math.asin(v));
028: }
029:
030: public static double atan(double v) {
031: return check(Math.atan(v));
032: }
033:
034: public static double atan2(double v, double w) {
035: return check(Math.atan2(v, w));
036: }
037:
038: public static double ceil(double v) {
039: return check(Math.ceil(v));
040: }
041:
042: public static double cos(double v) {
043: return check(Math.cos(v));
044: }
045:
046: public static double exp(double v) {
047: return check(Math.exp(v));
048: }
049:
050: public static double floor(PyObject v) {
051: return floor(v.__float__().getValue());
052: }
053:
054: public static double floor(double v) {
055: return check(Math.floor(v));
056: }
057:
058: public static double log(PyObject v) {
059: if (v instanceof PyLong) {
060: int e[] = new int[1];
061: double x = ((PyLong) v).scaledDoubleValue(e);
062: if (x <= 0.0)
063: throw Py.ValueError("math domain error");
064: return log(x) + (e[0] * 8.0) * log(2.0);
065: }
066: return log(v.__float__().getValue());
067: }
068:
069: private static double log(double v) {
070: return check(Math.log(v));
071: }
072:
073: public static double pow(double v, double w) {
074: return check(Math.pow(v, w));
075: }
076:
077: public static double sin(PyObject v) {
078: return sin(v.__float__().getValue());
079: }
080:
081: public static double sin(double v) {
082: return check(Math.sin(v));
083: }
084:
085: public static double sqrt(PyObject v) {
086: return sqrt(v.__float__().getValue());
087: }
088:
089: public static double sqrt(double v) {
090: return check(Math.sqrt(v));
091: }
092:
093: public static double tan(double v) {
094: return check(Math.tan(v));
095: }
096:
097: public static double log10(PyObject v) {
098: if (v instanceof PyLong) {
099: int e[] = new int[1];
100: double x = ((PyLong) v).scaledDoubleValue(e);
101: if (x <= 0.0)
102: throw Py.ValueError("math domain error");
103: return log10(x) + (e[0] * 8.0) * log10(2.0);
104: }
105: return log10(v.__float__().getValue());
106: }
107:
108: private static double log10(double v) {
109: return check(ExtraMath.log10(v));
110: }
111:
112: public static double sinh(double v) {
113: return check(0.5 * (Math.exp(v) - Math.exp(-v)));
114: }
115:
116: public static double cosh(double v) {
117: return check(0.5 * (Math.exp(v) + Math.exp(-v)));
118: }
119:
120: public static double tanh(double v) {
121: return check(sinh(v) / cosh(v));
122: }
123:
124: public static double fabs(double v) {
125: return Math.abs(v);
126: }
127:
128: public static double fmod(double v, double w) {
129: return v % w;
130: }
131:
132: public static PyTuple modf(double v) {
133: double w = v % 1.0;
134: v -= w;
135: return new PyTuple(new PyObject[] { new PyFloat(w),
136: new PyFloat(v) });
137: }
138:
139: public static PyTuple frexp(double v) {
140: int i = 0;
141: if (v != 0.0) {
142: int sign = 1;
143: if (v < 0) {
144: sign = -1;
145: v = -v;
146: }
147: // slow...
148: while (v < 0.5) {
149: v = v * 2.0;
150: i = i - 1;
151: }
152: while (v >= 1.0) {
153: v = v * 0.5;
154: i = i + 1;
155: }
156: v = v * sign;
157: }
158: return new PyTuple(new PyObject[] { new PyFloat(v),
159: new PyInteger(i) });
160: }
161:
162: public static double ldexp(double v, int w) {
163: return check(v * Math.pow(2.0, w));
164: }
165:
166: public static double hypot(double v, double w) {
167: return check(ExtraMath.hypot(v, w));
168: }
169: }
|