Solved

possible loss of data warning.

Posted on 2006-11-02
15
326 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
  • 7
  • 3
  • 2
  • +2
15 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 375 total points
Comment Utility
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
Comment Utility
>> 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
Comment Utility
Btw, with some algebra, you can reduce this equation to 100 * a / b, which may help in preserving precision.
0
 
LVL 6

Expert Comment

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

Expert Comment

by:Exceter
Comment Utility
Be nice. We were all beginners once...
0
 
LVL 8

Expert Comment

by:Exceter
Comment Utility
Maybe I missed something but, how are you getting

100 * a / b

out of,

a / b / 100

?
0
 
LVL 8

Expert Comment

by:Exceter
Comment Utility
You could say,

.01 * a / b

but not,

100 * a / b
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 3

Author Comment

by:Unimatrix_001
Comment Utility
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
Comment Utility
Exceter: Is

.01 * a / b

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

Expert Comment

by:Exceter
Comment Utility
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
Comment Utility
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_
Comment Utility
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
Comment Utility
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
Comment Utility
"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
Comment Utility
>> 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

762 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now