Sha256: 99b3a556c4d35e1227b6d27353ef49e59a2ac892bc1c63e09465e311502c566a

Contents?: true

Size: 1.73 KB

Versions: 17

Compression:

Stored size: 1.73 KB

Contents

#include "LongDouble.h"
#include <stdio.h>
#include <stdarg.h>
#include <float.h>

#if defined (__CYGWIN__) || defined(__INTERIX)
# 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_cBigDecimal, rb_intern("new"), 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"));
        return strtold(RSTRING_PTR(s), NULL);
    }

    /* 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;
}

Version data entries

17 entries across 16 versions & 5 rubygems

Version Path
vagrant-unbundled-2.2.9.0 vendor/bundle/ruby/2.7.0/gems/ffi-1.12.2/ext/ffi_c/LongDouble.c
vagrant-unbundled-2.2.8.0 vendor/bundle/ruby/2.7.0/gems/ffi-1.12.2/ext/ffi_c/LongDouble.c
talon_one-2.0.0 vendor/bundle/ruby/2.7.0/gems/ffi-1.12.2/ext/ffi_c/LongDouble.c
talon_one-2.0.0 vendor/bundle/ruby/2.3.0/gems/ffi-1.12.2/ext/ffi_c/LongDouble.c
cloudsmith-api-0.49.118 vendor/bundle/ruby/2.6.0/gems/ffi-1.12.2/ext/ffi_c/LongDouble.c
cloudsmith-api-0.49.98 vendor/bundle/ruby/2.6.0/gems/ffi-1.12.2/ext/ffi_c/LongDouble.c
cloudsmith-api-0.49.94 vendor/bundle/ruby/2.6.0/gems/ffi-1.12.2/ext/ffi_c/LongDouble.c
cloudsmith-api-0.49.21 vendor/bundle/ruby/2.6.0/gems/ffi-1.12.2/ext/ffi_c/LongDouble.c
cloudsmith-api-0.49.15 vendor/bundle/ruby/2.6.0/gems/ffi-1.12.2/ext/ffi_c/LongDouble.c
cloudsmith-api-0.49.13 vendor/bundle/ruby/2.6.0/gems/ffi-1.12.2/ext/ffi_c/LongDouble.c
cloudsmith-api-0.49.9 vendor/bundle/ruby/2.3.0/gems/ffi-1.12.2/ext/ffi_c/LongDouble.c
ffi-1.12.2 ext/ffi_c/LongDouble.c
ffi-1.12.1 ext/ffi_c/LongDouble.c
ffi-1.12.0 ext/ffi_c/LongDouble.c
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/ffi-1.11.3/ext/ffi_c/LongDouble.c
ffi-1.11.3 ext/ffi_c/LongDouble.c
ffi-1.11.2 ext/ffi_c/LongDouble.c