• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 342
  • Last Modified:

possible loss of data warning.

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
Unimatrix_001
Asked:
Unimatrix_001
  • 7
  • 3
  • 2
  • +2
2 Solutions
 
ozoCommented:
A divide by 0 may cause worse than loss of data, but the compiler may prefer
result=(int)((float)a/((float)b/100));
0
 
ExceterCommented:
>> 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
 
aescntCommented:
Btw, with some algebra, you can reduce this equation to 100 * a / b, which may help in preserving precision.
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
_iskywalker_Commented:
really ****** question, 0/0; what you expect from it?
0
 
ExceterCommented:
Be nice. We were all beginners once...
0
 
ExceterCommented:
Maybe I missed something but, how are you getting

100 * a / b

out of,

a / b / 100

?
0
 
ExceterCommented:
You could say,

.01 * a / b

but not,

100 * a / b
0
 
Unimatrix_001Author Commented:
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
 
Unimatrix_001Author Commented:
Exceter: Is

.01 * a / b

Any different from a performance POV to the solution ozo offered?
0
 
ExceterCommented:
If you'd actually read the thread you'd have realized I was responding to aescnt in that post...
0
 
ExceterCommented:
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
 
_iskywalker_Commented:
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
 
Unimatrix_001Author Commented:
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
 
aescntCommented:
"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
 
ExceterCommented:
>> 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now