• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

programming and math

Red Squirrel

No Lifer
In terms of accuracy, is there a difference between this:

var1 = (var2/var3)*100;

and this? :

var1 = ((var2*100)/(var3*100))*100;



This is all ints. I'm wondering if the 2nd one is better practice for acuracy, or if it does not matter?

This is C#, but I assume it would apply to any language.
 
Your second function will have overflow/divide by zero errors when var3 reaches the upper end of the int's range.
 
Try this:

var1 = (var2*100)/ var3 ;

As Crusty notes this will cause overflow for large values of var2 (above 2^31 / 100 for 32-bit signed ints), but it will give you a more accurate result than either of your examples for any smaller values of var2.

To see the difference use var2 = 10, var3 = 3.
 
Honestly, if you are that concerned with accuracy use .NET's Decimal class to do your math, and then round/truncate as you see fit on the return of the function.
 
I'm not sure what the stuff is used for, but it looks like a percentage thing (hence num1/num2 times 100). In that case, it's best to just use something like...

int val1 = System.Convert.ToInt32( ( (double)val2/val3 ) * 100);

Or just declare val2 and/or val3 as a double rather than casting it, but that may mess up other calcs that use val2 and/or val3.
 
Yeah I ended up casting everything to double then casting the final result to int. Is that a bad practice to do, like do doubles have smaller data range then ints, for whole numbers?
 
doubles by nature are imprecise, but I do believe they have enough precision to work just fine in your function.
 
What you should be doing really depends on your application. Fortunately this is 2008 and if you have calculations that need precision, you have classes like C#'s Decimal to abstract that.

Generally, though, you probably don't need that much precision. In that case, just use doubles and not worry about it. If you want to compare doubles, you will want to see if it is within some epsilon instead of equal.

So know your domain and know what final error is acceptable and use the appropriate data types. If you find yourself trying to make optimizations like the one in the OP, then (in most domains), you are probably doing something wrong.
 
Yeah basically I just need the precision to the point where I can get 100 values out of it.

With the first line of code I posted it would return either 0 or 100, but never anything in between. Using doubles solved my issue.
 
Back
Top