Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

possible loss of data warning.

Posted on 2006-11-02
Medium Priority
336 Views
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
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
• 7
• 3
• 2
• +2

LVL 84

Accepted Solution

ozo earned 1500 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

Exceter earned 500 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

ID: 17864797
Btw, with some algebra, you can reduce this equation to 100 * a / b, which may help in preserving precision.
0

LVL 6

Expert Comment

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

LVL 8

Expert Comment

ID: 17865676
Be nice. We were all beginners once...
0

LVL 8

Expert Comment

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

100 * a / b

out of,

a / b / 100

?
0

LVL 8

Expert Comment

ID: 17865865
You could say,

.01 * a / b

but not,

100 * a / b
0

LVL 3

Author Comment

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

ID: 17865934
Exceter: Is

.01 * a / b

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

LVL 8

Expert Comment

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

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

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

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

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

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

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. â€¦
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticallâ€¦
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relatâ€¦
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.
Suggested Courses
Course of the Month9 days, 16 hours left to enroll