class Rational
{
public:
//Constructor Definition
Rational(int num = 1.0, int den = 1.0) //N/D both default to 1
{
int factor=den; //Factor starts at den
while (factor > 0) //and counts down
{
if ((den%factor == 0) && (num%factor == 0))
{ break; } //until it finds
//a common factor
factor--;
}
numerator = num/factor; //reduced numerator
denominator = den/factor; //reduced denominator
}
//Print Rational Function
void printRational()
{
cout << numerator << "/" << denominator;
}
//Print Rational as Floating
void printRationalAsFloating()
{
float n = numerator;
float d = denominator;
cout << setprecision(3) << n/d;
}
....
I'm trying to get two Rational numbers' product like 1/2 * 1/2 to Print
Rational as Floating 0.25, but I'm getting warned about turning an int into
a float. I think I'm missing a technique that would get me the result
without the warning.
Chris
-
No matter what happens, somebody will find a way to take it too
seriously. -Dave Barry
"Corey Murtagh" <em***@slingshot.co.nz.no.uce> wrote in message
news:10***************@radsrv1.tranzpeer.net...
Chris Watson wrote:
I'm trying to eliminate some warnings I'm getting -- probably not
necessary but it's my final project:
I have to use modulus so my variables are int. Is there some way to
divide two results and get a float without getting a "conversion; possible loss
of data" warning?
I think by default all automatic conversions to floating point are of
type 'double'. If you're assigning to a var of type 'float' then the
compiler will likely warn you about it.
If you post a small bit of code to demonstrate what you're doing we'll
have more of an idea.
--
Corey Murtagh
The Electric Monk
"Quidquid latine dictum sit, altum viditur!"