pow() function issue

Can anyone explain to me why this scale function doesn't work with variable input? Is there a solution/workaround? Thanks!

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  delay(500);
  long val = random(2000);
  Serial.print(val);
  Serial.print("\t");
  Serial.println(scale(val));
}

long scale(long input) {
  return pow(2.0, input);
}

Doesn't work, or isn't accurate? It isn't like this hasn't come up before.

If you want integer powers of two, use shift operations, not floating point.

You are generating a random number between 1 and 2000 inclusive. You are then raising 2 to the power of it. Any value of input > 31 wont fit into the long type return variable.

This might work:

float scale(long input) {
  return pow(2.0, input);
}

edit: but again, anything that is input > 100 is wont fit into a float.

pow() takes two doubles and returns a double (floats).

For long values use powl().

2^32 is the biggest value you will be able to store in a long variable.

2^32 is the biggest value you will be able to store in a long variable.

231-1 is the largest value you can store in a long.

AWOL:

2^32 is the biggest value you will be able to store in a long variable.

231-1 is the largest value you can store in a long.

That’s what I meant.

I apologize, I did not make myself clear. The first argument in pow() doesn't matter. I just noticed that the scale() function worked fine when the number was static, but as soon as I made it variable, it didn't work.

So let me rephrase my question:

I am trying to create an exponential function. The input values I am using are from 1500 to 2000. This is the function I made:

uint16_t scale(uint16_t input) {
  return (1500 / pow((pow((4 / 3), (1 / 670))), 1080)) * pow((pow((4 / 3), (1 / 670))), input);
}

Obviously, this did not work because of the limitation of pow(). I am curious, how I can get this working?

The problem has nothing to do with pow.

uint16_t scale(uint16_t input) {
return (1500.0 / pow((pow((4.0 / 3.0), (1.0 / 670.0))), 1080.0)) * pow((pow((4.0 / 3.0), (1.0 / 670.0))), input);
}

facepalm Thank you.