The solution of the square root of 11 in a normal calculator is roughly
3.31662479036
I am trying round to 3.32
So my problem I am having in iOS is;
In my viewDidLoad
int Mynumber = sqrt(11);
NSLog(#"%d", Mynumber);
I keep getting 3 when I should to get the first three integers.
Can anyone help me solve this?
int is for integers, not decimals. Use float and %f.
float myNumber = sqrtf(11);
NSLog(#"%.2f", myNumber);
float theFloat = sqrt(11);
NSLog(#"%.2f", theFloat);
// also u can use integer round figur like this...
int rounded = lroundf(theFloat); NSLog(#"%d",rounded);
int roundedUp = ceil(theFloat); NSLog(#"%d",roundedUp);
int roundedDown = floor(theFloat); NSLog(#"%d",roundedDown);
Related
I wrote a sample to convert string to float in Objective-C:
NSString *sampleFloatString = #"1.3";
float sampleFloatValue = [sampleFloatString floatValue];
But when I display sampleFloatValue, it shows '1.29999995'. I know it's equal to 1.3, but why is it not exactly '1.3'? Why do we need to format it explicitly? Is there any other way of doing this conversion?
Its called "Floating point error". The way that computers represent decimal numbers causes them to not be 100% accurate all the time:
http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
Try this
float sampleFloatValue = (float) sampleFloatString;
Hope it helps.
if you just wants to show these value somewhere than u can do these and it shows 1.3 exact..
NSString *sampleFloatString = #"1.3";
float sampleFloatValue = [sampleFloatString floatValue];
NSLog(#"%.1f",sampleFloatValue);
Let's say I have a number like 134658 and I want the 3rd digit (hundreds place) which is "6".
What's the shortest length code to get it in Objective-C?
This is my current code:
int theNumber = 204398234;
int theDigitPlace = 3;//hundreds place
int theDigit = (int)floorf((float)((10)*((((float)theNumber)/(pow(10, theDigitPlace)))-(floorf(((float)theNumber)/(pow(10, theDigitPlace)))))));
//Returns "2"
There are probably better solutions, but this one is slightly shorter:
int theNumber = 204398234;
int theDigitPlace = 3;//hundreds place
int theDigit = (theNumber/(int)(pow(10, theDigitPlace - 1))) % 10;
In your case, it divides the number by 100 to get 2043982 and then "extracts"
the last decimal digit with the "remainder operator" %.
Remark: The solution assumes that the result of pow(10, theDigitPlace - 1) is
exact. This works because double has about 16 significant decimal digits and int on iOS
is a 32-bit number and has at most 10 decimal digits.
How about good old C?
int theNumber = 204398234;
char output[20]; //Create a string bigger than any number we might get.
sprintf(output, "%d", theNumber);
int theDigit = output[strlen(output)-4]-'0'; //index is zero-based.
That's really only 2 executable lines.
Yours is only 1 line, but that's a nasty, hard-to-understand expression you've got there, and uses very slow transcendental math.
Note: Fixed to take the 3rd digit from the right instead of the 3rd from the left. (Thanks #Maddy for catching my mistake)
Another solution that uses integer math, and a single line of code:
int theNumber = 204398234;
int result = (theNumber/100) % 10;
This is likely the fastest solution proposed yet.
It shifts the hundreds place down into the 1s place, then uses modulo arithmetic to get rid of everything but the lowest-order decimal digit.
NSTimeInterval expirationTime = (secondsSinceUnixEpoch*1000)+120000;
expirationTime = ceil(expirationTime/2);
int expirationInt = (int)expirationTime;
NSLog(#"%d", expirationInt);
The log output is always negative, even though before I convert it to an int it's positive... I tried just multiplying it by -1 to make it positive again and it's just staying negative! I'm totally perplexed.... don't know much about C, am I just doing something silly??
The number (secondsSinceUnixEpoch*1000)+120000 looks to me like it's going to be way too large to fit in an int. Chances are the integer is overflowing and becoming negative.
Converting to long long is one solution. As you stated in a comment, you want to show a whole number for use in a URL. Just do this:
NSTimeInterval expirationTime = (secondsSinceUnixEpoch*1000)+120000;
expirationTime = ceil(expirationTime/2);
NSString *urlString = [NSString stringWithFormat:#"http://example.com?time=%.0f", expirationTime];
This will format the decimal number as a whole number.
I have a problem with my float variable, i have to do some operation and then i have a final number what I saw is that some time the number is not correct, but just for a point for example when i have this number and i try to print it i don't give me back the same number:
float myNumber = 27589353.0f;
NSLog(#"My Number is %.2f", myNumber);
the result is: My Number is 27589352.00
I've tried to put the variable double but i have the same issue.
The problem is that floats do not have enough precision, as maddy said in the comments above.
And this code:
double myNumber = 27589353.0f;
NSLog(#"My Number is %.2f", myNumber);
Won't work either because the "f" qualifier on the constant forces the number to be a float, causes the loss of precision, then promotes the value to a double, once the damage is done.
This code however, will work correctly:
double myNumber = 27589353.0;
NSLog(#"My Number is %.2f", myNumber);
(Note that I'm assigning a floating point value with a decimal, but no final "f"
try like that it's work, you have to remove the 'f' as well
double myNumber = 27589353.0;
NSLog(#"My Number is %.2f", myNumber);
So for some reason I have a float named wday which has the value of 7.000000, but when I say
int wdaygiven = wday;
I get 6 and not 7. This also happens when I do this
NSNumber *num = [NSNumber numberWithFloat:wday];
Any ideas why? I really need 7 as the value given and not 6.
Thanks in advance
It's probably because the value isn't exact 7, but something like 6.999... (lots of 9) which appears to be 7. If converting to int, all decimals are cut and it results in 6.
Depending on your application you could round, take the next greater int, add 0.1 and convert to int a.s.o.
float value wday=7.00000 maybe less than 7.
Use this:
int wdaygiven = roundf(wday);