Advertisement Jump to content
Sign in to follow this  

fractions in variables?

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 this post

Link to post
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 this post

Link to post
Share on other sites
using namespace std;
int main(void)
float a,b;
cout<<"Fahrenheit Temperature=";
cout<<"Celsius Temperature=";
return 0;
C++ compiled right. Everything is zero though for celsius. I think it has to do with the 5/9 ^_^

Share this post

Link to post
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 this post

Link to post
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...


Share this post

Link to post
Share on other sites
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

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!