C/++/# Need more precision in the calculations...

Tags:
1. Aug 14, 2016

Hello friends, today i have this question about precision. Actually i'm trying to do a simple use of the pow function for a small number x=0.005 squared. By entrying pow ( x, 2) my console does not output the proper number 0.000025, but some memory error.

I've read about it and i may be able to change how many characters i want to see in console by using the comand:

Code (C):
cout << setprecision (n) << pow ( x, 2);
for an arbitrary number of characters n.

It is a function of <iomanip> and only has effects on the presentations of data in console, does not accurate the math opperators ( for y / pow ( x, 2) for example), and that is what i need, i've heard about a library called GMP, created by this propose, is that right ? Do you guys have any tips about it ?

2. Aug 14, 2016

Staff: Mentor

Here's some examples of the pow function that you can try:

http://en.cppreference.com/w/cpp/numeric/math/pow

I don't see why your program had a memory fault unless the x value was not initialized when you called the pow function. Check the examples in the link above to see if your program has done the same things, same dataypes, same assignments...

Also try a simpler version like pow(3.0,2) and then pow(0.5,2) and then pow(0.0005,2)

3. Aug 14, 2016

chiro

You are trying to output information to the standard output but you are (by inference of your code) using the setprecision command for your output.

If the setprecision function actually sets the precision, then you need to run that function before you actually start executing the pow() function and getting the output.

Try restructuring your code so that you initialize the math library (if you need to), set the precision information for the math library (if you can), calculate the power function given an input base and exponent and then take that input and copy it to the standard output.

That is a set of four possible commands:

1) Initialize the math library (possible)
2) Set the precision (possible and optional)
3) Calculate the power of a base and exponent after declaring and initializing all required variables
4) Send the result to the standard output.

4. Aug 14, 2016

Integrand

Other than pow(x, 2) being inferior to x * x, the code posted by the OP is fine. std::setprecision has nothing to do with the math library.

The OP needs to post a complete, minimal example that reproduces the problem. "some memory error": for help with the error, tell us what it is.

Filling out the OP's example, and presuming n and x are variables previously declared, we have:

Code (Text):

#include <cmath>
#include <iomanip>
#include <iostream>

int main(int argc, char* argv[]) {
int n=3;
double x=0.005;
std::cout << std::setprecision(n) << pow(x, 2);
return 0;
}

This works fine for me.

5. Aug 14, 2016

Staff: Mentor

In C++, setprecision() affects only the displayed output, not the calculations or the data stored in a variable. It and the other things in <iomanip> are analogous to Fortran's FORMAT statement.

6. Aug 14, 2016

Staff: Mentor

With regard to GMP, it is one of the so-called "bignum" ( big number or multiple precision arithmetic) libraries. It allows setting the default maximum precision of integer and floating point operations to some large numbers of digits. It also slows speed of computation, more digits of precision == slower.

See: https://en.wikipedia.org/wiki/List_of_C++_multiple_precision_arithmetic_libraries

The C++ double datatype usually has 15 digits of precision in most implementations, and bignum libraries extend the number of digits substantially.

Last edited: Aug 15, 2016
7. Aug 15, 2016

chiro

The point I was trying to make is that it's a better idea to structure the calls made to functions rather than sandwich them in a cout statement.

It's a bit of a risk to do something like that.