By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,171 Members | 943 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,171 IT Pros & Developers. It's quick & easy.

Need precision using modulus

P: n/a
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


Jul 19 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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!"

Jul 19 '05 #2

P: n/a
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!"

Jul 19 '05 #3

P: n/a
On Thu, 7 Aug 2003 21:32:46 -0400, "Chris Watson"
<ch***@hatethespam.com> wrote in comp.lang.c++:
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
Jul 19 '05 #4

P: n/a

"Chris Watson" <ch***@hatethespam.com> wrote in message
news:10***************@newshost03.voicenet.com...
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
Jul 19 '05 #5

P: n/a

"Chris Watson" <ch***@hatethespam.com> wrote in message
news:10***************@newshost03.voicenet.com...
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

Jul 19 '05 #6

P: n/a
"Chris Watson" <ch***@hatethespam.com> wrote in message news:<10***************@newshost03.voicenet.com>.. .
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 ...
Jul 19 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.