Connecting Tech Pros Worldwide Forums | Help | Site Map

Need precision using modulus

Chris Watson
Guest
 
Posts: n/a
#1: Jul 19 '05
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




Corey Murtagh
Guest
 
Posts: n/a
#2: Jul 19 '05

re: Need precision using modulus


Chris Watson wrote:
[color=blue]
> 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?[/color]

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!"

Chris Watson
Guest
 
Posts: n/a
#3: Jul 19 '05

re: Need precision using modulus


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" <emonk@slingshot.co.nz.no.uce> wrote in message
news:1060307504.981554@radsrv1.tranzpeer.net...[color=blue]
> Chris Watson wrote:
>[color=green]
> > I'm trying to eliminate some warnings I'm getting -- probably not[/color][/color]
necessary[color=blue][color=green]
> > but it's my final project:
> >
> > I have to use modulus so my variables are int. Is there some way to[/color][/color]
divide[color=blue][color=green]
> > two results and get a float without getting a "conversion; possible loss[/color][/color]
of[color=blue][color=green]
> > data" warning?[/color]
>
> 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!"
>[/color]


Jack Klein
Guest
 
Posts: n/a
#4: Jul 19 '05

re: Need precision using modulus


On Thu, 7 Aug 2003 21:32:46 -0400, "Chris Watson"
<chris@hatethespam.com> wrote in comp.lang.c++:
[color=blue]
> 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,[/color]

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
John Harrison
Guest
 
Posts: n/a
#5: Jul 19 '05

re: Need precision using modulus



"Chris Watson" <chris@hatethespam.com> wrote in message
news:1060308201.221863@newshost03.voicenet.com...[color=blue]
> 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;
> }[/color]

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


John Harrison
Guest
 
Posts: n/a
#6: Jul 19 '05

re: Need precision using modulus



"Chris Watson" <chris@hatethespam.com> wrote in message
news:1060308201.221863@newshost03.voicenet.com...[color=blue]
> class Rational
> {
> public:
> //Constructor Definition
> Rational(int num = 1.0, int den = 1.0) //N/D both default to 1[/color]

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



yvan joffre
Guest
 
Posts: n/a
#7: Jul 19 '05

re: Need precision using modulus


"Chris Watson" <chris@hatethespam.com> wrote in message news:<1060308201.221863@newshost03.voicenet.com>.. .[color=blue]
> 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
> }
>[/color]

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 ...
Closed Thread