Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame^] | 1 | /* Float object implementation */ |
| 2 | |
| 3 | #include <stdio.h> |
| 4 | #include <math.h> |
| 5 | #include <ctype.h> |
| 6 | |
| 7 | #include "PROTO.h" |
| 8 | #include "object.h" |
| 9 | #include "floatobject.h" |
| 10 | #include "stringobject.h" |
| 11 | #include "objimpl.h" |
| 12 | |
| 13 | object * |
| 14 | newfloatobject(fval) |
| 15 | double fval; |
| 16 | { |
| 17 | /* For efficiency, this code is copied from newobject() */ |
| 18 | register floatobject *op = (floatobject *) malloc(sizeof(floatobject)); |
| 19 | if (op == NULL) { |
| 20 | errno = ENOMEM; |
| 21 | } |
| 22 | else { |
| 23 | NEWREF(op); |
| 24 | op->ob_type = &Floattype; |
| 25 | op->ob_fval = fval; |
| 26 | } |
| 27 | return (object *) op; |
| 28 | } |
| 29 | |
| 30 | double |
| 31 | getfloatvalue(op) |
| 32 | object *op; |
| 33 | { |
| 34 | if (!is_floatobject(op)) { |
| 35 | errno = EBADF; |
| 36 | return -1; |
| 37 | } |
| 38 | else |
| 39 | return ((floatobject *)op) -> ob_fval; |
| 40 | } |
| 41 | |
| 42 | /* Methods */ |
| 43 | |
| 44 | static void |
| 45 | float_buf_repr(buf, v) |
| 46 | char *buf; |
| 47 | floatobject *v; |
| 48 | { |
| 49 | register char *cp; |
| 50 | /* Subroutine for float_repr and float_print. |
| 51 | We want float numbers to be recognizable as such, |
| 52 | i.e., they should contain a decimal point or an exponent. |
| 53 | However, %g may print the number as an integer; |
| 54 | in such cases, we append ".0" to the string. */ |
| 55 | sprintf(buf, "%.12g", v->ob_fval); |
| 56 | cp = buf; |
| 57 | if (*cp == '-') |
| 58 | cp++; |
| 59 | for (; *cp != '\0'; cp++) { |
| 60 | /* Any non-digit means it's not an integer; |
| 61 | this takes care of NAN and INF as well. */ |
| 62 | if (!isdigit(*cp)) |
| 63 | break; |
| 64 | } |
| 65 | if (*cp == '\0') { |
| 66 | *cp++ = '.'; |
| 67 | *cp++ = '0'; |
| 68 | *cp++ = '\0'; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | static void |
| 73 | float_print(v, fp, flags) |
| 74 | floatobject *v; |
| 75 | FILE *fp; |
| 76 | int flags; |
| 77 | { |
| 78 | char buf[100]; |
| 79 | float_buf_repr(buf, v); |
| 80 | fputs(buf, fp); |
| 81 | } |
| 82 | |
| 83 | static object * |
| 84 | float_repr(v) |
| 85 | floatobject *v; |
| 86 | { |
| 87 | char buf[100]; |
| 88 | float_buf_repr(buf, v); |
| 89 | return newstringobject(buf); |
| 90 | } |
| 91 | |
| 92 | static int |
| 93 | float_compare(v, w) |
| 94 | floatobject *v, *w; |
| 95 | { |
| 96 | double i = v->ob_fval; |
| 97 | double j = w->ob_fval; |
| 98 | return (i < j) ? -1 : (i > j) ? 1 : 0; |
| 99 | } |
| 100 | |
| 101 | static object * |
| 102 | float_add(v, w) |
| 103 | floatobject *v; |
| 104 | object *w; |
| 105 | { |
| 106 | if (!is_floatobject(w)) { |
| 107 | errno = EINVAL; |
| 108 | return NULL; |
| 109 | } |
| 110 | return newfloatobject(v->ob_fval + ((floatobject *)w) -> ob_fval); |
| 111 | } |
| 112 | |
| 113 | static object * |
| 114 | float_sub(v, w) |
| 115 | floatobject *v; |
| 116 | object *w; |
| 117 | { |
| 118 | if (!is_floatobject(w)) { |
| 119 | errno = EINVAL; |
| 120 | return NULL; |
| 121 | } |
| 122 | return newfloatobject(v->ob_fval - ((floatobject *)w) -> ob_fval); |
| 123 | } |
| 124 | |
| 125 | static object * |
| 126 | float_mul(v, w) |
| 127 | floatobject *v; |
| 128 | object *w; |
| 129 | { |
| 130 | if (!is_floatobject(w)) { |
| 131 | errno = EINVAL; |
| 132 | return NULL; |
| 133 | } |
| 134 | return newfloatobject(v->ob_fval * ((floatobject *)w) -> ob_fval); |
| 135 | } |
| 136 | |
| 137 | static object * |
| 138 | float_div(v, w) |
| 139 | floatobject *v; |
| 140 | object *w; |
| 141 | { |
| 142 | if (!is_floatobject(w)) { |
| 143 | errno = EINVAL; |
| 144 | return NULL; |
| 145 | } |
| 146 | if (((floatobject *)w) -> ob_fval == 0) { |
| 147 | errno = EDOM; |
| 148 | return NULL; |
| 149 | } |
| 150 | return newfloatobject(v->ob_fval / ((floatobject *)w) -> ob_fval); |
| 151 | } |
| 152 | |
| 153 | static object * |
| 154 | float_rem(v, w) |
| 155 | floatobject *v; |
| 156 | object *w; |
| 157 | { |
| 158 | double wx; |
| 159 | extern double fmod(); |
| 160 | if (!is_floatobject(w)) { |
| 161 | errno = EINVAL; |
| 162 | return NULL; |
| 163 | } |
| 164 | wx = ((floatobject *)w) -> ob_fval; |
| 165 | if (wx == 0.0) { |
| 166 | errno = EDOM; |
| 167 | return NULL; |
| 168 | } |
| 169 | return newfloatobject(fmod(v->ob_fval, wx)); |
| 170 | } |
| 171 | |
| 172 | static object * |
| 173 | float_pow(v, w) |
| 174 | floatobject *v; |
| 175 | object *w; |
| 176 | { |
| 177 | double iv, iw, ix; |
| 178 | extern double pow(); |
| 179 | if (!is_floatobject(w)) { |
| 180 | errno = EINVAL; |
| 181 | return NULL; |
| 182 | } |
| 183 | iv = v->ob_fval; |
| 184 | iw = ((floatobject *)w)->ob_fval; |
| 185 | errno = 0; |
| 186 | ix = pow(iv, iw); |
| 187 | if (errno != 0) |
| 188 | return NULL; |
| 189 | else |
| 190 | return newfloatobject(ix); |
| 191 | } |
| 192 | |
| 193 | static object * |
| 194 | float_neg(v) |
| 195 | floatobject *v; |
| 196 | { |
| 197 | return newfloatobject(-v->ob_fval); |
| 198 | } |
| 199 | |
| 200 | static object * |
| 201 | float_pos(v) |
| 202 | floatobject *v; |
| 203 | { |
| 204 | return newfloatobject(v->ob_fval); |
| 205 | } |
| 206 | |
| 207 | static number_methods float_as_number = { |
| 208 | float_add, /*tp_add*/ |
| 209 | float_sub, /*tp_subtract*/ |
| 210 | float_mul, /*tp_multiply*/ |
| 211 | float_div, /*tp_divide*/ |
| 212 | float_rem, /*tp_remainder*/ |
| 213 | float_pow, /*tp_power*/ |
| 214 | float_neg, /*tp_negate*/ |
| 215 | float_pos, /*tp_plus*/ |
| 216 | }; |
| 217 | |
| 218 | typeobject Floattype = { |
| 219 | OB_HEAD_INIT(&Typetype) |
| 220 | 0, |
| 221 | "float", |
| 222 | sizeof(floatobject), |
| 223 | 0, |
| 224 | free, /*tp_dealloc*/ |
| 225 | float_print, /*tp_print*/ |
| 226 | 0, /*tp_getattr*/ |
| 227 | 0, /*tp_setattr*/ |
| 228 | float_compare, /*tp_compare*/ |
| 229 | float_repr, /*tp_repr*/ |
| 230 | &float_as_number, /*tp_as_number*/ |
| 231 | 0, /*tp_as_sequence*/ |
| 232 | 0, /*tp_as_mapping*/ |
| 233 | }; |
| 234 | |
| 235 | /* |
| 236 | XXX This is not enough. Need: |
| 237 | - automatic casts for mixed arithmetic (3.1 * 4) |
| 238 | - mixed comparisons (!) |
| 239 | - look at other uses of ints that could be extended to floats |
| 240 | */ |