C++ output irrational numbers

Sep 13, 2008 at 12:14am
Hi!! I just started programming in c++ and I am using DevC++ to program in. I am working on something to help me with my trig homework, and I would like to output
exact numbers such as 3/2 rather than decimal numbers, is there any way to go about doing this? oh and btw I love programming, plan on going into it as my profession!
Sep 13, 2008 at 2:18am
Hrm, with the crappiness of floats and doubles, I don't believe there is any "easy" way of doing this...I think you would have to % 1 the number, then check for the possiblities (although doing this would still have problems with floats and doubles -_-)
Sep 13, 2008 at 2:40am
I think GMP could do this.
Sep 13, 2008 at 9:13am
Well, I just started programming as well, so all I could come with is a way to simulate that. Basically, all I did is to find the greatest common divisor, divide each number by it and finally displaying them with a / in between.
All you can use it for is to see any fraction to its lowest terms but then again, that's what you asked for "output exact numbers." What I mean is, you won't be able to use that number to do any further calculations.

#include <iostream>

using namespace std;


int division (int x1, int y1)

{
int z = x1 % y1;

if (z != 0)
{
return division (y1, z);
}
return y1;
}

int main()
{
int x, y;

cin >> x;
cin >> y;


cout << x / division(x, y) << "/" << y / division(x, y) << endl;


return 0;
}
Sep 13, 2008 at 11:04pm
Hey that just might work!! Thank you! Further calculations may be needed, but for this it should work for this program!
Sep 15, 2008 at 3:57am
If you restrict your trig output from going further than the ratio stage you could then invoke a (say) 'reduce () ' function which will guarantee that the ratio pair will have no common divisors greater than 1. (the smallest numerical pair able to express that ratio)
It would look something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void reduce()
int g=gcd(num,den);//where num and den is numerator and denominator
{num/=g;
den/=g;
cout<<num<<den<<endl;
}
and:
int gcd(int m,int n)
{  
  while(m>0)
    {if (m<n)swap(m,n);
      m-=n;}
   return n;
}

You would need to write the swap function or use the one in the <algorithm> header file
Also there are no features in the above to take care of zero or negative numbers. eg den can't be zero.
Sep 25, 2008 at 4:15am
Wow! That is amazing! Thank you! So in order to take care of a negative number, could I just multiply the negative number by -1 and test to see if either one or both of the original numbers were negative, and if one was then the output would be negative, or if they were both the same, the resulting number would be positive?
Sep 25, 2008 at 6:50pm
Well, a division of negative with negative is positive, as the rule of the arithmetic calculation.

-/-=+
+ / - = + or - depending of the biggest value.

Just to help, thats all.

Thanks
Sep 25, 2008 at 8:45pm
Well, a division of negative with negative is positive, as the rule of the arithmetic calculation.

-/-=+
+ / - = + or - depending of the biggest value.


What in the world?!?!
A positive divided by a negative is ALWAYS negative. It has nothing to do with the larger value.
Sep 26, 2008 at 1:01am
@PSPMAN90

Are you talking about irrational numbers? or regular integers?
Topic archived. No new replies allowed.