The string representation of float special values is currently dependent on the
target language and, for C grades, the underlying platform. As a result,
values like NaN and Infinity are being printed or converted into strings in
variety of ways. (See, for example bug #348 in Mantis.)
This diff changes float->string conversion to detect float special values and
use a consistent string representation for them.
library/string.m:
library/io.m:
runtime/mercury_float.c:
Check if a float is not-a-number or of infinite magnitude when converting
floats into strings. If so, then use a standard representation of the
value: "nan" for not-a-number values, and "infinity" / "-infinity" for
values of infinite magnitude.
For uppercase conversion specifiers in format strings, we output the above
strings in uppercase (e.g. "NAN", "INFINITY").
library/float.m:
Add is_infinite/1 and is_nan_or_infinite/1 as synonyms for is_inf/1 and
is_nan_or_inf/1 respectively.
Group float classification predicates into their own section. Upcoming
change will add more of them.
NEWS:
Announce the above changes.
tests/general/string_format_special_floats.m:
tests/general/string_format_special_floats.exp:
Modify this test to check that we actually print float special values as
above.
tests/hard_coded/write_float_special.exp:
Conform to the above change.
tests/EXPECT_FAIL_TESTS.all_grades:
Do not expect the write_float_special test case to fail any more since
the expected outputs should be the same on every platform.
The Mercury implementation of string formatting used by the non-C grades did
not handle the various special float values (i.e. nan, inf and -inf) correctly.
For example, nan was with the '%f' conversion specifier was being formatted as
"nan.0". Similarly, for -inf and inf.
library/string.m:
In the Mercury implementation of string formatting handle special
float values correctly. (We format them as per C99.)
tests/general/string_format_special_floats.{m,exp}:
Extend this test to cover the capitalized versions of the float
conversion specifiers.
Update the formatting of this module.
NEWS:
Announce the above fix.
Estimated hours taken: 0.5
Branches: main
tests/general/string_format_special_floats.m:
tests/general/string_format_special_floats.exp:
Allow `-NaN' as valid output for the NaN tests.
This is not really valid output IMHO, but that's
what the Solaris 2.7 C library printf() outputs,
and it's not useful to report this as a failure in
the Mercury test suite.
Also, split up the tests for +Inf and -Inf,
so that the test case checks that infinities
are formatted with the correct sign.
Estimated hours taken: 2
Branches: main
Reorganize the string__format tests so that we only need one expected
output file for each test.
tests/general/string_format_special_floats.exp:
tests/general/string_format_special_floats.m:
Test that NaNs and infinites are handled correctly, by
internally checking in the test case that we generate a valid
string rather than by checking the expected output.
tests/general/Mmakefile:
Add the new test.
tests/general/string_format/string_format_f.exp:
tests/general/string_format/string_format_f.m:
Remove the test cases of infinite_floats.
Remove the testing of +/-float__max as each C version of
sprintf seems to print the number out to a different
precision.
tests/general/string_format/string_format_e.exp:
tests/general/string_format/string_format_e.m:
tests/general/string_format/string_format_g.exp:
tests/general/string_format/string_format_g.m:
Remove the test cases of infinite_floats.