Public Group

# fractions in variables?

This topic is 4840 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

how would i set a variable to like 5/9... is there any way?

##### Share on other sites
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...

##### Share on other sites
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.

##### Share on other sites
#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 ^_^

##### Share on other sites
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.

##### Share on other sites
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.

##### Share on other sites
ooh the 5.0f/9.0f worked, thank you so much ^_^

##### Share on other sites
if you're using msvc++ the divisor has to be a float to return a float, the rest can be int.

##### Share on other sites
Quote:
 Original post by Montbrunif 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

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 9
• 9
• 9
• 14
• 12
• ### Forum Statistics

• Total Topics
633289
• Total Posts
3011231
• ### Who's Online (See full list)

There are no registered users currently online

×