I am trying to submit a float of two decimal length to my Game Center leaderboard, however the only format allowed to submit with is int64_t. I am using the default Apple report score method:
- (void)reportScore:(int64_t)score forCategory:(NSString *)category {
GKScore *scoreReporter = [[GKScore alloc] initWithCategory:category];
scoreReporter.value = score;
[scoreReporter reportScoreWithCompletionHandler: ^(NSError *error) {
[self callDelegateOnMainThread: #selector(scoreReported:) withArg: NULL error: error];
}];
}
I am trying to use this method to provide the score to the report score method:
- (IBAction)increaseScore {
self.currentScore = self.currentScore + 1;
currentScoreLabel.text = [NSString stringWithFormat: #"%lld", self.currentScore];
NSLog(#"%lld", self.currentScore);
}
Please help, I have been googling like crazy and cannot find the answer to this.
GameCenter only accepts int64_t
The only difference between values that appear like floats or decimal values and those that appear as integers is the position of the decimal mark, while in fact all of them are int64_t.
If your internal representation is a double and you configured game center to show 3 digits after the decimal mark you have to convert it to an integer by multiplying with 10^3 and casting to integer.
int64_t gameCenterScore = (int64_t)(doubleValue * 1000.0f)
You can only submit 64 bit integers as scores to a leaderboard. From the documentation:
To Game Center, a score is just a
64-bit integer value reported by your
application. You are free to decide
what a score means, and how your
application calculates it. When you
are ready to add the leaderboard to
your application, you configure
leaderboards on iTunes Connect to tell
Game Center how a score should be
formatted and displayed to the player.
Further, you provide localized strings
so that the scores can be displayed
correctly in different languages. A
key advantage of configuring
leaderboards in iTunes Connect is that
the Game Center application can show
your game’s scores without you having
to write any code.
That doc page should tell you about formatting your score. It sounds like in order to display float-like scores you will have to tinker with the format settings in iTunes Connect.
Update
Try this for increaseScore:
- (IBAction) increaseScore {
self.currentScore = self.currentScore + 5;
float score = (float)self.currentScore / 100.0f;
currentScoreLabel.text = [NSString stringWithFormat: #"%f", score];
NSLog(#"%lld", self.currentScore);
}
You can see the GKScore.h file.
#property(nonatomic, assign) int64_t value; // The score value as a 64bit integer.
So float value now is not available.
Related
I'm currently parsing NSString values to NSNumbers and then adding them into a NSMutableArray called operands in an object called "data" like so:
NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber * myNumber = [f numberFromString:*operandString];
[data.operands addObject:myNumber];
I then retrieve those numbers, perform some math on them, then update the array:
double x = [[data.operands objectAtIndex: i]doubleValue];
double y = [[data.operands objectAtIndex: i + 1]doubleValue];
double answer = x * y;
[data.operands replaceObjectAtIndex:(i) withObject:[NSNumber numberWithDouble:answer]];
When I get the answer, everything looks fine eg: ( 3.33 * 5 = 16.65)
BUT, when I look in the debugger I'm seeing some crazy values for x and answer, such as:
x = 3.3300000000000001
answer = 16.649999999999999
Why is this happening? Am I loosing some precision with parsing these back and fourth? Is it how I've used the NSNumberFormatter to parse the string?
The reason I'm in trouble with this is because I'm trying to ensure there's no double overflow errors so I'm using this simple test to check the integrity:
if (answer / y != x){
//THROW OVERFLOW ERROR
}
With the above crazy numbers this is always inconsistent. When I NSLog the answer it comes out fine:
NSLog (#"%g", [[data.operands objectAtIndex:i]doubleValue]]);
Same for
NSLog (#"%f", [[data.operands objectAtIndex:i]doubleValue]]);
You are not losing any precision that you need to worry about. Those are the correct values. There are only about 2^60 different double numbers, that finite set has to try to approximately cover the infinite 'number of numbers' in the range that doubles cover.
In other words, there are no exact answers in computer land and your
if (answer / y != x){
//THROW OVERFLOW ERROR
}
Will not work. Or it may work much of the time, but fail if you push it. Instead you need to acknowledge the limited precision (which is pretty high precision) of doubles:
//Don't waste time worrying like this...
if (fabs(answer / y - x) > 1e-12*fabs(answer)){
//Not correct or useful thing to check don't use this - i did not check
}
// let the math package handle it:
if (isnan(answer)){
// we gots problems
}
if (!isnormal(answer)){
// we gots some other problems
}
Also don't forget that 10^300 is a very large number, doubles work pretty well. To use 32 bit floats you need to pay much more attention to order of execution, etc.
NSLog is likely outputting with fewer decimals of precision, and rounds to the nearest thing, so the answers look better.
So I have this app that tracks your speed and altitude. I have a UILabel that shows the current speed you are at and the current altitude you are at. What I want to do is have a label that shows the max altitude and the max speed. So basically if you are running and your speed reaches 5 mph the max label will say 5 mph and then if you get to 6 mph the max label will change to 6 etc... Same thing for the altitude label. So how do I do this?
I'm assuming that you have either a float or an integer value for your currentSpeed/currentAltitude. Have another set of the same type, (I'll use int for my example) and name them maxSpeed/maxAltitude.
And add this code to the function which updates speed.
if(currentSpeed > maxSpeed){
maxSpeed = currentSpeed;
maxSpeedLabel.text = [NSString stringWithFormat: #"%d mph", maxSpeed];
}
Same goes with altitude.
EDIT: Given your statement: speedLabel.text = [NSString stringWithFormat:#"%.2f", [location speed]*2.236936284];
Here is the updated code:
float currentSpeed = [location speed] * 2.236936284;
if(currentSpeed - maxSpeed >= 0.01){
maxSpeed = currentSpeed;
maxSpeedLabel.text = [NSString stringWithFormat: #"%.2f mph", maxSpeed];
}
Also, now, currentSpeed and maxSpeed are both floats. Note that I have used a different type of comparison here. Since floats go into a great deal of precision (that we don't always need) I've restricted yours to 2 decimal places. So the text and maxSpeed will only update if it is greater by a factor of 0.01.
I'm adding a bunch of coordinates into quad tree and when I'm asking for the closest coordinate near my location, sometimes I've coordinate with 0 at the end, added automatically perhaps by the quad tree or I don't know how.
The problem is when I'm asking the double value in my core data using predicate it won't match because of the 0 digit addition to the number.
I thought about removing it when I've 0 but I'm sure there is a better way doing it.
For example:
Near location 31.123456, 34.123456, the nearest is 31.123444, 34.123450
when '34.123450' is actually 34.12345 in the database.
//Convert float to String
NSString *str_lat = #"34.123450";
NSString *trimmedString=[str_lat substringFromIndex:MAX((int)[str_lat length]-1, 0)];
if([trimmedString isEqualToString:#"0"])
{
str_lat = [str_lat substringToIndex:[str_lat length] - 1];
}
else
{
}
NSLog(#"%#",str_lat);
First: You should not store numbers as strings. 7.3 and 7.30 are the same values with simply different representations. You should not compare the representations, but the value.
Second: You should not compare floating-point numbers with == but their difference to a delta. In a calculation precision might get lost, rounding is applied and so on. The "mathematical" equal values might be physical different by a more or less small amount.
// remove the zeros from values (if you have them as floats)
NSString *valueFromTheDataBase = [NSString stringWithFormat:#"%g", 34.123450];
NSString *yourValue = [NSString stringWithFormat:#"%g", 34.12345];
if([yourValue isEqualToString:valueFromDataBase]) {
// they are equal
}
OR Make Them floats and compare them
// make them floats and compare them
CGFloat floatFromDB = [valueFromDB floatValue];
CGFloat yourFloat = [yourString floatValue];
if((floatFromDB - yourFloat) == 0) {
// they are equal
}
UPDATED as #Amin Negm says
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'm building an app that allows the user to perform some calculations except the calculations result in numbers with lots of decimal digits. It's fine for me to see that kind of precision but I want to let the users be able to choose how many significant digits they want shown. I'm creating a result string using a double and using the %g format shown here:
NSString *resultString = [[NSString alloc] initWithFormat:#"%.14g", result];
I have created a stepper that the users can interact with and storing the number they have chosen in another double. My question is, how can insert that double where the 14 is to change the number of significant digits? Or is this even possible? Please comment if you need clarification.
Any field width or precision in a format can be replaced by an * to indicate a dynamic value which is supplied by an int argument.
For example:
double d = 1.0/7;
for(int i = 4; i < 12; i++)
NSLog(#"%.*g", i, d);
Outputs:
0.1429
0.14286
0.142857
0.1428571
0.14285714
0.142857143
0.1428571429
0.14285714286