Solved

possible loss of data warning.

Posted on 2006-11-02
15
334 Views
Last Modified: 2012-06-21
Hi there,

I'm getting a few warnings about loss of data, mainly something like this:

##################################
int result=0;
int a=0;
int b=0;

result=(float)a/((float)b/100);
##################################

So a couple of things, firstly, is there a better way of doing this, or is there a way to make the compiler ignore the warning for that specific line (MSVC 2K5)?

Thanks,
Uni
0
Comment
Question by:Unimatrix_001
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
  • 2
  • +2
15 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 375 total points
ID: 17862255
A divide by 0 may cause worse than loss of data, but the compiler may prefer
result=(int)((float)a/((float)b/100));
0
 
LVL 8

Assisted Solution

by:Exceter
Exceter earned 125 total points
ID: 17862403
>> I'm getting a few warnings about loss of data, mainly something like this:
Generally these warnings are "a possible loss of precision." Meaning that by casting a floating point datatype to an integer the float will be truncated to a whole number. I.E. 3.14 would become 3 and 2.79 would become 2. The best way to deal with this is to use variables of the same type.

However, as ozo pointed out, you have far bigger problems. You code will attempt to divide by zero. This is a bad thing because, mathematically, dividing by zero results in an infinite number. So, in finite mathematics, dividing by zero is strictly forbidden. I would suggest initializing a and b to anything other than zero.

For example,

##################################
float result=0;
float a=1;
float b=1;

result=(float)a/((float)b/100);
##################################

Cheers!
Exceter
0
 
LVL 6

Expert Comment

by:aescnt
ID: 17864797
Btw, with some algebra, you can reduce this equation to 100 * a / b, which may help in preserving precision.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:_iskywalker_
ID: 17865232
really ****** question, 0/0; what you expect from it?
0
 
LVL 8

Expert Comment

by:Exceter
ID: 17865676
Be nice. We were all beginners once...
0
 
LVL 8

Expert Comment

by:Exceter
ID: 17865850
Maybe I missed something but, how are you getting

100 * a / b

out of,

a / b / 100

?
0
 
LVL 8

Expert Comment

by:Exceter
ID: 17865865
You could say,

.01 * a / b

but not,

100 * a / b
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 17865930
Hi all,

Thanks for your comments everybody! :) My bad about the /0 problem. In the actual code I check for that although clearly overlooked that when typing that example. Ozo: Yep, that got rid of the warning. Thanks!

_iskywalker_:
Go play with traffic you muppet... Although since you asked, "what you expect from it?", should be obvious being the expert you are. I want a way to do that line so I don't get the warnings or if you used your eyes, a way to make the compiler ignore the warning on that line... So, tell you what, since you're clearly the top expert here and it's such a ****** (whatever that means...) question, you answer it.... If not then bugger off.

Uni.
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 17865934
Exceter: Is

.01 * a / b

Any different from a performance POV to the solution ozo offered?
0
 
LVL 8

Expert Comment

by:Exceter
ID: 17865949
If you'd actually read the thread you'd have realized I was responding to aescnt in that post...
0
 
LVL 8

Expert Comment

by:Exceter
ID: 17865979
My appologies. That was out of line. I thought you were one of those snobs that goes around accusing people of posting duplicate answers and didn't notice that this was your question until after I clicked submit. *drinks more coffee*

The performance is really irrelevant unless you're doing this thousands or hundreds of thousands of times per second. I only posted that because aescnt made a typo when he posted his equation. He said 100 when I think he meant .01.

Sorry for not making that clearer.

Cheers!
Exceter
0
 
LVL 6

Expert Comment

by:_iskywalker_
ID: 17866036
i would make a check if one of the values are zero (it is cheaper operation).
if(a==0){
result=0;
}
else{
if(b==0){
result=0; // or what you expect from a/0
}
else{
result=100*(a/b); // a/b/100=a/(b*0.01)=100*(a/b); since 1/0.01=100*1
}
0/0 is a school question. not an expert question i mean, it is common knowledge that this does not exist if they are integers, if they are results of functions then there is some methods of analysis.
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 17866131
Exceter: Hehehe, no problem mate. :) I was just wondering if the performance was much to bother about, although clearly it's not a massive deal.
0
 
LVL 6

Expert Comment

by:aescnt
ID: 17868539
"Maybe I missed something but, how are you getting 100 * a / b out of, a / b / 100"

The original was a / (b/100), which is the same as a * 1/(b/100), same as a * 100 / b.

I noted that this *may* (or may not) help in preserving accuracy because dealing with larger numbers (a * 100 as opposed to b / 100) may reduce or rid the need to represent fractional parts of the numbers. :)
0
 
LVL 8

Expert Comment

by:Exceter
ID: 17872033
>> The original was a / (b/100), which is the same as a * 1/(b/100), same as a * 100 / b.
Setting the equations equal to each other resulted in a true statement. I stand corrected.

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question