Need more precision in the calculations....

Click For Summary

Discussion Overview

The discussion revolves around issues of precision in calculations using the pow function in C++. Participants explore the behavior of the pow function, output formatting with setprecision, and the potential use of the GMP library for higher precision arithmetic.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant reports a memory error when using pow(0.005, 2) and suggests that the output precision can be adjusted using setprecision from .
  • Another participant questions the cause of the memory fault, suggesting that the variable x may not have been initialized properly.
  • A different participant advises restructuring the code to ensure proper initialization and execution order of commands related to the math library and output.
  • One participant argues that using pow(x, 2) is less efficient than simply calculating x * x, and emphasizes the need for a complete example to diagnose the memory error.
  • Another participant clarifies that setprecision only affects output formatting and does not influence the underlying calculations.
  • A participant introduces GMP as a library for multiple precision arithmetic, noting that it allows for greater precision but may slow down computations.
  • One participant emphasizes the importance of structuring function calls properly rather than combining them in a single output statement.

Areas of Agreement / Disagreement

Participants express differing views on the efficiency of using pow versus direct multiplication, and there is no consensus on the cause of the memory error. The discussion remains unresolved regarding the best approach to handle precision in calculations.

Contextual Notes

There are limitations regarding the assumptions made about variable initialization and the specific nature of the memory error mentioned by the original poster. The discussion also highlights the differences in precision capabilities between standard data types and bignum libraries.

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:
count << 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   Reactions: 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   Reactions: 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   Reactions: 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 count statement.

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

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K