Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Printf.printf "%F" (-.nan) returns -nan #5739

Closed
vicuna opened this issue Aug 25, 2012 · 3 comments
Closed

Printf.printf "%F" (-.nan) returns -nan #5739

vicuna opened this issue Aug 25, 2012 · 3 comments
Milestone

Comments

@vicuna
Copy link

@vicuna vicuna commented Aug 25, 2012

Original bug ID: 5739
Reporter: smimram
Status: closed (set by @xavierleroy on 2015-12-11T18:21:03Z)
Resolution: fixed
Priority: normal
Severity: minor
Version: 3.12.1
Target version: 4.01.0+dev
Fixed in version: 4.01.0+dev
Category: standard library
Related to: #4688
Monitored by: @hcarty smimram @damiendoligez

Bug description

When trying to generate code for OCaml (in OCaml) I expected Printf.printf "%F" to generate a float which is a valid expression in OCaml syntax: according to the documentation "%F converts a floating-point argument to Caml syntax". However,

-nan;;

Error: This expression has type float but an expression was expected of type
int

Printf.printf "%F\n" (-.nan);;

-nan

  • : unit = ()

Maybe should the last expression return "-.nan" (or the syntax could be modified to allow -nan).

Steps to reproduce

Printf.printf "%F\n" (-.nan);;

@vicuna
Copy link
Author

@vicuna vicuna commented Aug 25, 2012

Comment author: @gasche

Confirmed under 3.12.1 and 4.00.

printf "%F" neg_infinity also prints "-inf" which suffers from the same problem. I think this was already discussed in #4688.

If I understand correctly the patch of "dra" in #4688, the effect of (printf "%F" (-.nan)) on the Windows platform (mingw or MSVC) could be to return "nan" instead of the negative nan on 4.00 -- I can't test myself, though.

@vicuna
Copy link
Author

@vicuna vicuna commented Jul 11, 2013

Comment author: @damiendoligez

What the hell is the meaning of the sign bit on nan??? Does -.nan mean "not a negative number"?

This prints "nan" on Mac OS X, but the problem with -inf remains.

Seriously, this needs to be fixed.

@vicuna
Copy link
Author

@vicuna vicuna commented Jul 19, 2013

Comment author: @xavierleroy

The payload of NaNs include the sign bit. So, a sloppy float->string conversion could test the sign bit, emit a "-" sign, then notice the float is a NaN.

Fixed in 4.01 branch (commit 13910) and in trunk (commit 13911) by special-casing NaN and positive/negative infinity for "%F" conversions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant