Is there a way, using gmplib, to print an mpf_t number without the exponent? I would rather not have to write a function that reads the exponent and moves the decimal manually, as that kind of seems like overkill.
I'm not familiar with gmplib, but does it support the fixed formatting manipulator?
In standard C++:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double d = 1.12345e6;
cout << d << endl;
cout << fixed << d << endl;
return 0;
}
produces:
$ ./test
1.12345e+06
1123450.000000
You can play with the precision with setprecision(n) and width with setw(n) to further tweak the result.
in fact, the GMPLib provide the way to print the mpf_t value well. See follows.
#include <stdio.h>
#include <gmp.h>
int main(int argc, const char * argv[]) {
mpf_t f;
mpf_init_set_str(f, "314.15926", 10);
gmp_printf("%.5Ff\n", f);
return 0;
}
if you run the script,the console will output:
314.15926
Here, the most important thing is about 5 in the GMT float format flag : %.5Ff,which means that it will output the float with 5 digitals
Related
I have to convert an mpq_t into a z3::expr. The z3 README says: "It is optionally possible to use GMP [...]." How exactly would this look like? I couldn't find anything gmp related in the API documentation and the following does not work:
#include <z3++.h>
#include <gmp.h>
#include <iostream>
int main()
{
z3::context c;
mpq_t gmp_frac;
mpq_init(gmp_frac);
mpq_set_str(gmp_frac, "22/7", 10);
z3::expr z3_frac = c.real_val(gmp_frac);
std::cout << z3_frac << std::endl;
return 0;
}
It is possible to first convert the mpq_t into an char array (see below), but I am not very satisfied with that. Is there a more direct way?
#include <z3++.h>
#include <gmp.h>
#include <iostream>
int main()
{
z3::context c;
mpq_t gmp_frac;
mpq_init(gmp_frac);
mpq_set_str(gmp_frac, "22/7", 10);
char char_frac[mpz_sizeinbase(mpq_numref(gmp_frac), 10)+mpz_sizeinbase(mpq_denref(gmp_frac), 10)+3];
mpq_get_str(char_frac, 10, gmp_frac);
z3::expr z3_frac = c.real_val(char_frac);
std::cout << z3_frac << std::endl;
return 0;
}
So I know setprecision(int n) should be used when printing a double value with precision n. However, I've run into a problem on a project that I'm working on that is akin to this code:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
double var = 1.0000001;
cout << setprecision(10)<< var << endl;
string str = to_string(var);
cout << str << endl;
return 0;
}
Here is the output:
1.0000001
1.000000
In the project I'm working on, I need to save the double value as a string, and it will occasionally need more than six decimal places of precision. Here, precision is clearly lost in the conversion. Any pointers would be greatly appreciated.
You can use std::stringstream.
#include <iostream>
#include <sstream>
#include <iomanip>
using namespace std;
int main(void) {
double var = 1.0000001;
cout << setprecision(10)<< var << endl;
stringstream ss;
ss << setprecision(10) << var;
string str;
ss >> str;
cout << str << endl;
return 0;
}
If you want to get the full precision of your double without limiting it to a specific value (implied by your "occasionally need more than six decimal places"), and if you are using the Boost libraries, you can also try this following alternative:
#include <boost/lexical_cast.hpp>
#include <iostream>
using namespace boost;
using namespace std;
int main() {
double var = 1.0000001;
cout << lexical_cast<string>(var) << endl;
return 0;
}
This has proven useful in one of my applications where precision did matter, and where the std::stringstream approach was not very convenient and elegant due to usage of some specific logging function wrappers. You can find more information about boost::lexical_cast and how it deals with internal representations here.
Obviously, if you are not currently using Boost in your project, this approach is overkill.
try this:
#include <sstream>
#include <string>
// In some function:
double d = 453.23;
std::ostringstream os;
os << d;
std::string str = os.str();
My code should calculate with maximum precision different powers of different ints. Sometimes I have problem digits after nought point.
Example code:
#include <iostream>
#include <math.h>
#include <iomanip>
using namespace std;
int main()
{
cout << setprecision(10000);
int length = 5;
long double Maxi = pow(10, length);
cout << Maxi << endl;
return 0;
}
prints
100000.00000000000000710542735760100185871124267578125
instead of
100000
Screen: http://postimg.org/image/epdsrinkx/
Is it something to worry about or should I just round down and forget about it?
I use setprecision & fixed, but I want to cut off trailing zeros, how do I do that for cout?
#include <iostream>
#include<iomanip>
int main()
{
double b = 132.7489;
double a = 49.932;
double e = a + b;
std::cout << std::fixed;
std::cout << std::setprecision(20);
std::cout << e;
}
How do I tell the program to automatically cut off the trailing decimal places for the exact answer like a calculator. Sorry this is my first time here. I'm used to Javascript & I've never had a problem with exact math. I've googled everywhere.
This will do it...
#include <iostream>
using namespace std;
int main()
{
float f = 42.43500000f, f2 = 25.004300;
cout << defaultfloat << f << endl;
cout << defaultfloat << f2 << endl;
return 0;
}
defaultfloat is a format flag defined in the std namespace.
If printing is your only concern you could use printf and %g format.
As by default six significant digits are printed ,you could use long enough numeric width like .10.
printf("%.10g",e);
So:
#include <iostream>
#include<cstdio>
int main()
{
double b = 132.7489;
double a = 49.932;
double e = a + b;
printf("%.10g",e);
}
I have a simple program:
#include <stdio.h>
#include <iostream>
using namespace std;
int main(int argc, char **argv)
{
double i=0.000006;
printf("%lf\n",i);
cout <<i<<endl;
return 0;
}
the output of which is :
pearl.236> ./a.out
0.000006
6e-06
pearl.237>
How can i achieve 0.000006 using the cout too?
the actual proble i have is i am redirecting this double to a string stream and later on i am printing it on screen.i want to know how we can store the actuall double representation inside the string stream.
Stream formatting is achieved with the help of manipulators.
The manipluators to specify standard and scientific notation are fixed and scientific.
cout << fixed <<i<<endl;
Try using std::fixed
std::cout << std::fixed << i << "\n";