Need more precision in the calculations....

AI Thread Summary
The discussion centers on the use of the pow function in C++ for calculating the square of a small number, specifically x=0.005. The original poster experiences a memory error when executing pow(x, 2) and seeks advice on how to resolve this issue. Participants suggest that the problem may stem from uninitialized variables or improper use of the setprecision function, which only affects output formatting and not the actual calculations. They recommend restructuring the code to ensure proper initialization and execution order, emphasizing that setprecision should be applied before outputting results. Additionally, the conversation touches on the GMP library, which provides multiple precision arithmetic capabilities, allowing for greater accuracy in calculations but potentially at the cost of speed. The importance of providing a minimal, complete example for troubleshooting is also highlighted. Overall, the key points focus on correct usage of mathematical functions, output formatting, and the potential benefits of using specialized libraries for precision.
Leonardo Machado
Messages
56
Reaction score
2
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:
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 ?
 
Technology news on Phys.org
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)
 
Hey Leonardo Machado.

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.
 
  • Like
Likes Pepper Mint
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:
#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.
 
chiro said:
If the setprecision function actually sets the precision
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.
 
  • Like
Likes Pepper Mint
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:
  • Like
Likes Pepper Mint
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.
 
Back
Top