views:

76

answers:

2

There is a Table of pairs , which defines pieces bounds. And we are using straightforward algorithm:

y = f(x)

  1. Calculate index n in Table using x
  2. Get Yn and Yn+1, compute linear interpolation Y

Y is the answer.

So i think, there must be more efficient method, could you please point me?

A: 

Use binary search for step 1.

EDIT: due to the comment you added afterwards, this is not necessary, since your intervals are equally spaced.

Doc Brown
+1  A: 

Depending on the number and distribution of pairs, you might be able to instead store a table T containing only the Y values at regular intervals. Pick the interval to be a power of 2: i=2^c. Then for a given X:

n=X>>c;
Y= T[n]
Y+= ((T[n+1]-T[n])* (X&(i-1))>>c;

This should work as long as you have space for a table with small enough intervals to catch sudden changes in the slope of Y, and enough headroom in Y for the multiply.

AShelly