Need more precision in the calculations....

In summary: If the library you're using doesn't have the function you need, you'll get an error.Plus, there's always the possibility that the library you're using might change in the future and the function you need will no longer be available.In summary, this conversation is about the pow function. The OP is trying to use the pow function but is getting an error. The pow function can only be used for displaying data in the console, it is not accurate and the OP needs to post a minimal example that reproduces the problem in order to get help.
  • #1
Leonardo Machado
57
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
  • #2
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
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
  • #4
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.
 
  • #5
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
  • #6
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
  • #7
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.
 

Related to Need more precision in the calculations....

1. How can I ensure more precision in my calculations?

In order to achieve more precision in your calculations, you can increase the number of significant figures used in your calculations. This will result in a more accurate and precise answer.

2. What are the benefits of having more precision in calculations?

Having more precision in your calculations can lead to more accurate and reliable results, which is crucial in scientific research and experiments. It can also help to minimize errors and provide a more thorough understanding of the data.

3. Are there any tools or techniques that can help improve precision in calculations?

Yes, there are various tools and techniques that can aid in improving precision in calculations. These include using more accurate measuring instruments, performing multiple trials or repetitions, and using advanced mathematical methods such as interpolation or extrapolation.

4. What are some common sources of error that can affect precision in calculations?

Some common sources of error that can affect precision in calculations include measurement errors, rounding errors, and human error in data entry or manipulation. Environmental factors such as temperature, humidity, and equipment calibration can also impact precision.

5. How can I check for the accuracy and precision of my calculations?

To check for the accuracy and precision of your calculations, you can compare your results with known or expected values, perform repeated trials to assess consistency, and use statistical analysis to evaluate the data. It is also important to ensure that all measurements and data are recorded correctly and any potential sources of error are identified and addressed.

Similar threads

  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
8
Views
3K
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
6
Views
1K
Replies
36
Views
4K
Back
Top