Sha256: c5f1b398cb1d41ada5b9fb112c64227b2d720dadaa1e1053917af2f282ad1015

Contents?: true

Size: 1.8 KB

Versions: 5555

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,555 entries across 5,551 versions & 30 rubygems

Version Path
affixapi-1.1.102 vendor/bundle/ruby/3.3.0/gems/ffi-1.16.3/ext/ffi_c/LongDouble.c
mux_ruby-3.20.0 vendor/bundle/ruby/3.2.0/gems/ffi-1.16.3/ext/ffi_c/LongDouble.c
ory-client-1.15.12 vendor/bundle/ruby/3.1.0/gems/ffi-1.17.0/ext/ffi_c/LongDouble.c
direct7-0.0.18 vendor/bundle/ruby/2.7.0/gems/ffi-1.16.3/ext/ffi_c/LongDouble.c
direct7-0.0.17 vendor/bundle/ruby/2.7.0/gems/ffi-1.16.3/ext/ffi_c/LongDouble.c
ory-client-1.15.10 vendor/bundle/ruby/3.1.0/gems/ffi-1.17.0/ext/ffi_c/LongDouble.c
mux_ruby-3.19.0 vendor/bundle/ruby/3.2.0/gems/ffi-1.16.3/ext/ffi_c/LongDouble.c
direct7-0.0.16 vendor/bundle/ruby/2.7.0/gems/ffi-1.16.3/ext/ffi_c/LongDouble.c
ory-client-1.15.7 vendor/bundle/ruby/3.1.0/gems/ffi-1.17.0/ext/ffi_c/LongDouble.c
affixapi-1.1.101 vendor/bundle/ruby/3.3.0/gems/ffi-1.16.3/ext/ffi_c/LongDouble.c
ory-client-1.15.6 vendor/bundle/ruby/3.1.0/gems/ffi-1.17.0/ext/ffi_c/LongDouble.c
affixapi-1.1.100 vendor/bundle/ruby/3.3.0/gems/ffi-1.16.3/ext/ffi_c/LongDouble.c
affixapi-1.1.99 vendor/bundle/ruby/3.3.0/gems/ffi-1.16.3/ext/ffi_c/LongDouble.c
cloudsmith-api-2.0.15 vendor/bundle/ruby/2.6.0/gems/ffi-1.17.0/ext/ffi_c/LongDouble.c
ory-client-1.15.5 vendor/bundle/ruby/3.1.0/gems/ffi-1.17.0/ext/ffi_c/LongDouble.c
cloudsmith-api-2.0.14 vendor/bundle/ruby/2.6.0/gems/ffi-1.17.0/ext/ffi_c/LongDouble.c
ory-client-1.15.4 vendor/bundle/ruby/3.1.0/gems/ffi-1.17.0/ext/ffi_c/LongDouble.c
ory-client-1.15.3 vendor/bundle/ruby/3.1.0/gems/ffi-1.17.0/ext/ffi_c/LongDouble.c
ory-client-1.15.0 vendor/bundle/ruby/3.1.0/gems/ffi-1.17.0/ext/ffi_c/LongDouble.c
affixapi-1.1.94 vendor/bundle/ruby/3.3.0/gems/ffi-1.16.3/ext/ffi_c/LongDouble.c