Change soft-fail to use the config, rather than env
[rbx.git] / shotgun / lib / float.c
blob3018270bc65837b95668264a67317ead5be8c7bd
1 #include <string.h>
2 #include <math.h>
4 /* Begin borrowing from MRI 1.8.6 stable */
5 #if defined(__FreeBSD__) && __FreeBSD__ < 4
6 #include <floatingpoint.h>
7 #endif
9 #ifdef HAVE_FLOAT_H
10 #include <float.h>
11 #endif
13 #ifdef HAVE_IEEEFP_H
14 #include <ieeefp.h>
15 #endif
17 #include "shotgun/lib/shotgun.h"
18 #include "shotgun/lib/string.h"
19 #include "shotgun/lib/array.h"
20 #include "shotgun/lib/bignum.h"
21 #include "shotgun/lib/float.h"
23 int float_radix() { return FLT_RADIX; }
24 int float_rounds() { return FLT_ROUNDS; }
25 double float_min() { return DBL_MIN; }
26 double float_max() { return DBL_MAX; }
27 int float_min_exp() { return DBL_MIN_EXP; }
28 int float_max_exp() { return DBL_MAX_EXP; }
29 int float_min_10_exp() { return DBL_MIN_10_EXP; }
30 int float_max_10_exp() { return DBL_MAX_10_EXP; }
31 int float_dig() { return DBL_DIG; }
32 int float_mant_dig() { return DBL_MANT_DIG; }
33 double float_epsilon() { return DBL_EPSILON; }
35 OBJECT float_new(STATE, double dbl) {
36 double *value;
37 OBJECT o;
38 o = object_memory_new_opaque(state, BASIC_CLASS(floatpoint), sizeof(double));
39 value = (double *)BYTES_OF(o);
40 *value = dbl;
41 return o;
44 /* This functions is only used when unmarshalling.
45 * The assumptions made here are therefore safe.
46 * String#to_f uses string_to_double
48 OBJECT float_from_string(STATE, char *str) {
49 char *endp;
50 double d;
51 d = ruby_strtod(str, &endp);
52 if (str != endp && *endp == '\0') {
53 return float_new(state, d);
55 return Qnil;
58 void float_into_string(STATE, OBJECT self, char *buf, int sz) {
59 snprintf(buf, sz, "%+.17e", FLOAT_TO_DOUBLE(self));
62 OBJECT float_compare(STATE, double a, double b) {
63 if(a < b) {
64 return I2N(-1);
65 } else if(a > b) {
66 return I2N(1);
68 return I2N(0);
71 OBJECT float_coerce(STATE, OBJECT value) {
72 if(FIXNUM_P(value)) {
73 return float_new(state, (double)N2I(value));
74 } else if(BIGNUM_P(value)) {
75 return float_new(state, bignum_to_double(state, value));
77 return value;