Opened 8 years ago

Last modified 7 years ago

#10103 assigned Bugs

setprecision(0) does displays too many digits of a float128

Reported by: Charles Karney <charles@…> Owned by: John Maddock
Milestone: To Be Determined Component: multiprecision
Version: Boost 1.54.0 Severity: Problem
Keywords: printing Cc:

Description

setprecision(0) is not honored for float128. Compare the display of a double and a float128 (default and fixed):

0.1 0.123400000000000000000000000000000006
0 0.123400000000000000000000000000000006

This is produced with

#include <iostream>
#include <boost/multiprecision/float128.hpp>

int main() {
  double x = 0.1234;
  boost::multiprecision::float128 y = 0.1234Q;
  std::cout << std::setprecision(0) << x << " " << y << "\n";
  std::cout << std::fixed << x << " " << y << "\n";
}

Change History (4)

comment:1 by John Maddock, 8 years ago

Status: newassigned

This is indeed a bug, but I don't know how to fix it without breaking existing code: we've used a precision of 0 to mean "as many digits as you've got", and this is pervasive/consistent throughout the library. I guess we should have used -1 as the "special" value, but it's too late now...

comment:2 by Charles Karney <charles@…>, 8 years ago

Perhaps define an enum for this special value and advertise that it will change in future versions.

comment:3 by Charles Karney <charles@…>, 8 years ago

Perhaps boost can fruitfully distinguish the cases

cout << fixed << setprecision(0) << x;

and

cout << scientific << setprecision(0) << x;

with the former printing x as the nearest integer (ties go to even) and the latter retaining its current interpretation of printing "as many digits as you've got". This would certainly satisfy my requirements and, typically, the "as many digits as you've got" mode implies scientific notation.

comment:4 by Charles Karney <charles@…>, 7 years ago

With C++11 manipulators, I recommend that

cout << scientific << setprecision(0) << x;
cout << hexfloat << setprecision(0) << x;

should print lossless representations of x. While

cout << fixed << setprecision(0) << x;
cout << defaultfloat << setprecision(0) << x;

should mimic whatever the standard library does with doubles.

Note: See TracTickets for help on using tickets.