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
zuora_connect_ui-0.8.0 vendor/ruby/2.6.0/gems/ffi-1.11.1/ext/ffi_c/LongDouble.c
dadapush_client-1.0.1 vendor/bundle/ruby/2.3.0/gems/ffi-1.9.25/ext/ffi_c/LongDouble.c
zuora_connect_ui-0.7.1 vendor/ruby/2.6.0/gems/ffi-1.11.1/ext/ffi_c/LongDouble.c
zuora_connect_ui-0.7.0 vendor/ruby/2.6.0/gems/ffi-1.11.1/ext/ffi_c/LongDouble.c
ffi-1.11.1 ext/ffi_c/LongDouble.c
tdiary-5.0.13 vendor/bundle/gems/ffi-1.10.0/ext/ffi_c/LongDouble.c
approveapi-1.0.8 vendor/bundle/ruby/2.6.0/gems/ffi-1.10.0/ext/ffi_c/LongDouble.c
tdiary-5.0.12.1 vendor/bundle/gems/ffi-1.10.0/ext/ffi_c/LongDouble.c
vagrant-unbundled-2.2.4.0 vendor/bundle/ruby/2.5.0/gems/ffi-1.9.25/ext/ffi_c/LongDouble.c
vagrant-unbundled-2.2.4.0 vendor/bundle/ruby/2.6.0/gems/ffi-1.10.0/ext/ffi_c/LongDouble.c
vagrant-unbundled-2.2.4.0 vendor/bundle/ruby/2.5.0/gems/ffi-1.10.0/ext/ffi_c/LongDouble.c
approveapi-1.0.5 vendor/bundle/ruby/2.6.0/gems/ffi-1.10.0/ext/ffi_c/LongDouble.c
alimentos-alu0100945645-0.1.0 vendor/bundle/ruby/2.3.0/gems/ffi-1.9.25/ext/ffi_c/LongDouble.c
alimentos-alu0100945645-1.0.0 vendor/bundle/ruby/2.3.0/gems/ffi-1.9.25/ext/ffi_c/LongDouble.c
vagrant-unbundled-2.2.3.0 vendor/bundle/ruby/2.5.0/gems/ffi-1.10.0/ext/ffi_c/LongDouble.c
vagrant-unbundled-2.2.3.0 vendor/bundle/ruby/2.5.0/gems/ffi-1.9.25/ext/ffi_c/LongDouble.c
ffi-1.10.0 ext/ffi_c/LongDouble.c
tdiary-5.0.11 vendor/bundle/gems/ffi-1.9.25/ext/ffi_c/LongDouble.c
vagrant-unbundled-2.2.2.0 vendor/bundle/ruby/2.5.0/gems/ffi-1.9.25/ext/ffi_c/LongDouble.c
vagrant-unbundled-2.2.0.0 vendor/bundle/ruby/2.5.0/gems/ffi-1.9.25/ext/ffi_c/LongDouble.c