exact math

Hello -
I am trying to make a program that will calculate some flat sheet metal layouts for me, and am running into trouble because the "double" numbers I am using keep going to the wrong number of decimals. I need to have them exactly what I need or I will not be able to add / subtract by 1/10,000 of an inch and get good answers. I have Visual Studio 2008 and am working in (just starting to learn) C++.
I took the number I need (as a double) and multiplied it by 10,000. Then called that an "int" and sent it back to the calling program. Re-typed it as a double here and divided by 10,000 to have something with exactly 4 decimals, but no luck.
What else can I do? I have looked on google and can't find help that I can understand.

By the way, my code works, it just doesn't return the right amount of decimals in the answer. If you put in 123.456789 you get back a 3 place decimal (not 4).

Any help will be gratefully appreciated.
Thanks,
SmokyRick

Here is the code of my program:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// tryout4pl_dec.cpp  RLC 2011-04-08
// trying to get a true 4 place decimal from a one with more or less digits.

#include <iostream>
#include <cmath>
using std::cin;  using std::cout;  using std::endl;

// declare functions
double dec_pl(double nmbr, int pwr);

// declare variables
double nmbr = 0.0;
double pwr = 1;
double returned = 0.0;

// here is where the work is done
double dec_pl(double nmbr, double pwr)
{
	double number1 = 0.0;
	int number2 = 0;
	double number3 = 0.0;
	double power = pow(10, pwr);
	number1 = nmbr * power;
	cout << "number1 = nmbr * power = " << number1 << endl;
	number2 = (static_cast<int>(number1));
	cout << "number2 = (static_cast<int>(number1)) = " << number2 << endl;
	number3 = (static_cast<double>(number2)) / power;
	cout << "number3 = (static_cast<double>(number2)) / power = " << number3 << endl;
	return number3;
}

// and here is main()
int main()
{
	cout << "Enter a number to be converted: ";
	cin >> nmbr;
	cout << "\nEnter a number of decimal places to convert to: ";
	cin >> pwr;
	returned = dec_pl(nmbr, pwr);
	cout << endl << returned << endl << endl;
	return 0;
}

Last edited on
this is simply a format thing. add

1
2
3
#include<iomanip>

cout<<setiosflags(ios::fixed)<<setprecision(4)


to the beginning of your code. c++ actually keeps track of more digits than it shows on screen. using this you can control how many decimal places are shown (you only need it once)

note: the 4 is the number of digits shown after the decimal point.
Last edited on
Wow, thanks for the speedy reply. Just a few more questions, then. Does this set all the farther digits to zero, so that other functions will work properly? That would be great! If not, I still need to do something else.
Also, how did you put the code in a separate block like that? I couldn't find the instructions for that.

Thanks so much,
Smoky Rick
Does this set all the farther digits to zero,

not sure what this means
Zaq says
(you only need it once)

...........meaning you need to reset the value of setprecision() if you subsequently want to show a different number of decimals from the current setting (4).
What I was getting at, is whether this is just a display formatting thing or a real limitation of the number itself. Leaving the number itself with 4 important digits and the rest zeros is what I need. Just a display thing will not do what I need, as it will not divide exactly as I need it to. I am trying to come up with exact divisions of distance for a sheet metal layout and without the proper dimensions it will not work.

Thank for your help, will try this out after work tonight.
Smoky Rick
Got this figured out after a google search and some time with tryouts. The setiosflags is ONLY a display manipulator. I need something that will actually change the number to less digits. I had thought I could use floor(number * 10000); but this does not work.

Any other ideas?

Thanks anyway,
Rick
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <iomanip>
#include <sstream>

using namespace std;

int main() {
    double PI = 3.14159265;
    double PI_new;
    stringstream ss; 

    ss << fixed << setprecision(4) << PI; // insert PI to 4 decimal places
    ss >> PI_new; // extract our new PI from the stream

    cout << PI_new << endl;
}
Topic archived. No new replies allowed.