9

I've tried searching for logarithm + objective-c, but all I get is math test pages from teachers, or explanations what a logarithm is ;)

I've got some measurements that are like 83912.41234 and others are 32.94232. I need to press down this huge spectrum into something between 0 and 100, and that 32.94232 would habe to be at least something bigger than 2, where the 83912.41234 would be something near 100. So I think a logarithm function will be my friend here.

UPDATE: I've came across the math.h file through "Open Quickly" (very nice command in Xcode: SHIFT + CMD + D), and there, big surprise:

extern double log ( double );
extern float logf ( float );

extern double log10 ( double );
extern float log10f ( float );

extern double log2 ( double );
extern float log2f ( float );

extern double log1p ( double );
extern float log1pf ( float );

extern double logb ( double );
extern float logbf ( float );

But: No text, no comments. I'm not such a math-freak. So some description would be good, i.e. what kind of logarithm for what case, how the curve looks like, etc... so any great links are highly appreciated!

Thanks
  • 40,109
  • 71
  • 208
  • 322
  • Are you looking for the results to be on a curve or a straight line? – Abizern May 21 '09 at 09:02
  • I have to try it out, but for the moment I guess it is a curve. log2() appears to be good, but it also seems that it is a very expensive calculation. – Thanks May 21 '09 at 09:24
  • it seems like performance goes down dramatically when I use log2(). But it's not 100% sure that it's log2()'s fault. Although it seems very much like it is, because when I don't use it and just make a x * 0.00412 + 2, the performance feels better. – Thanks May 21 '09 at 09:59
  • Measure, measure, measure. Ue Shark or Instruments to tell you what is really going on. – Mike Abdullah May 21 '09 at 10:25
  • aside, any version of log you use you'll get comparable results, since they are monotonic functions. – nlucaroni Oct 20 '09 at 19:23
  • @Thanks: Just wondering what does x*00412+2 as a replacement for log2(). Just tested it and math.log(61440, 2) == 15.906890595608518 and your alternative 61440 * 0.00412 + 2 == 255.13280000000003. How is it an alternative? – ruralcoder Aug 25 '13 at 00:56

3 Answers3

15

Also, getting a logarithm with an arbitrary base:

float logx(float value, float base) 
{
   return log10f(value) / log10f(base);
}
Adam Wiggins
  • 2,591
  • 1
  • 16
  • 6
2

math.h is a standard include. the wikipedia page has some documentation.

Another way to 'squish' the values is to fit your values to a straight line. For you example:

Straight line equation

y = mx + c
y = 'squished' value.
x = The value you want to squish
m = Gradient of the line
c = intercept on the y axis

A quick calculation on your values gives something like:

y = 1.17e-3 x + 1.96

Hope this helps.

Abizern
  • 146,289
  • 39
  • 203
  • 257
1

How about grabbing a book on C standard library functions?

Otherwise, you can try man pages: man 3 logf, for example

alamar
  • 18,729
  • 4
  • 64
  • 97