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 | |
| 73 | static void |
| 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 | |
| 101 | static void |
| 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); |
| 110 | } |
| 111 | |
| 112 | static object * |
| 113 | float_repr(v) |
| 114 | floatobject *v; |
| 115 | { |
| 116 | char buf[100]; |
| 117 | float_buf_repr(buf, v); |
| 118 | return newstringobject(buf); |
| 119 | } |
| 120 | |
| 121 | static int |
| 122 | float_compare(v, w) |
| 123 | floatobject *v, *w; |
| 124 | { |
| 125 | double i = v->ob_fval; |
| 126 | double j = w->ob_fval; |
| 127 | return (i < j) ? -1 : (i > j) ? 1 : 0; |
| 128 | } |
| 129 | |
| 130 | static object * |
| 131 | float_add(v, w) |
| 132 | floatobject *v; |
| 133 | object *w; |
| 134 | { |
| 135 | if (!is_floatobject(w)) { |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 136 | err_badarg(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 137 | return NULL; |
| 138 | } |
| 139 | return newfloatobject(v->ob_fval + ((floatobject *)w) -> ob_fval); |
| 140 | } |
| 141 | |
| 142 | static object * |
| 143 | float_sub(v, w) |
| 144 | floatobject *v; |
| 145 | object *w; |
| 146 | { |
| 147 | if (!is_floatobject(w)) { |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 148 | err_badarg(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 149 | return NULL; |
| 150 | } |
| 151 | return newfloatobject(v->ob_fval - ((floatobject *)w) -> ob_fval); |
| 152 | } |
| 153 | |
| 154 | static object * |
| 155 | float_mul(v, w) |
| 156 | floatobject *v; |
| 157 | object *w; |
| 158 | { |
| 159 | if (!is_floatobject(w)) { |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 160 | err_badarg(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 161 | return NULL; |
| 162 | } |
| 163 | return newfloatobject(v->ob_fval * ((floatobject *)w) -> ob_fval); |
| 164 | } |
| 165 | |
| 166 | static object * |
| 167 | float_div(v, w) |
| 168 | floatobject *v; |
| 169 | object *w; |
| 170 | { |
| 171 | if (!is_floatobject(w)) { |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 172 | err_badarg(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 173 | return NULL; |
| 174 | } |
| 175 | if (((floatobject *)w) -> ob_fval == 0) { |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 176 | err_setstr(ZeroDivisionError, "float division by zero"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 177 | return NULL; |
| 178 | } |
| 179 | return newfloatobject(v->ob_fval / ((floatobject *)w) -> ob_fval); |
| 180 | } |
| 181 | |
| 182 | static object * |
| 183 | float_rem(v, w) |
| 184 | floatobject *v; |
| 185 | object *w; |
| 186 | { |
| 187 | double wx; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 188 | if (!is_floatobject(w)) { |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 189 | err_badarg(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 190 | return NULL; |
| 191 | } |
| 192 | wx = ((floatobject *)w) -> ob_fval; |
| 193 | if (wx == 0.0) { |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 194 | err_setstr(ZeroDivisionError, "float division by zero"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 195 | return NULL; |
| 196 | } |
| 197 | return newfloatobject(fmod(v->ob_fval, wx)); |
| 198 | } |
| 199 | |
| 200 | static object * |
Guido van Rossum | eba1b5e | 1991-05-05 20:07:00 +0000 | [diff] [blame] | 201 | float_divmod(v, w) |
| 202 | floatobject *v; |
| 203 | object *w; |
| 204 | { |
| 205 | double wx; |
| 206 | if (!is_floatobject(w)) { |
| 207 | err_badarg(); |
| 208 | return NULL; |
| 209 | } |
| 210 | err_setstr(RuntimeError, "divmod() on float not implemented"); |
| 211 | return NULL; |
| 212 | } |
| 213 | |
| 214 | static object * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 215 | float_pow(v, w) |
| 216 | floatobject *v; |
| 217 | object *w; |
| 218 | { |
| 219 | double iv, iw, ix; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 220 | if (!is_floatobject(w)) { |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 221 | err_badarg(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 222 | return NULL; |
| 223 | } |
| 224 | iv = v->ob_fval; |
| 225 | iw = ((floatobject *)w)->ob_fval; |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 226 | if (iw == 0.0) |
Guido van Rossum | eba1b5e | 1991-05-05 20:07:00 +0000 | [diff] [blame] | 227 | return newfloatobject(1.0); /* x**0 is 1, even 0**0 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 228 | errno = 0; |
| 229 | ix = pow(iv, iw); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 230 | if (errno != 0) { |
| 231 | /* XXX could it be another type of error? */ |
| 232 | err_errno(OverflowError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 233 | return NULL; |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 234 | } |
| 235 | return newfloatobject(ix); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | static object * |
| 239 | float_neg(v) |
| 240 | floatobject *v; |
| 241 | { |
| 242 | return newfloatobject(-v->ob_fval); |
| 243 | } |
| 244 | |
| 245 | static object * |
| 246 | float_pos(v) |
| 247 | floatobject *v; |
| 248 | { |
Guido van Rossum | eba1b5e | 1991-05-05 20:07:00 +0000 | [diff] [blame] | 249 | INCREF(v); |
| 250 | return (object *)v; |
| 251 | } |
| 252 | |
| 253 | static object * |
| 254 | float_abs(v) |
| 255 | floatobject *v; |
| 256 | { |
| 257 | if (v->ob_fval < 0) |
| 258 | return float_neg(v); |
| 259 | else |
| 260 | return float_pos(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | static number_methods float_as_number = { |
Guido van Rossum | eba1b5e | 1991-05-05 20:07:00 +0000 | [diff] [blame] | 264 | float_add, /*nb_add*/ |
| 265 | float_sub, /*nb_subtract*/ |
| 266 | float_mul, /*nb_multiply*/ |
| 267 | float_div, /*nb_divide*/ |
| 268 | float_rem, /*nb_remainder*/ |
| 269 | float_divmod, /*nb_divmod*/ |
| 270 | float_pow, /*nb_power*/ |
| 271 | float_neg, /*nb_negative*/ |
| 272 | float_pos, /*nb_positive*/ |
| 273 | float_abs, /*nb_absolute*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 274 | }; |
| 275 | |
| 276 | typeobject Floattype = { |
| 277 | OB_HEAD_INIT(&Typetype) |
| 278 | 0, |
| 279 | "float", |
| 280 | sizeof(floatobject), |
| 281 | 0, |
| 282 | free, /*tp_dealloc*/ |
| 283 | float_print, /*tp_print*/ |
| 284 | 0, /*tp_getattr*/ |
| 285 | 0, /*tp_setattr*/ |
| 286 | float_compare, /*tp_compare*/ |
| 287 | float_repr, /*tp_repr*/ |
| 288 | &float_as_number, /*tp_as_number*/ |
| 289 | 0, /*tp_as_sequence*/ |
| 290 | 0, /*tp_as_mapping*/ |
| 291 | }; |
| 292 | |
| 293 | /* |
| 294 | XXX This is not enough. Need: |
| 295 | - automatic casts for mixed arithmetic (3.1 * 4) |
| 296 | - mixed comparisons (!) |
| 297 | - look at other uses of ints that could be extended to floats |
| 298 | */ |