Try to design a rational number class

closed account (oG8U7k9E)
...
Last edited on
3/2 is integer division, so the result is 1 with a remainder of 1.
If you want real division, do 3.0/2.0. To get it in float, it's 3.0f/2.0f.
Last edited on
closed account (oG8U7k9E)
Is there anyway to ensure that when I write "3/2" is would by default perform real division, and never integer division?
No.
closed account (oG8U7k9E)
could I make it throw an error?
Yeah:
std::cout << "OH NO ERROR HAPPENED OMG OMG OMG";

What are you trying to do with try... catch?
I thought the compiler was supposed to give a warning about implicit conversion from int to double, but apparently, it doesn't even do that. No, you can't make the compiler give an error, either.
I think you should add a constructor that takes two ints.
#error Error: You put an error directive in your code!
That's not really useful, in this case.
I can't see why it's ever useful without a #if clause, I just wanted to say you can make the compiler give an error. Or at least, the preprocessor...
Without an #if, #error is really useful when you want to play a prank.
More subtle ones include removing the f at the end of float literals. That forces the compiler to convert to double and back to float
Last edited on
Except when the compiler error comes out like this: "#error my error".

If you could get rid of the #error part of the message, it would be good.
I personally like the __LINE__ macro. I like to inform my users on what particular line in my source code they made an idiotic mistake.
closed account (oG8U7k9E)
It is just an exercise--if I can't, I can't. It just seems proper to me that I should be able write a rational number as a rational number in the code (and If I can't I should be able to prevent someone from making what I consider an easy mistake to make.)

The trys and catches in my code are just boilerplate--I only wrote this program so that I can periodically test the class I am trying to design.

I still have other problems with this code that I have not even tried to solve--like what to do if either the numerator or the denominator become to large (I believe that somewhere over 2 million you can no longer use integers.)

Sorry about not responding quicker, but wife insist that I attend a holiday celerbration.

Thanks for the help.
32-bit ints have an upper limit of 2^31-1, or 2,147,483,647.
Topic archived. No new replies allowed.