Need precision using modulus

C

Chris Watson

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?

Thanks for the help,

--

Chris
-
No matter what happens, somebody will find a way to take it too
seriously. -Dave Barry
 
C

Corey Murtagh

Chris said:
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.
 
C

Chris Watson

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
 
J

Jack Klein

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?

Thanks for the help,

Post the code that is giving you the message, and the definition of
the variables involved.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 
J

John Harrison

Chris Watson said:
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;
}

There's no reason to use floats here

void printRationalAsFloating()
{
double n = numerator;
double d = denominator;
cout << setprecision(3) << n/d;
}

floats are likely to be slower than doubles. There only advantage is to
occupy less space, and that isn't a consideration here.

john
 
J

John Harrison

Chris Watson said:
class Rational
{
public:
//Constructor Definition
Rational(int num = 1.0, int den = 1.0) //N/D both default to 1

Strange, why not?

Rational(int num = 1, int den = 1) //N/D both default to 1

I beginning to think This Isn't The Real Code.

Always post the real code, it will help you get an accurate answer.

john
 
Y

yvan joffre

Chris Watson said:
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
}

Hi,

I think that float type has a 23 bit mantissa (or somethink like
that). If int type is 32 bit wide on your architecture, you risk a
precision lost when converting an int to float. If you use an explicit
cast, the warning disappears, but the risk doesn't.

If you use a double variable, the conversion is safe because double
type has wider mantissa (52 bits I think). But you'll get the same
problem if you have 64 bit wide int type ...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top