1 package org.python.modules; 3 4 import org.python.core.*; 5 import java.lang.Math ; 6 7 public class math implements ClassDictInit { 8 public static PyFloat pi = new PyFloat(Math.PI); 9 public static PyFloat e = new PyFloat(Math.E); 10 11 public static void classDictInit(PyObject dict) { 12 } 13 14 private static double check(double v) { 15 if (Double.isNaN(v)) 16 throw Py.ValueError("math domain error"); 17 if (Double.isInfinite(v)) 18 throw Py.OverflowError("math range error"); 19 return v; 20 } 21 22 public static double acos(double v) { 23 return check(Math.acos(v)); 24 } 25 26 public static double asin(double v) { 27 return check(Math.asin(v)); 28 } 29 30 public static double atan(double v) { 31 return check(Math.atan(v)); 32 } 33 34 public static double atan2(double v, double w) { 35 return check(Math.atan2(v, w)); 36 } 37 38 public static double ceil(double v) { 39 return check(Math.ceil(v)); 40 } 41 42 public static double cos(double v) { 43 return check(Math.cos(v)); 44 } 45 46 public static double exp(double v) { 47 return check(Math.exp(v)); 48 } 49 50 public static double floor(PyObject v) { 51 return floor(v.__float__().getValue()); 52 } 53 54 public static double floor(double v) { 55 return check(Math.floor(v)); 56 } 57 58 public static double log(PyObject v) { 59 if (v instanceof PyLong) { 60 int e[] = new int[1]; 61 double x = ((PyLong)v).scaledDoubleValue(e); 62 if (x <= 0.0) throw Py.ValueError("math domain error"); 63 return log(x) + (e[0]*8.0)*log(2.0); 64 } 65 return log(v.__float__().getValue()); 66 } 67 68 private static double log(double v) { 69 return check(Math.log(v)); 70 } 71 72 public static double pow(double v, double w) { 73 return check(Math.pow(v, w)); 74 } 75 76 public static double sin(PyObject v) { 77 return sin(v.__float__().getValue()); 78 } 79 80 public static double sin(double v) { 81 return check(Math.sin(v)); 82 } 83 84 public static double sqrt(PyObject v) { 85 return sqrt(v.__float__().getValue()); 86 } 87 88 public static double sqrt(double v) { 89 return check(Math.sqrt(v)); 90 } 91 92 public static double tan(double v) { 93 return check(Math.tan(v)); 94 } 95 96 public static double log10(PyObject v) { 97 if (v instanceof PyLong) { 98 int e[] = new int[1]; 99 double x = ((PyLong)v).scaledDoubleValue(e); 100 if (x <= 0.0) throw Py.ValueError("math domain error"); 101 return log10(x) + (e[0]*8.0)*log10(2.0); 102 } 103 return log10(v.__float__().getValue()); 104 } 105 106 private static double log10(double v) { 107 return check(ExtraMath.log10(v)); 108 } 109 110 public static double sinh(double v) { 111 return check(0.5 * (Math.exp(v) - Math.exp(-v))); 112 } 113 114 public static double cosh(double v) { 115 return check(0.5 * (Math.exp(v) + Math.exp(-v))); 116 } 117 118 public static double tanh(double v) { 119 return check(sinh(v) / cosh(v)); 120 } 121 122 public static double fabs(double v) { 123 return Math.abs(v); 124 } 125 126 public static double fmod(double v, double w) { 127 return v % w; 128 } 129 130 public static PyTuple modf(double v) { 131 double w = v % 1.0; 132 v -= w; 133 return new PyTuple(new PyObject[] {new PyFloat(w), new PyFloat(v)}); 134 } 135 136 public static PyTuple frexp(double v) { 137 int i = 0; 138 if (v != 0.0) { 139 int sign = 1; 140 if (v < 0) { 141 sign = -1; 142 v = -v; 143 } 144 while (v < 0.5) { 146 v = v*2.0; 147 i = i-1; 148 } 149 while (v >= 1.0) { 150 v = v*0.5; 151 i = i+1; 152 } 153 v = v*sign; 154 } 155 return new PyTuple(new PyObject[] {new PyFloat(v), new PyInteger(i)}); 156 } 157 158 public static double ldexp(double v, int w) { 159 return check(v * Math.pow(2.0, w)); 160 } 161 162 public static double hypot(double v, double w) { 163 return check(ExtraMath.hypot(v, w)); 164 } 165 } 166 | Popular Tags |