0


I'm tring to convert the following 3 methods from java-actionscript to Objective C. Part of my confusion I think is not knowing what Number types, primitives I should be using. ie in actionscript you have only Number, int, and uint. These are the 3 functions I am trying to convert

 public function normalize(value:Number, minimum:Number, maximum:Number):Number
    {
        return (value - minimum) / (maximum - minimum);
    }

    public function interpolate(normValue:Number, minimum:Number, maximum:Number):Number
    {
        return minimum + (maximum - minimum) * normValue;
    }

    public function map(value:Number, min1:Number, max1:Number, min2:Number, max2:Number):Number
    {
        return interpolate( normalize(value, min1, max1), min2, max2);
    }

This is what I have so far

-(float) normalize:(float*)value 
 withMinimumValue:(float*)minimum 
 withMaximumValue:(float*)maximum
 {
 return (value - minimum) / (maximum - minimum);
 }

-(float) interpolate:(float*)normValue
  withMinimumValue:(float*)minimum 
  withMaximumValue:(float*)maximum
 {
return minimum + (maximum - minimum) * normValue;
 }

-(float) map:(float*)value 
 withMinimumValue1:(float*)min1 
 withMaximumValue1:(float*)max1 
 withMinimumValue2:(float*)min2 
 withMaximumValue2:(float*)max2
 {
return interpolate( normalize(value, min1, max1), min2, max2);
 }
1
  • Oh, and (float*) is nonsense in the above-- you'd just use (float) as your argument types... not a pointer to a float. Commented Jun 1, 2010 at 6:03

3 Answers 3

1
  • If you're looking for a primitive type (not an object) that can handle non-integer values, float is probably fine (or CGFloat, as suggested by Chris)

  • unless your functions need to modify their arguments, you want the arguments to be just float not float *.

  • you are mixing up Objective-C message passing and plain C function invocation syntax: your example declares them as instance methods, but you're calling them like a function (won't work).

To match your declarations, the invocation would look something like:

return [self interpolate:
           [self normalize:value withMinimumValue:min1 withMaximumValue:max1]
           withMinimumValue:min2
           withMaximumValue:max2];
  • Because your interpolate, normalize, and map methods do not rely on any variables from the current object instance, they might be better off as objective-C class methods (or even as plain C functions).
Sign up to request clarification or add additional context in comments.

Comments

1

Given that none of your methods require any other state to work, all are basic math kinda stuff, and all are pretty straightforward, I would skip Objective-C entirely and just go with straight C. Something like:

static inline float normalize(float val, float min, float max) {
    return (val - min) / (max - min);
}

Stick that in a header file somewhere in your project and be done with it. Do the same for interpolate and map.

Note: as others have mentioned, you might want to change the type to CGFloat, if needed.

1 Comment

I think using CGFloat is dependent on what these numbers are for. If they are coordinates or colour values, then fine - CGFloat. If they represent some data that is part of the model, it would be better to use float or double depending on the required precision.
0

Assuming you're using Apple's frameworks, these are the conversions you'll probably want to use:

  • Number =~ CGFloat

  • int =~ NSInteger

  • uint =~ NSUInteger

2 Comments

Is CGFloat used in Cocoa these days? I though that was mainly an iPhonism. Could be a brain fart, though.
CGFloat has been in OS X since 10.5

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.