Debian Codegraph @

mypyc/lib-rt/float_ops.c

raw
1 // Float primitive operations
2 //
3 // These are registered in mypyc.primitives.float_ops.
4
5 #include <Python.h>
6 #include "CPy.h"
7
8
9 static double CPy_DomainError(void) {
10 PyErr_SetString(PyExc_ValueError, "math domain error");
11 return CPY_FLOAT_ERROR;
12 }
13
14 static double CPy_MathRangeError(void) {
15 PyErr_SetString(PyExc_OverflowError, "math range error");
16 return CPY_FLOAT_ERROR;
17 }
18
19 static double CPy_MathExpectedNonNegativeInputError(double x) {
20 char *buf = PyOS_double_to_string(x, 'r', 0, Py_DTSF_ADD_DOT_0, NULL);
21 if (buf) {
22 PyErr_Format(PyExc_ValueError, "expected a nonnegative input, got %s", buf);
23 PyMem_Free(buf);
24 }
25 return CPY_FLOAT_ERROR;
26 }
27
28 static double CPy_MathExpectedPositiveInputError(double x) {
29 char *buf = PyOS_double_to_string(x, 'r', 0, Py_DTSF_ADD_DOT_0, NULL);
30 if (buf) {
31 PyErr_Format(PyExc_ValueError, "expected a positive input, got %s", buf);
32 PyMem_Free(buf);
33 }
34 return CPY_FLOAT_ERROR;
35 }
36
37 static double CPy_MathExpectedFiniteInput(double x) {
38 char *buf = PyOS_double_to_string(x, 'r', 0, Py_DTSF_ADD_DOT_0, NULL);
39 if (buf) {
40 PyErr_Format(PyExc_ValueError, "expected a finite input, got %s", buf);
41 PyMem_Free(buf);
42 }
43 return CPY_FLOAT_ERROR;
44 }
45
46 double CPyFloat_FromTagged(CPyTagged x) {
47 if (CPyTagged_CheckShort(x)) {
48 return CPyTagged_ShortAsSsize_t(x);
49 }
50 double result = PyFloat_AsDouble(CPyTagged_LongAsObject(x));
51 if (unlikely(result == -1.0) && PyErr_Occurred()) {
52 return CPY_FLOAT_ERROR;
53 }
54 return result;
55 }
56
57 double CPyFloat_Sin(double x) {
58 double v = sin(x);
59 if (unlikely(isnan(v)) && !isnan(x)) {
60 #if CPY_3_14_FEATURES
61 return CPy_MathExpectedFiniteInput(x);
62 #else
63 return CPy_DomainError();
64 #endif
65 }
66 return v;
67 }
68
69 double CPyFloat_Cos(double x) {
70 double v = cos(x);
71 if (unlikely(isnan(v)) && !isnan(x)) {
72 #if CPY_3_14_FEATURES
73 return CPy_MathExpectedFiniteInput(x);
74 #else
75 return CPy_DomainError();
76 #endif
77 }
78 return v;
79 }
80
81 double CPyFloat_Tan(double x) {
82 if (unlikely(isinf(x))) {
83 #if CPY_3_14_FEATURES
84 return CPy_MathExpectedFiniteInput(x);
85 #else
86 return CPy_DomainError();
87 #endif
88 }
89 return tan(x);
90 }
91
92 double CPyFloat_Sqrt(double x) {
93 if (x < 0.0) {
94 #if CPY_3_14_FEATURES
95 return CPy_MathExpectedNonNegativeInputError(x);
96 #else
97 return CPy_DomainError();
98 #endif
99 }
100 return sqrt(x);
101 }
102
103 double CPyFloat_Exp(double x) {
104 double v = exp(x);
105 if (unlikely(v == INFINITY) && x != INFINITY) {
106 return CPy_MathRangeError();
107 }
108 return v;
109 }
110
111 double CPyFloat_Log(double x) {
112 if (x <= 0.0) {
113 #if CPY_3_14_FEATURES
114 return CPy_MathExpectedPositiveInputError(x);
115 #else
116 return CPy_DomainError();
117 #endif
118 }
119 return log(x);
120 }
121
122 CPyTagged CPyFloat_Floor(double x) {
123 double v = floor(x);
124 return CPyTagged_FromFloat(v);
125 }
126
127 CPyTagged CPyFloat_Ceil(double x) {
128 double v = ceil(x);
129 return CPyTagged_FromFloat(v);
130 }
131
132 bool CPyFloat_IsInf(double x) {
133 return isinf(x) != 0;
134 }
135
136 bool CPyFloat_IsNaN(double x) {
137 return isnan(x) != 0;
138 }
139
140 // From CPython 3.10.0, Objects/floatobject.c
141 static void
142 _float_div_mod(double vx, double wx, double *floordiv, double *mod)
143 {
144 double div;
145 *mod = fmod(vx, wx);
146 /* fmod is typically exact, so vx-mod is *mathematically* an
147 exact multiple of wx. But this is fp arithmetic, and fp
148 vx - mod is an approximation; the result is that div may
149 not be an exact integral value after the division, although
150 it will always be very close to one.
151 */
152 div = (vx - *mod) / wx;
153 if (*mod) {
154 /* ensure the remainder has the same sign as the denominator */
155 if ((wx < 0) != (*mod < 0)) {
156 *mod += wx;
157 div -= 1.0;
158 }
159 }
160 else {
161 /* the remainder is zero, and in the presence of signed zeroes
162 fmod returns different results across platforms; ensure
163 it has the same sign as the denominator. */
164 *mod = copysign(0.0, wx);
165 }
166 /* snap quotient to nearest integral value */
167 if (div) {
168 *floordiv = floor(div);
169 if (div - *floordiv > 0.5) {
170 *floordiv += 1.0;
171 }
172 }
173 else {
174 /* div is zero - get the same sign as the true quotient */
175 *floordiv = copysign(0.0, vx / wx); /* zero w/ sign of vx/wx */
176 }
177 }
178
179 double CPyFloat_FloorDivide(double x, double y) {
180 double mod, floordiv;
181 if (y == 0) {
182 PyErr_SetString(PyExc_ZeroDivisionError, "float floor division by zero");
183 return CPY_FLOAT_ERROR;
184 }
185 _float_div_mod(x, y, &floordiv, &mod);
186 return floordiv;
187 }
188
189 // Adapted from CPython 3.10.7
190 double CPyFloat_Pow(double x, double y) {
191 if (!isfinite(x) || !isfinite(y)) {
192 if (isnan(x))
193 return y == 0.0 ? 1.0 : x; /* NaN**0 = 1 */
194 else if (isnan(y))
195 return x == 1.0 ? 1.0 : y; /* 1**NaN = 1 */
196 else if (isinf(x)) {
197 int odd_y = isfinite(y) && fmod(fabs(y), 2.0) == 1.0;
198 if (y > 0.0)
199 return odd_y ? x : fabs(x);
200 else if (y == 0.0)
201 return 1.0;
202 else /* y < 0. */
203 return odd_y ? copysign(0.0, x) : 0.0;
204 }
205 else if (isinf(y)) {
206 if (fabs(x) == 1.0)
207 return 1.0;
208 else if (y > 0.0 && fabs(x) > 1.0)
209 return y;
210 else if (y < 0.0 && fabs(x) < 1.0) {
211 #if PY_VERSION_HEX < 0x030B0000
212 if (x == 0.0) { /* 0**-inf: divide-by-zero */
213 return CPy_DomainError();
214 }
215 #endif
216 return -y; /* result is +inf */
217 } else
218 return 0.0;
219 }
220 }
221 double r = pow(x, y);
222 if (!isfinite(r)) {
223 if (isnan(r)) {
224 return CPy_DomainError();
225 }
226 /*
227 an infinite result here arises either from:
228 (A) (+/-0.)**negative (-> divide-by-zero)
229 (B) overflow of x**y with x and y finite
230 */
231 else if (isinf(r)) {
232 if (x == 0.0)
233 return CPy_DomainError();
234 else
235 return CPy_MathRangeError();
236 }
237 }
238 return r;
239 }