5

I want to calculate a logarithm in iOS. Can Objective-C do this?

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
Kuldeep Singh
  • 1,214
  • 4
  • 18
  • 45

3 Answers3

10

You can use the C functions for calculating logarithms

#import <math.h>
double myLog = log(10);

You can also see here: What kind of logarithm functions / methods are available in objective-c / cocoa-touch?

Community
  • 1
  • 1
Brock Woolf
  • 46,656
  • 50
  • 121
  • 144
1

We can use the same math functions which are available in C to do math operations in Objective-C.

I have used the following functions to do mathematical operations. Here the return type of these functions is double and argument should also be of double type... so

double number;

display.text = [NSString stringWithFormat:@"%f" ,log(number)];  
display.text = [NSString stringWithFormat:@"%f" ,log2(number)]; 

display.text = [NSString stringWithFormat:@"%f" ,log10(number)];
display.text = [NSString stringWithFormat:@"%f" ,exp(number)];
ram
  • 107
  • 1
  • 2
  • 11
0

Yes.

Objective C is a superset of ANSI C, and iOS supports using most of the usual C libraries.

Thus, you can use the C library math.h, which include several log functions (base10, base e, float result, double result, etc.)

hotpaw2
  • 70,107
  • 14
  • 90
  • 153