Float with the value of 7 gives 6 as an int - ios

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);

Related

Int to format %.2f returns unexpected number on iPhone 5

I'm passing 0 as an argument to String(format: "%.2f"), it works on iPhone 5s, se, 6, 6s etc as expected ... However, it stopped working on iPhone 5, I guessed that it was a problem of 32 bit and 64 bit systems, because %f formats 64-bit floating-point number. Wrapped 0 with Double(0) and it worked, result was 0.00.
Can someone explain it in more details ?
String(format:) uses the same conversion specifications as
printf
(with some additions like %# for objects). In particular, the %f
conversion expects a Double on the argument list, and passing
anything else causes undefined behaviour: It may produce unexpected
output or crash.
On a 64-bit platform, passing 0 may work by chance because then
Int is a 64-bit integer and thus has the same size as a Double.
But even that is not guaranteed to work:
passing an integer argument instead of the expected floating
pointer number is still undefined behaviour.
You can use swift inbuilt method for a more consistent behavior
// Round the given value to a specified number
// of decimal places
func round(_ value: Double, toDecimalPlaces places: Int) -> Double {
let divisor = pow(10.0, Double(places))
return round(value * divisor) / divisor
}
Example:
round(52.3761, toDecimalPlaces: 3) // 52.376
round(52.3761, toDecimalPlaces: 2) // 52.38

Converting String to Float is not giving exact value in iOS

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);

Getting all decimals from interger being Squarooted

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);

Shortest way to get digit number from a value

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.

Why is converting my float to an int making the number negative?

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.

Resources