[erlang-bugs] printing NaN causes exception
Bob Ippolito
bob@REDACTED
Fri May 24 18:33:02 CEST 2013
Erlang's floating point type doesn't allow for NaN or Inf. The only way to
safely get float specials in and out is to leave them as binary, or match
the bit patterns for them and convert to special atoms. It's a pretty sad
state.
On Friday, May 24, 2013, Daniel Goertzen wrote:
> 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
> argv[])
> {
> 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)
> 2>
>
>
>
> 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)
>
> Cheers,
> Dan.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-bugs/attachments/20130524/845c24d1/attachment.htm>
More information about the erlang-bugs
mailing list