Skip to content

Pretty float #914

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

Merged
merged 3 commits into from
May 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions regression/cbmc/printf1/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <stdio.h>
#include <assert.h>

int main()
{
printf("PRINT d1 %d, %d\n", 123, -123);
printf("PRINT g1 %g, %g, %g, %g\n", 123.0, -123.0, 123.123, 0.123);
printf("PRINT e1 %e, %e, %e, %e\n", 123.0, -123.0, 123.123, 0.123);
printf("PRINT f1 %f, %f, %f, %f\n", 123.0, -123.0, 123.123, 0.123);
assert(0);
}
11 changes: 11 additions & 0 deletions regression/cbmc/printf1/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CORE
main.c
--trace
^EXIT=10$
^SIGNAL=0$
^PRINT d1 123, -123$
^PRINT g1 123, -123, 123\.123, 0\.123$
^PRINT e1 1\.230000e\+2, -1\.230000e\+2, 1\.231230e\+2, 1\.230000e-1$
^PRINT f1 123\.000000, -123\.000000, 123\.123000, 0\.123000$
--
^warning: ignoring
11 changes: 11 additions & 0 deletions src/ansi-c/printf_formatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,18 @@ void printf_formattert::process_format(std::ostream &out)
out << ch;
break;

case 'e':
case 'E':
format_constant.style=format_spect::stylet::SCIENTIFIC;
if(next_operand==operands.end())
break;
out << format_constant(
make_type(*(next_operand++), double_type()));
break;

case 'f':
case 'F':
format_constant.style=format_spect::stylet::DECIMAL;
if(next_operand==operands.end())
break;
out << format_constant(
Expand All @@ -169,6 +179,7 @@ void printf_formattert::process_format(std::ostream &out)

case 'g':
case 'G':
format_constant.style=format_spect::stylet::AUTOMATIC;
if(format_constant.precision==0)
format_constant.precision=1;
if(next_operand==operands.end())
Expand Down
34 changes: 24 additions & 10 deletions src/util/ieee_float.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,25 +161,39 @@ std::string ieee_floatt::format(const format_spect &format_spec) const

case format_spect::stylet::AUTOMATIC:
{
// On Linux, the man page says:
// "Style e is used if the exponent from its conversion
// is less than -4 or greater than or equal to the precision."
//
// On BSD, it's
// "The argument is printed in style f (F) or in style e (E)
// whichever gives full precision in minimum space."

mp_integer _exponent, _fraction;
extract_base10(_fraction, _exponent);

if(_exponent>=0)
mp_integer adjusted_exponent=base10_digits(_fraction)+_exponent;

if(adjusted_exponent>=format_spec.precision ||
adjusted_exponent<-4)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to have a comment explaining the magic number -4?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't actually know where that is from (was there before); and actual implementations appear to be different.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind adding a comment to that effect? In particular, the fact that actual implementations appear to differ sounds interesting to me.

{
if(base10_digits(_fraction)+_exponent>=format_spec.precision)
result+=to_string_scientific(format_spec.precision);
else
result+=to_string_decimal(format_spec.precision);
result+=to_string_scientific(format_spec.precision);
}
else // _exponent<0
else
{
if(true) // base10_digits(fraction)+_exponent<-4)
result+=to_string_scientific(format_spec.precision);
else
result+=to_string_decimal(format_spec.precision);
result+=to_string_decimal(format_spec.precision);

// Implementations tested also appear to suppress trailing zeros
// and trailing dots.

{
std::size_t trunc_pos=result.find_last_not_of('0');
if(trunc_pos!=std::string::npos)
result.resize(trunc_pos+1);
}

if(!result.empty() && result.back()=='.')
result.resize(result.size()-1);
}
}
break;
Expand Down