Sha256: 182a7abee63eac12b983baa7ccd96af11a59e345c5625dae69cbe6d7b3627aeb

Contents?: true

Size: 1.71 KB

Versions: 192

Compression:

Stored size: 1.71 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 
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)
{
    return value;
}

Version data entries

192 entries across 177 versions & 30 rubygems

Version Path
files.com-1.0.55 docs/vendor/bundle/ruby/2.5.0/gems/ffi-1.9.25/ext/ffi_c/LongDouble.c
vagrant-unbundled-2.2.7.0 vendor/bundle/ruby/2.7.0/gems/ffi-1.11.1/ext/ffi_c/LongDouble.c
vagrant-unbundled-2.2.7.0 vendor/bundle/ruby/2.4.0/gems/ffi-1.9.18/ext/ffi_c/LongDouble.c
vagrant-unbundled-2.2.7.0 vendor/bundle/ruby/2.6.0/gems/ffi-1.11.1/ext/ffi_c/LongDouble.c
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/ffi-1.11.1/ext/ffi_c/LongDouble.c
vagrant-unbundled-2.2.6.2 vendor/bundle/ruby/2.6.0/gems/ffi-1.11.1/ext/ffi_c/LongDouble.c
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/ffi-1.11.1/ext/ffi_c/LongDouble.c
chatops-rpc-0.0.2 fixtures/chatops-controller-example/vendor/bundle/ruby/2.5.0/gems/ffi-1.11.1/ext/ffi_c/LongDouble.c
chatops-rpc-0.0.1 fixtures/chatops-controller-example/vendor/bundle/ruby/2.5.0/gems/ffi-1.11.1/ext/ffi_c/LongDouble.c
zuora_connect_ui-0.9.1 vendor/ruby/2.6.0/gems/ffi-1.11.1/ext/ffi_c/LongDouble.c
vagrant-unbundled-2.2.6.1 vendor/bundle/ruby/2.6.0/gems/ffi-1.11.1/ext/ffi_c/LongDouble.c
vagrant-unbundled-2.2.6.0 vendor/bundle/ruby/2.6.0/gems/ffi-1.11.1/ext/ffi_c/LongDouble.c
zuora_connect_ui-0.9.0 vendor/ruby/2.6.0/gems/ffi-1.11.1/ext/ffi_c/LongDouble.c
cloudsmith-api-0.44.4 vendor/bundle/ruby/2.3.0/gems/ffi-1.11.1/ext/ffi_c/LongDouble.c
zuora_connect_ui-0.8.3 vendor/ruby/2.6.0/gems/ffi-1.11.1/ext/ffi_c/LongDouble.c
vagrant-unbundled-2.2.5.0 vendor/bundle/ruby/2.5.0/gems/ffi-1.9.25/ext/ffi_c/LongDouble.c
vagrant-unbundled-2.2.5.0 vendor/bundle/ruby/2.5.0/gems/ffi-1.10.0/ext/ffi_c/LongDouble.c
vagrant-unbundled-2.2.5.0 vendor/bundle/ruby/2.6.0/gems/ffi-1.10.0/ext/ffi_c/LongDouble.c
zuora_connect_ui-0.8.2 vendor/ruby/2.6.0/gems/ffi-1.11.1/ext/ffi_c/LongDouble.c
zuora_connect_ui-0.8.1 vendor/ruby/2.6.0/gems/ffi-1.11.1/ext/ffi_c/LongDouble.c