Calculate download percentage in Objective C - objective-c

I got stuck with a problem that looked pretty easy but i cant make it work.
I'm making a simple download manager for OSX using objective C. As part of the app im trying to calculate the percentage of the current download. im trying to use this code but it wont work for me
long double precent = (bytesDownloaded/downloadSize) * 100;
NSLog(#"precnt %Lf",precent);
NSLog(#"Downloadedbyret: %lld", bytesDownloaded);
The bytesDownloaded and downloadSize are long long.
Can someone please advise,
thanks

To get the correct answer, you must cast the values to long double.
long double precent = ((long double)bytesDownloaded/(long double)downloadSize);
NSLog(#"precnt %Lf",precent);
NSLog(#"Downloadedbyret: %lld", bytesDownloaded);

Is the denominator an int, change it to a floating point? Try:
long double precent = (bytesDownloaded/(downloadSize * 1.0));
NSLog(#"precnt %Lf",precent);
NSLog(#"Downloadedbyret: %lld", bytesDownloaded);

To get % complete you need to multiply by 100.
long double precent = (bytesDownloaded/downloadSize) * 100.0;
if the values bytesDownloaded and downloadSize are int you will need to cast them to a floating point value.
Or if integers multiple the dividend by 100 first:
(bytesDownloaded * 100) / downloadSize;

Related

Double Over Precision

Apologies if this has been asked already - not sure what to search for.
Simple bit of code:
double x = 4505;
double y = 1000;
double z = 1000000;
double result = (x * y) / z;
Answer should be 4.505; but I get:
result = 4.5049999999999999
The values of x, y and z could be anything, and sometimes I need that level of precision in the result but I can't see why this is happening.
The Question is how to I remove the rounding error so that I can re-run further calculations on the decimal value without getting erroneous results and at the same time maintain high level of precision for numbers that need it.
It's simply a Floating Point Rounding Error. Also there is this.
If you want result rounded to 3 decimal places, then use:
result = floor(result * 1000.0) / 1000.0;
or just during presentation:
NSLog(#"result = %.3f", result);

ios issue with log calculation

I am working on a calculation for free space loss and hitting a snag.
Doing this calculation:
fslLoss = 36.6 + (20 * log(fromAntenna/5280)) + (20 * log(serviceFreq))
Where fslLoss is a float and fromAntenna and servicefreq are integers:
NSLog(#"the freespace Loss is %0.01f", fslLoss);
The result is "the freespace Loss is -inf"
The issue appears to be in the 20log(fromAntenna/5280) section, as I get normal results without it.
BTW ... tried log10 with the same results.
Thanks for the help,
padapa
You say fromAntenna is an integer, so fromAntenna/5280 will be calculated with integer arithmetic. That means it will be rounded (floored, technically), probably not what you intended.
Fix it with:
log( (double) fromAntenna / 5280.0 )
log(0) is -inf. The integer division inside the logarithm may be zero. Use fromAntenna/5280.0 to get float division.
The compiler is correctly using fromAntenna & serviceFreq as ints and that's not giving you good results when fslLoss is a float. Use some float casts and you'll have better luck:
fslLoss = 36.6 + (20 * log((float)fromAntenna/5280)) + (20 * log((float)serviceFreq));

Cannot add/subtract from large doubles in Objective C

I am trying to subtract 1 from a double with this code:
-(NSMutableArray *)getMoreTwitterDataWithMaxID:(double)maxID {
double newestID = (maxID - 1);
NSLog(#"newest ID is %f and maxID is %f", newestID, maxID);
// other code
}
The console is spitting this out:
2012-05-15 11:21:14.693 nearYou[2570:3a03] newest ID is 202429657738522624.000000 and maxID is 202429657738522624.000000
I'm not sure why they aren't subtracting . . .
Any help greatly appreciated!
May be you have reached the limit of the double data type. You can use NSDecimal instead.
You may have reached the limits of the double floating point format. The 64-bit floating point format only has a 52-bit mantissa, meaning it can only hold an integer of 52 bits with any integer accuracy before it uses the exponent. Your number is larger than that so the gap between one integer and the next possible one has grown bigger than 1.

Convert float representing hours to integer hours and minutes

I'm using the standard equation of distance / speed = arrival time. This works fine, but the answer is a float number and most people would find it awkward to convert something like 1.75 hrs to be 1 hr and 45 minutes.
I want to take that final float number result and extract the hour(s) separately from the minutes as integers.
Here is what I've tried:
-(IBAction)calculate:(id)sender {
float spd=[speed.text floatValue];
float dist=[distKnots.text floatValue];
//this give me the answer as a float
float arr=(dist/bs);
//this is how I showed it as an answer
//Here I need to convert "arr" and extract the hours & minutes as whole integers
arrivalTime.text=[NSString stringWithFormat:#"%0.02f", arr];
[speed resignFirstResponder];
}
And this is the conversion I tried to do -- and on paper it works, but in code it's full of errors:
int justHours = arr*60;
int justMinutes = (arr*60)-(justHours*60);
//then for the user friendly answer:
arrivalTime.text=[NSString stringWithFormat:#"%n hours and %n minutes", justHours, justMinutes];
I'm new to Objective-C and hoping there is a way to get this to work or a better way altogether to resolve this.
Your arr variable is already measured in hours, so you shouldn't be scaling it, just rounding it down:
int justHours = (int)arr;
and then your minutes is sixty times the (integer) difference between the original and rounded hours (i.e. the fractional part).
int justMinutes = (int)((arr - justHours) * 60);
The int justHours = arr/60; seems to be incorrect, it should be int justHours = arr;.
check the NSNumber numberFormater class. I believe you can wrap your float with time format and the return it to the user.

Syntax for rounding up in VB.NET

What is the syntax to round up a decimal leaving two digits after the decimal point?
Example: 2.566666 -> 2.57
If you want regular rounding, you can just use the Math.Round method. If you specifially want to round upwards, you use the Math.Ceiling method:
Dim d As Decimal = 2.566666
Dim r As Decimal = Math.Ceiling(d * 100D) / 100D
Here is how I do it:
Private Function RoundUp(value As Double, decimals As Integer) As Double
Return Math.Ceiling(value * (10 ^ decimals)) / (10 ^ decimals)
End Function
Math.Round is what you're looking for. If you're new to rounding in .NET - you should also look up the difference between AwayFromZero and ToEven rounding. The default of ToEven can sometime take people by surprise.
dim result = Math.Round(2.56666666, 2)
You can use System.Math, specifically Math.Round(), like this:
Math.Round(2.566666, 2)
Math.Round(), as suggested by others, is probably what you want. But the text of your question specifically asked how to "roundup"[sic]. If you always need to round up, regarless of actual value (ie: 2.561111 would still go to 2.57), you can do this:
Math.Ceiling(d * 100)/100D
The basic function for rounding up is Math.Ceiling(d), but the asker specifically wanted to round up after the second decimal place. This would be Math.Ceiling(d * 100) / 100. For example, it may multiply 46.5671 by 100 to get 4656.71, then rounds up to get 4657, then divides by 100 to shift the decimal back 2 places to get 46.57.
I used this way:
Math.Round(d + 0.49D, 2)
Math.Ceiling((14.512555) * 100) / 100
Dot net will give you 14.52. So, you can use above syntax to round the number up for 2 decimal numbers.
I do not understand why people are recommending the incorrect code below:
Dim r As Decimal = Math.Ceiling(d * 100D) / 100D
The correct code to round up should look like this:
Dim r As Double = Math.Ceiling(d)
Math.Ceiling works with data type Double (not Decimal).
The * 100D / 100D is incorrect will break your results for larger numbers.
Math.Ceiling documentation is found here: http://msdn.microsoft.com/en-us/library/zx4t0t48.aspx