views:

167

answers:

3

Hi
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);
 }
A: 

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

  • Number =~ CGFloat

  • int =~ NSInteger

  • uint =~ NSUInteger

Chris Cooper
Is CGFloat used in Cocoa these days? I though that was mainly an iPhonism. Could be a brain fart, though.
Chuck
CGFloat has been in OS X since 10.5
David Gelhar
+1  A: 
  • 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).
David Gelhar
+1  A: 

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.

bbum
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.
JeremyP