[erlang-bugs] printing NaN causes exception

Daniel Goertzen <>
Fri May 24 17:33:05 CEST 2013

I am working with a c++ generated floating point data stream that encodes
certain events as NaN (not a number).  When I try to print out this number,
io_lib_format crashes.

Here is a [c++11] nif that creates a NaN:

static ERL_NIF_TERM quiet_nan(ErlNifEnv* env, int, const ERL_NIF_TERM
    double num = std::numeric_limits<double>::quiet_NaN();
    cerr << "quiet_nan: iostream prints num as " << num << endl;
    return enif_make_double(env, num);

... and then when I run this interactively I get:

Erlang R15B01 (erts-5.9.1) [source] [smp:3:3] [async-threads:0]

Eshell V5.9.1  (abort with ^G)
1> channel_nif:quiet_nan().
quiet_nan: iostream prints num as nan
                                     ** exception error: no case clause
matching <<127,248,0,0,0,0,0,0>>
     in function  io_lib_format:mantissa_exponent/1 (io_lib_format.erl,
line 374)
     in call from io_lib_format:fwrite_g/1 (io_lib_format.erl, line 365)

Expected behaviour is to just print "NaN" or something similar.  For my use
case I can work around this problem by just using binary representations.

For reference, there's a thread about inf and nan about 28 Feb 2012 that
focuses on doing match with these numbers. (I do not want to do math, just
move them around and print them without crashing)

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-bugs/attachments/20130524/7e4c2f60/attachment-0001.html>

More information about the erlang-bugs mailing list