Rounding value and storing that value to variable

Hi folks! long time lurker here and decided to register since I couldn't find the answer to this.

Im having problems with something. I want to be able to round off a value to 1 decimal place and then store that value so that I can use it for cmath to use the tan( ) function.

Here's a sample code that I just wrote, sorry if there are any syntax errors.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
 double X1, X2
 const double Pi = 3.1459

 cout << "Enter the value: ";
 cin >> X1;
 
 X2 = (X1 * Pi)/180;

 cout << "The tangent is: " <<setprecision(4) <<fixed << tan(X2);


Basically I need to round off the X1 value that the user inputted and calculate the rounded value to convert that to radians so that I can use the tan function.

Any help is greatly appreciated.
Hi
Sorry I'm maybe being obtuse but why would you want to round off the value of X1 in the first place? Isn't that just going to give you a less accurate result?
Yes it would be a less accurate result but I want to either round up or down so that I can print a table of tangents that I can start in. For example 38.69 will round to 38.70, I would take that value as my starting value convert it to radians and print the tan, next I would do 37.71, then 37.72, 37.73,.....

Thanks

Try this link. There's some source code which I think would do what you're after:
http://www.cplusplus.com/forum/general/4011/
Regards, keineahnung
Not really related, but your value of Pi is wrong. It's 3.14159, not 3.1459.
Will look at that link a little later.

thanks for the clarification of Pi freddy91lol i meant 92 =)

But does anyone else have a solution to this?
@rapidxone
Will look at that link a little later.

Well, if you looked at it first, you'd see there's a snippet of source code that does exactly what you want, so then there wouldn't be any need for anyone else to post a solution ;)


Anushi (5) Oct 7, 2008 at 9:11am
@buffbill
i m sorry but thr is a slight flaw in the code u gave.
for a number say 7.13, it'll return 7.2 instead of 7.1.so i propose a correction in ur code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
float rndup(float n)//round up a float type and show one decimal place
{
      float t;
      t=n-floor(n);
      if (t>=0.5)    
      {
              n*=10;//where n is the multi-decimal float
              ceil(n);
              n/=10;
              }
      else 
      {
              n*=10;//where n is the multi-decimal float
              floor(n);
              n/=10;
              }
      return n;
}          

floor() and ceil() both are defined in <cmath>
Last edited on
Thanks Keineahnung, I did look at the code and tried to see if that would work and my program just hangs. I made sure to change the variables as well.

I ended up finding a solution which worked for me.

When I call the Min_Angle_Rounded it shows up either rounded up or down depending on the value I inputed. I don't know if I have to do an If statement since whenever I put a value of 38.68 it rounds to 38.7 and when I put a value of 30.43 it rounds to 30.4 which is what I need.

Do you think I would need an If statement?
Last edited on
Topic archived. No new replies allowed.