Rounding?

Write your question here.
Hello! I am writing a bit of code, but the issue I run into is the that values are wrong! :(
I notice that it is not rounding up, I tried set precision, but it only works on Cout? What can I use for a function or a cin?
Thank you so much
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
  #include<iostream>
#include<String>
#include<iomanip>
#include<fstream>
#include<stdio.h>
#include<math.h>

using namespace std;



double FindRadius(double Radius);
double FindSurfaceArea(double Radius, double Height);
double FindVolume(double Radius, double Height);
int main()
{
	// Read the File
	string nameCan = "";
	double Height = 0.0;
	double Radius = 0.0;
	double actualRadius = 0.0;
	double surfaceArea = 0.0;
	double Volume = 0.0;
	ifstream doTheCancan;
	doTheCancan.open("DataFile.txt", ios::in);
	if (!doTheCancan.is_open())
	{
		cout << "Oops" << endl;
		system("pause");
		exit(1);
	}// If it doesn't Open
	while (!doTheCancan.eof())
	{
		getline(doTheCancan, nameCan);
		doTheCancan >> Height;
		doTheCancan >> Radius;
		doTheCancan.ignore();//ignore next line	
		cout << setprecision(2);
		actualRadius = FindRadius(Radius);

		surfaceArea = FindSurfaceArea(actualRadius, Height);
		
		Volume = FindVolume(actualRadius, Height);


		//Debugging
		cout << nameCan << ":" << endl;
		cout << "*****************************"<<endl;
		cout << "The height: " << Height << " Cm" << endl;
		cout << "The Radius is " << fixed << setprecision(2) << actualRadius << " Cm" << endl;
		cout << "The Surface Area is: " << fixed << setprecision(2) << surfaceArea << endl;
		cout << "The Volume is: " << fixed << setprecision(2) << Volume << "" << endl;
		cout << "*******************************" << endl << endl;
	}













	




	system("pause");
	return 0;
}

double FindVolume(double Radius, double Height) 
{
	double PI = 3.14;
	double Volume = 0.0;
	Volume = PI*pow(Radius,2.0)*Height;
	return Volume;

}
double FindRadius(double Radius)
	{
		double Found = 0;
		double PI = 3.14;

		Found = Radius / (PI * 2);
		return Found;
	};
double FindSurfaceArea(double Radius, double Height)
	{
		double SurfaceArea = 0.0;
		double PI = 3.14;
		SurfaceArea = 2*PI*Radius*Height+2*(PI*pow(Radius,2.00));
		return SurfaceArea;


};
Last edited on
Hello Helquin,

If you want to round up and have a new value for the left side of the decimal point try "ceil" from the "cmath" header file. There is also "floor" for rounding down and "round" (C++11 on).

As I think you have found "serprecision" only works in the "cout" and only on the right side of the decimal point.

Line 37 may only ignore one character from the input buffer, but that may be all you need. I like to use:
doTheCancan.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.

You do not need line 38 because you also have it in line 50.

Actually after line 46 I would use std::cout << std::fixed << std::showpoint << std::setprecision(2); as you only need it once because it will affect everything after it until you change it.

Hope that helps,

Andy
Thank you Andy, But I was wondering how I would get it to round 2 decimal Places?
so in DataFile.txt I have:
Coin bank
11//Height
23.3//Circumference

After going through the function actualRadius I get a return Value of 3.35987261144649682
and PI is 3.1400000000000000001, giving me absurd numbers. I see that ceil and floor round to the nearest whole number, is there anything to get to two decimal places?

some values can't be represented exactly in float/double. The extra grillionth on pi is not a problem and its just the nature of floating point values. The general rule of thumb is to do everything in as high a precision as you can and then round once at the end, so your rounding isnt accumulated leading to bigger errors later. On that note you might want to use more digits on pi.

you can multiply by some power of 10, in your case, 100, and use floor/ceil with that, and divide back.
> After going through the function actualRadius I get a return Value of 3.35987261144649682
> and PI is 3.1400000000000000001, giving me absurd numbers.

Floating point values are inexact. For instance, the precise mathematical value of 3.14 may not be representable as a floating point number; 3.1400000000000001 (to 16 digits after the decimal) may be the closest approximation that is available.

Just round the result to 2 decimal places in the output.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <cmath>
#include <iomanip>

int main()
{
    std::cout << "representable floating point values around 3.14\n" 
              << "(double, to 16 digits after the decimal point)\n" ;
              
    double n = 3.139999999999995 ;
    for( int i = 0 ; i < 25 ; ++i )
    {
        std::cout << std::fixed << std::setprecision(16) << n << '\n' ;
        // http://en.cppreference.com/w/cpp/numeric/math/nextafter
        n = std::nextafter( n, 100.0 ) ;
    }
}

representable floating point values around 3.14
(double, to 16 digits after the decimal point)
3.1399999999999948
3.1399999999999952
3.1399999999999957
3.1399999999999961
3.1399999999999966
3.1399999999999970
3.1399999999999975
3.1399999999999979
3.1399999999999983
3.1399999999999988
3.1399999999999992
3.1399999999999997
3.1400000000000001
3.1400000000000006
3.1400000000000010
3.1400000000000015
3.1400000000000019
3.1400000000000023
3.1400000000000028
3.1400000000000032
3.1400000000000037
3.1400000000000041
3.1400000000000046
3.1400000000000050
3.1400000000000055

http://coliru.stacked-crooked.com/a/22c8fac08042ecb7
Topic archived. No new replies allowed.