Sha256: c5f1b398cb1d41ada5b9fb112c64227b2d720dadaa1e1053917af2f282ad1015

Contents?: true

Size: 1.8 KB

Versions: 5556

Compression:

Stored size: 1.8 KB

Contents

#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;
}

Version data entries

5,556 entries across 5,552 versions & 30 rubygems

Version Path
vagrant-unbundled-2.2.10.0 vendor/bundle/ruby/2.7.0/gems/ffi-1.13.1/ext/ffi_c/LongDouble.c
cloudsmith-api-0.53.1 vendor/bundle/ruby/2.6.0/gems/ffi-1.13.1/ext/ffi_c/LongDouble.c
cloudsmith-api-0.52.121 vendor/bundle/ruby/2.6.0/gems/ffi-1.13.1/ext/ffi_c/LongDouble.c
cloudsmith-api-0.52.92 vendor/bundle/ruby/2.6.0/gems/ffi-1.13.1/ext/ffi_c/LongDouble.c
cloudsmith-api-0.52.79 vendor/bundle/ruby/2.6.0/gems/ffi-1.13.1/ext/ffi_c/LongDouble.c
metanorma-cli-1.3.4 gems/ruby/2.6.0/gems/ffi-1.13.1/ext/ffi_c/LongDouble.c
metanorma-cli-1.3.3.1 gems/ruby/2.6.0/gems/ffi-1.13.1/ext/ffi_c/LongDouble.c
cloudsmith-api-0.52.5 vendor/bundle/ruby/2.6.0/gems/ffi-1.13.1/ext/ffi_c/LongDouble.c
cloudsmith-api-0.52.0 vendor/bundle/ruby/2.6.0/gems/ffi-1.13.1/ext/ffi_c/LongDouble.c
cloudsmith-api-0.51.93 vendor/bundle/ruby/2.3.0/gems/ffi-1.13.1/ext/ffi_c/LongDouble.c
cloudsmith-api-0.51.38 vendor/bundle/ruby/2.6.0/gems/ffi-1.13.1/ext/ffi_c/LongDouble.c
cloudsmith-api-0.51.37 vendor/bundle/ruby/2.6.0/gems/ffi-1.13.1/ext/ffi_c/LongDouble.c
cloudsmith-api-0.51.34 vendor/bundle/ruby/2.6.0/gems/ffi-1.13.1/ext/ffi_c/LongDouble.c
ffi-1.13.1 ext/ffi_c/LongDouble.c
cloudsmith-api-0.51.22 vendor/bundle/ruby/2.6.0/gems/ffi-1.13.0/ext/ffi_c/LongDouble.c
ffi-1.13.0 ext/ffi_c/LongDouble.c