fractions in variables?

Started by
7 comments, last by JohnBolton 18 years, 8 months ago
how would i set a variable to like 5/9... is there any way?
Advertisement
I assume you don't want the binary approximation of the decimal that you will get if you divide 5 by 9, but the exact value, right?

I suppose the best way in an object orientated language like C++ is to write your own class to do it, and then store the numerator and denominator as separate integers. Or have a look to see if someone has already written the class for you, as it sounds like something that will already have been done (I'm not a wizard at remembering code libraries, and I don't know what language you are using, so you'll be best Googling for that yourself, or waiting for another reply [smile]).

Hope that was of some help...
If you want to store an irrational number, the precision will never be perfect. However, you can get pretty damn close using floats and doubles instead of ints

For example:
int i = 14 / 8;


In this case, the decimal part of i will be cut off because it is declared to be an integer. However:

double i = 14 / 8;


In this case, i will correctly store the product of 14 / 8, or 1.75.

Like I said before, you're never going to get perfect precision with irrational numbers. But, using doubles/floats as the type for your variable will allow you to get, for the most part, as close as you'll ever need.
#include<iostream>
using namespace std;
int main(void)
{
float a,b;
cout<<"Fahrenheit Temperature=";
cin>>a;
cout<<'\n';
b=(5/9)*(a-32);
cout<<"Celsius Temperature=";
cout<<b;
cin.get();
return 0;
}
C++ compiled right. Everything is zero though for celsius. I think it has to do with the 5/9 ^_^
Ah, I see your problem now. 5 / 9 is an integer division that will round down, and so the compiler will turn that into a zero. What you want is (5.0 / 9.0), which will force that to be a float. Or you can use a cast, by having ((float)5 / 9), I guess, although I always use the former approach.
My guess would be that the compiler is interpreting (5/9) as an integer operation. Try explicitly making the number constants floats: (5.0f/9.0f)

The same may be occuring with the (a-32) term, but I can't remember off the top of my head...

Mike.
ooh the 5.0f/9.0f worked, thank you so much ^_^
if you're using msvc++ the divisor has to be a float to return a float, the rest can be int.
Quote:Original post by Montbrun
if you're using msvc++ the divisor has to be a float to return a float, the rest can be int.

That is not quite true. In an operation involving an int and a float, the int is first promoted to float, regardless of which operand it is. Precedence is also a factor.

1 / 2 = 0
1.0 / 2 = 0.5
1 / 2.0 = 0.5

3 / 4 / 5 = 0, because (3/4)/5 = 0/5 = 0
3 / 4 / 5.0 = 0.0, because (3/4)/5.0 = 0/5.0 = 0.0
3 / 4.0 / 5 = 0.15, because (3/4.0)/5 = 0.75/5 = 0.15
3.0 / 4 / 5 = 0.15, because (3.0/4)/5 = 0.75/5 = 0.15


John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!

This topic is closed to new replies.

Advertisement