Search code examples
iosobjective-clogarithm

Logarithmic calculation in Objective-C


I am building a game using Sprite Kit and I want to gradually increase the difficulty (starting at 1.0) based on the time since starting the game.

Someone suggested that I should use a logarithmic calculation for this but I'm unsure how to implement this in Objective-C.

- (float)difficulty
{
  timeSinceStart = ???; // I don't what kind of object this should be to make it play nice w/ `log`
  return log(???);
}

Update #1

I know that I need to use the log method but I'm uncertain what values I need to pass to it.


Solution

  • Objective C is a superset of the C language, therefore you can use "math.h".

    The function that computes the natural logarithm from "math.h" is double log(double x);

    EDIT

    Since you want the difficulty to increase as a function of time, you would pass the time as the argument to log(double x). How you would use that to calculate and change the "difficulty" is an entirely different question.

    If you want to change the shape of the curve, either multiply the expression by a constant, as in 2*log(x) or multiply the parameter by a constant, as in log(2*x). You will have to look at the individual curves to see what will work best for your specific application.