Daniel
Daniel

Reputation: 285

Formula from Excel to Objective-C

Problem

I've tried to port this formula directly from Excel. However, the end result is not the same. I'll provide my attempt as well as the right version of Excel. Can someone provide me with the information as to where I've gotten incorrect?

Excel

=EXP(E3*C7)*0.108*C4*C4/C5*C6/35

In the next formula

E3 = T
C7 = TF
C4 = S
C5 = St
C6 = F

Objective-C

double exposure = exp((T * TF) * 0.108 * S * S / St * FF / 35);

Even

double exposure = exp(T * TF) * 0.108 * S * S / St * FF / 35;

Returns wrong.

However, the end results aren't the same between them. I've also tried most of the types of exponentials that are offered.

For example, the excel returns 5.2 and objective c returns 5.65.

Upvotes: 1

Views: 64

Answers (2)

gtwebb
gtwebb

Reputation: 3011

Forward by saying I know absolutely nothing about objective c but using this compiler I ran this code which returned 5.248942. This is the same value I get in excel.

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    float T = 0.767717;
    float TF = 1.1;
    float S = 20;
    float St = 19.15;
    float FF = 35;
    double exposure = exp(T * TF) * 0.108 * S * S / St * FF / 35;
   NSLog(@"%f", exposure);
   [pool drain];
   return 0;
}

I probably should have used doubles all the way down but there is sufficient precision for these numbers.

Upvotes: 1

danh
danh

Reputation: 62676

Highly probable that you've declared one of those variables as a lower resolution type, for example, as a float who's assigned value is getting truncated.

When declared with all double inputs, Objective-C and Numbers agree (I can't try excel because I happily don't own a copy). Inventing values for those variables...

double T = 1.1;
double TF = 2.2;
double S = 3.3;
double St = 4.4;
double FF = 5.5;
double exposure = exp(T * TF) * 0.108 * S * S / St * FF / 35.;
NSLog(@"%f", exposure);
// logs 0.472374

Numbers, produces the same value from this formula:

enter image description here

As in... enter image description here

Upvotes: 1

Related Questions