[erlang-questions] Convert bigint() to float() in erl_nif.

Sverker Eriksson sverker@REDACTED
Mon Nov 21 14:32:20 CET 2011


Michael Uvarov wrote:
> How to convert any number to float in erl_nif code?
> Function float/1 is for the erlang version of code, but there is no this
> function for NIFs.
>
>   
There is no support for dynamically sized bignums at all in the current 
erl_nif API. The best you can do now is:

double f;

if (!enif_get_double(env, term, &f)) {
    ErlNifSInt64 sint;
**    ErlNifUInt64 uint;
**
    if (enif_get_int64(env, term, &sint))
        f = (double)sint;
    else if (enif_get_uint64(env, term, &uint))
        f = (double)uint;
    else
        return enif_make_badarg(env);
}


I guess you would want something like:

int enif_get_number_as_double(ErlNifEnv*, ERL_NIF_TERM, double*);


/Sverker, Erlang/OTP, Ericsson




More information about the erlang-questions mailing list