How to Change the Precision of Printing with the Stl

How can I change the precision of printing with the stl?

Use std::fixed , this should work for you.

 file1 << std::fixed << std::setprecision(precision)
<< thePoint[0] << "\\"
<< thePoint[1] << "\\"
<< thePoint[2] << "\\";

ofstream reset precision

Retrieve the stream's original precision value first with precision(), store it, change it, do your insertions, then change it back to the stored value.

int main() {
std::stringstream ss;
ss << 1.12345 << " ";

std::streamsize p = ss.precision();

ss.precision(2);
ss << 1.12345 << " ";

ss.precision(p);
ss << 1.12345;

cout << ss.str(); // 1.12345 1.1 1.12345
}

Live demo.

How to print floating with 2 decimals using cout in C++

cout<< fixed <<setprecision(2)<< result<<endl ;

This is the right answer - fixed must be there!

How to format numbers to significant digits using STL

Stolen from another question:

#include <string>
#include <sstream>
#include <cmath>
#include <iostream>

std::string toPrecision(double num, int n) {
https://stackoverflow.com/questions/202302/rounding-to-an-arbitrary-number-of-significant-digits

if(num == 0) {
return "0";
}

double d = std::ceil(std::log10(num < 0 ? -num : num));
int power = n - (int)d;
double magnitude = std::pow(10., power);
long shifted = ::round(num*magnitude);

std::ostringstream oss;
oss << shifted/magnitude;
return oss.str();
}

int main() {
std::cout << toPrecision(123.4567, 2) << "\n";
std::cout << toPrecision(123.4567, 4) << "\n";
std::cout << toPrecision(123.4567, 5) << "\n";
}

Precision of acos function in c++

Use double instead of float to get more precision.

That way you are going to use this prototype:

double acos (double x);


A must read is the Difference between float and double question. From there we have:

  1. As the name implies, a double has 2x the precision of float.
  2. The C and C++ standards do not specify the representation of float,
    double and long double. It is possible that all three implemented as
    IEEE double-precision. Nevertheless, for most architectures (gcc,
    MSVC; x86, x64, ARM) float is indeed a IEEE single-precision
    floating point number (binary32), and double is a IEEE
    double-precision floating point number (binary64).

GDB: Pretty-Print class containing STL container

After some trying, I've found a way that comes very close. I'm basically using the default StdSetPrinter provided with the stdlib, but I'm not using it for printing, just for iterating the set. My code looks like this now:

from libstdcxx.v6.printers import StdSetPrinter

class FooPrinter(object):
def __init__(self, val):
self.val = val

def to_string(self):
return "X: " + str(self.val['x'])

class FooContainerPrinter(object):
def __init__(self, val):
self.val = val

def to_string(self):
return "My Foo Container"

def children(self):
pp = StdSetPrinter("dummy", self.val['content'])
return pp.children()

Now, the default pretty printing magic still adds some boilerplate (basically it outputs "My Foo Container = { … ⟨ pretty-print of the contents ⟩ …}") but that is fine with me. I think it would even be able to not define an own children(), but rather use the pp.children() inside to_string() and thus have full control over the output string.

It has the drawback that the path where libstdc++ puts its default pretty printers needs to be in the PYTHONPATH.



Related Topics



Leave a reply



Submit