Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
| 2 | Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The |
| 3 | Netherlands. |
| 4 | |
| 5 | All Rights Reserved |
| 6 | |
| 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
| 9 | provided that the above copyright notice appear in all copies and that |
| 10 | both that copyright notice and this permission notice appear in |
| 11 | supporting documentation, and that the names of Stichting Mathematisch |
| 12 | Centrum or CWI not be used in advertising or publicity pertaining to |
| 13 | distribution of the software without specific, written prior permission. |
| 14 | |
| 15 | STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 16 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 17 | FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE |
| 18 | FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 19 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 20 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 21 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 22 | |
| 23 | ******************************************************************/ |
| 24 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 25 | /* Float object implementation */ |
| 26 | |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 27 | /* XXX There should be overflow checks here, but it's hard to check |
| 28 | for any kind of float exception without losing portability. */ |
| 29 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 30 | #include "allobjects.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 31 | |
Guido van Rossum | c211ee4 | 1990-12-20 23:06:26 +0000 | [diff] [blame] | 32 | #include <errno.h> |
| 33 | #ifndef errno |
| 34 | extern int errno; |
| 35 | #endif |
| 36 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 37 | #include <ctype.h> |
| 38 | #include <math.h> |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 39 | |
Guido van Rossum | 6923e13 | 1990-11-02 17:50:43 +0000 | [diff] [blame] | 40 | #ifndef THINK_C |
| 41 | extern double fmod PROTO((double, double)); |
| 42 | extern double pow PROTO((double, double)); |
| 43 | #endif |
| 44 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 45 | object * |
| 46 | newfloatobject(fval) |
| 47 | double fval; |
| 48 | { |
| 49 | /* For efficiency, this code is copied from newobject() */ |
| 50 | register floatobject *op = (floatobject *) malloc(sizeof(floatobject)); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 51 | if (op == NULL) |
| 52 | return err_nomem(); |
| 53 | NEWREF(op); |
| 54 | op->ob_type = &Floattype; |
| 55 | op->ob_fval = fval; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 56 | return (object *) op; |
| 57 | } |
| 58 | |
| 59 | double |
| 60 | getfloatvalue(op) |
| 61 | object *op; |
| 62 | { |
| 63 | if (!is_floatobject(op)) { |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 64 | err_badarg(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 65 | return -1; |
| 66 | } |
| 67 | else |
| 68 | return ((floatobject *)op) -> ob_fval; |
| 69 | } |
| 70 | |
| 71 | /* Methods */ |
| 72 | |
Guido van Rossum | 27dec7e | 1991-06-04 19:42:53 +0000 | [diff] [blame] | 73 | void |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 74 | float_buf_repr(buf, v) |
| 75 | char *buf; |
| 76 | floatobject *v; |
| 77 | { |
| 78 | register char *cp; |
| 79 | /* Subroutine for float_repr and float_print. |
| 80 | We want float numbers to be recognizable as such, |
| 81 | i.e., they should contain a decimal point or an exponent. |
| 82 | However, %g may print the number as an integer; |
| 83 | in such cases, we append ".0" to the string. */ |
| 84 | sprintf(buf, "%.12g", v->ob_fval); |
| 85 | cp = buf; |
| 86 | if (*cp == '-') |
| 87 | cp++; |
| 88 | for (; *cp != '\0'; cp++) { |
| 89 | /* Any non-digit means it's not an integer; |
| 90 | this takes care of NAN and INF as well. */ |
| 91 | if (!isdigit(*cp)) |
| 92 | break; |
| 93 | } |
| 94 | if (*cp == '\0') { |
| 95 | *cp++ = '.'; |
| 96 | *cp++ = '0'; |
| 97 | *cp++ = '\0'; |
| 98 | } |
| 99 | } |
| 100 | |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 101 | static int |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 102 | float_print(v, fp, flags) |
| 103 | floatobject *v; |
| 104 | FILE *fp; |
| 105 | int flags; |
| 106 | { |
| 107 | char buf[100]; |
| 108 | float_buf_repr(buf, v); |
| 109 | fputs(buf, fp); |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 110 | return 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | static object * |
| 114 | float_repr(v) |
| 115 | floatobject *v; |
| 116 | { |
| 117 | char buf[100]; |
| 118 | float_buf_repr(buf, v); |
| 119 | return newstringobject(buf); |
| 120 | } |
| 121 | |
| 122 | static int |
| 123 | float_compare(v, w) |
| 124 | floatobject *v, *w; |
| 125 | { |
| 126 | double i = v->ob_fval; |
| 127 | double j = w->ob_fval; |
| 128 | return (i < j) ? -1 : (i > j) ? 1 : 0; |
| 129 | } |
| 130 | |
| 131 | static object * |
| 132 | float_add(v, w) |
| 133 | floatobject *v; |
| 134 | object *w; |
| 135 | { |
| 136 | if (!is_floatobject(w)) { |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 137 | err_badarg(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 138 | return NULL; |
| 139 | } |
| 140 | return newfloatobject(v->ob_fval + ((floatobject *)w) -> ob_fval); |
| 141 | } |
| 142 | |
| 143 | static object * |
| 144 | float_sub(v, w) |
| 145 | floatobject *v; |
| 146 | object *w; |
| 147 | { |
| 148 | if (!is_floatobject(w)) { |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 149 | err_badarg(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 150 | return NULL; |
| 151 | } |
| 152 | return newfloatobject(v->ob_fval - ((floatobject *)w) -> ob_fval); |
| 153 | } |
| 154 | |
| 155 | static object * |
| 156 | float_mul(v, w) |
| 157 | floatobject *v; |
| 158 | object *w; |
| 159 | { |
| 160 | if (!is_floatobject(w)) { |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 161 | err_badarg(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 162 | return NULL; |
| 163 | } |
| 164 | return newfloatobject(v->ob_fval * ((floatobject *)w) -> ob_fval); |
| 165 | } |
| 166 | |
| 167 | static object * |
| 168 | float_div(v, w) |
| 169 | floatobject *v; |
| 170 | object *w; |
| 171 | { |
| 172 | if (!is_floatobject(w)) { |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 173 | err_badarg(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 174 | return NULL; |
| 175 | } |
| 176 | if (((floatobject *)w) -> ob_fval == 0) { |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 177 | err_setstr(ZeroDivisionError, "float division by zero"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 178 | return NULL; |
| 179 | } |
| 180 | return newfloatobject(v->ob_fval / ((floatobject *)w) -> ob_fval); |
| 181 | } |
| 182 | |
| 183 | static object * |
| 184 | float_rem(v, w) |
| 185 | floatobject *v; |
| 186 | object *w; |
| 187 | { |
| 188 | double wx; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 189 | if (!is_floatobject(w)) { |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 190 | err_badarg(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 191 | return NULL; |
| 192 | } |
| 193 | wx = ((floatobject *)w) -> ob_fval; |
| 194 | if (wx == 0.0) { |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 195 | err_setstr(ZeroDivisionError, "float division by zero"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 196 | return NULL; |
| 197 | } |
| 198 | return newfloatobject(fmod(v->ob_fval, wx)); |
| 199 | } |
| 200 | |
| 201 | static object * |
Guido van Rossum | eba1b5e | 1991-05-05 20:07:00 +0000 | [diff] [blame] | 202 | float_divmod(v, w) |
| 203 | floatobject *v; |
| 204 | object *w; |
| 205 | { |
Guido van Rossum | 15ecff4 | 1991-10-20 20:16:45 +0000 | [diff] [blame] | 206 | double vx, wx; |
| 207 | double div, mod; |
| 208 | object *t; |
Guido van Rossum | eba1b5e | 1991-05-05 20:07:00 +0000 | [diff] [blame] | 209 | if (!is_floatobject(w)) { |
| 210 | err_badarg(); |
| 211 | return NULL; |
| 212 | } |
Guido van Rossum | 15ecff4 | 1991-10-20 20:16:45 +0000 | [diff] [blame] | 213 | wx = ((floatobject *)w) -> ob_fval; |
| 214 | if (wx == 0.0) { |
| 215 | err_setstr(ZeroDivisionError, "float division by zero"); |
| 216 | return NULL; |
| 217 | } |
| 218 | vx = v->ob_fval; |
| 219 | mod = fmod(vx, wx); |
| 220 | div = (vx - mod) / wx; |
| 221 | if (wx*mod < 0) { |
| 222 | mod += wx; |
| 223 | div -= 1.0; |
| 224 | } |
| 225 | t = newtupleobject(2); |
| 226 | if (t != NULL) { |
| 227 | settupleitem(t, 0, newfloatobject(div)); |
| 228 | settupleitem(t, 1, newfloatobject(mod)); |
| 229 | if (err_occurred()) { |
| 230 | DECREF(t); |
| 231 | t = NULL; |
| 232 | } |
| 233 | } |
| 234 | return t; |
Guido van Rossum | eba1b5e | 1991-05-05 20:07:00 +0000 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | static object * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 238 | float_pow(v, w) |
| 239 | floatobject *v; |
| 240 | object *w; |
| 241 | { |
| 242 | double iv, iw, ix; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 243 | if (!is_floatobject(w)) { |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 244 | err_badarg(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 245 | return NULL; |
| 246 | } |
| 247 | iv = v->ob_fval; |
| 248 | iw = ((floatobject *)w)->ob_fval; |
Guido van Rossum | 70d9346 | 1991-05-28 21:57:39 +0000 | [diff] [blame] | 249 | /* Sort out special cases here instead of relying on pow() */ |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 250 | if (iw == 0.0) |
Guido van Rossum | eba1b5e | 1991-05-05 20:07:00 +0000 | [diff] [blame] | 251 | return newfloatobject(1.0); /* x**0 is 1, even 0**0 */ |
Guido van Rossum | 70d9346 | 1991-05-28 21:57:39 +0000 | [diff] [blame] | 252 | if (iv == 0.0) { |
| 253 | if (iw < 0.0) { |
| 254 | err_setstr(RuntimeError, "0.0 to the negative power"); |
| 255 | return NULL; |
| 256 | } |
| 257 | return newfloatobject(0.0); |
| 258 | } |
| 259 | if (iv < 0.0) { |
| 260 | err_setstr(RuntimeError, "negative float to float power"); |
| 261 | return NULL; |
| 262 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 263 | errno = 0; |
| 264 | ix = pow(iv, iw); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 265 | if (errno != 0) { |
| 266 | /* XXX could it be another type of error? */ |
| 267 | err_errno(OverflowError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 268 | return NULL; |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 269 | } |
| 270 | return newfloatobject(ix); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | static object * |
| 274 | float_neg(v) |
| 275 | floatobject *v; |
| 276 | { |
| 277 | return newfloatobject(-v->ob_fval); |
| 278 | } |
| 279 | |
| 280 | static object * |
| 281 | float_pos(v) |
| 282 | floatobject *v; |
| 283 | { |
Guido van Rossum | eba1b5e | 1991-05-05 20:07:00 +0000 | [diff] [blame] | 284 | INCREF(v); |
| 285 | return (object *)v; |
| 286 | } |
| 287 | |
| 288 | static object * |
| 289 | float_abs(v) |
| 290 | floatobject *v; |
| 291 | { |
| 292 | if (v->ob_fval < 0) |
| 293 | return float_neg(v); |
| 294 | else |
| 295 | return float_pos(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Guido van Rossum | 50b4ef6 | 1991-05-14 11:57:01 +0000 | [diff] [blame] | 298 | static int |
| 299 | float_nonzero(v) |
| 300 | floatobject *v; |
| 301 | { |
| 302 | return v->ob_fval != 0.0; |
| 303 | } |
| 304 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 305 | static number_methods float_as_number = { |
Guido van Rossum | eba1b5e | 1991-05-05 20:07:00 +0000 | [diff] [blame] | 306 | float_add, /*nb_add*/ |
| 307 | float_sub, /*nb_subtract*/ |
| 308 | float_mul, /*nb_multiply*/ |
| 309 | float_div, /*nb_divide*/ |
| 310 | float_rem, /*nb_remainder*/ |
| 311 | float_divmod, /*nb_divmod*/ |
| 312 | float_pow, /*nb_power*/ |
| 313 | float_neg, /*nb_negative*/ |
| 314 | float_pos, /*nb_positive*/ |
| 315 | float_abs, /*nb_absolute*/ |
Guido van Rossum | 50b4ef6 | 1991-05-14 11:57:01 +0000 | [diff] [blame] | 316 | float_nonzero, /*nb_nonzero*/ |
Guido van Rossum | 27acb33 | 1991-10-24 14:55:28 +0000 | [diff] [blame] | 317 | 0, /*nb_invert*/ |
| 318 | 0, /*nb_lshift*/ |
| 319 | 0, /*nb_rshift*/ |
| 320 | 0, /*nb_and*/ |
| 321 | 0, /*nb_xor*/ |
| 322 | 0, /*nb_or*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 323 | }; |
| 324 | |
| 325 | typeobject Floattype = { |
| 326 | OB_HEAD_INIT(&Typetype) |
| 327 | 0, |
| 328 | "float", |
| 329 | sizeof(floatobject), |
| 330 | 0, |
| 331 | free, /*tp_dealloc*/ |
| 332 | float_print, /*tp_print*/ |
| 333 | 0, /*tp_getattr*/ |
| 334 | 0, /*tp_setattr*/ |
| 335 | float_compare, /*tp_compare*/ |
| 336 | float_repr, /*tp_repr*/ |
| 337 | &float_as_number, /*tp_as_number*/ |
| 338 | 0, /*tp_as_sequence*/ |
| 339 | 0, /*tp_as_mapping*/ |
| 340 | }; |
| 341 | |
| 342 | /* |
| 343 | XXX This is not enough. Need: |
| 344 | - automatic casts for mixed arithmetic (3.1 * 4) |
| 345 | - mixed comparisons (!) |
| 346 | - look at other uses of ints that could be extended to floats |
| 347 | */ |