1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
#include "LongDouble.h"
#include <stdio.h>
#include <stdarg.h>
#include <float.h>
#if defined (__CYGWIN__) || defined(__INTERIX) || defined(_MSC_VER)
# define strtold(str, endptr) ((long double) strtod((str), (endptr)))
#endif /* defined (__CYGWIN__) */
static VALUE rb_cBigDecimal = Qnil;
static VALUE bigdecimal_load(VALUE unused);
static VALUE bigdecimal_failed(VALUE value, VALUE exc);
VALUE
rbffi_longdouble_new(long double ld)
{
if (!RTEST(rb_cBigDecimal)) {
/* allow fallback if the bigdecimal library is unavailable in future ruby versions */
rb_cBigDecimal = rb_rescue(bigdecimal_load, Qnil, bigdecimal_failed, rb_cObject);
}
if (RTEST(rb_cBigDecimal) && rb_cBigDecimal != rb_cObject) {
char buf[128];
return rb_funcall(rb_mKernel, rb_intern("BigDecimal"), 1, rb_str_new(buf, sprintf(buf, "%.35Le", ld)));
}
/* Fall through to handling as a float */
return rb_float_new(ld);
}
long double
rbffi_num2longdouble(VALUE value)
{
if (TYPE(value) == T_FLOAT) {
return rb_num2dbl(value);
}
if (!RTEST(rb_cBigDecimal) && rb_const_defined(rb_cObject, rb_intern("BigDecimal"))) {
rb_cBigDecimal = rb_const_get(rb_cObject, rb_intern("BigDecimal"));
}
if (RTEST(rb_cBigDecimal) && rb_cBigDecimal != rb_cObject && RTEST(rb_obj_is_kind_of(value, rb_cBigDecimal))) {
VALUE s = rb_funcall(value, rb_intern("to_s"), 1, rb_str_new2("E"));
long double ret = strtold(RSTRING_PTR(s), NULL);
RB_GC_GUARD(s);
return ret;
}
/* Fall through to handling as a float */
return rb_num2dbl(value);
}
static VALUE
bigdecimal_load(VALUE unused)
{
rb_require("bigdecimal");
return rb_const_get(rb_cObject, rb_intern("BigDecimal"));
}
static VALUE
bigdecimal_failed(VALUE value, VALUE exc)
{
return value;
}
|