473,320 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Need precision using modulus

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
6 7655
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
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
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

"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

"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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Roger B. | last post by:
Hello, I am working on a personal interest project and have been racking my brain on this problem for about 5 hours total now, and tracking through old newsgroup posts but haven't figuried it...
4
by: Roger Leigh | last post by:
Hello, I'm writing a fixed-precision floating point class, based on the ideas in the example fixed_pt class in the "Practical C++ Programming" book by Steve Oualline (O' Reilly). This uses a...
2
by: Brian van den Broek | last post by:
Hi all, I guess it is more of a maths question than a programming one, but it involves use of the decimal module, so here goes: As a self-directed learning exercise I've been working on a...
2
by: rawCoder | last post by:
Hi All, I have a *.cer file, a public key of some one and I want to encrypt some thing using this public key. Can someone point me to a sample code for Encrypting some file using...
5
by: DAVID SCHULMAN | last post by:
I've been trying to perform a calculation that has been running into an underflow (insufficient precision) problem in Microsoft Excel, which calculates using at most 15 significant digits. For this...
2
by: mdeaver2003 | last post by:
I'm trying to output a double using a precision that varies, governed by the value of a precision variable. In C I can do it like this: double pi = 3.14159; int prec = 4; printf( "%.*f",...
6
by: R.Biloti | last post by:
Hi folks I wrote the naive program to show up the unit roundoff (machine precision) for single and double precision: #include <stdio.h> int main (void) { double x;
2
by: rupert | last post by:
i've got the following code: #include <iostream> #include <string> #include <vector> #include <iomanip> using namespace std; int main(double argc, char* argv) { double r = 0.01;
6
by: paradox6996 | last post by:
import java.util.Scanner; public class Program01C { public static void main(String args) { double purchase, totalTax, salesTax, countyTax, total; salesTax = 0.04; countyTax =...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.