Pages: [1]   Go Down
Author Topic: pow() function issue  (Read 1397 times)
0 Members and 1 Guest are viewing this topic.
United States
Offline Offline
Full Member
***
Karma: 0
Posts: 104
Indubitably
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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);
}
Logged

fuzzball27 >>-->

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26517
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Leeds, UK
Offline Offline
Edison Member
*
Karma: 80
Posts: 1729
Once the magic blue smoke is released, it won't go back in!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
Code:
float scale(long input) {
  return pow(2.0, input);
}
edit: but again, anything that is input > 100 is wont fit into a float.
« Last Edit: July 21, 2012, 02:52:42 pm by TCWORLD » Logged

~Tom~

UK
Offline Offline
Faraday Member
**
Karma: 100
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26517
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

UK
Offline Offline
Faraday Member
**
Karma: 100
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

United States
Offline Offline
Full Member
***
Karma: 0
Posts: 104
Indubitably
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
Code:
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?
Logged

fuzzball27 >>-->

Global Moderator
Dallas
Online Online
Shannon Member
*****
Karma: 210
Posts: 13039
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


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);
}
Logged

United States
Offline Offline
Full Member
***
Karma: 0
Posts: 104
Indubitably
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

**facepalm**
Thank you.
Logged

fuzzball27 >>-->

Pages: [1]   Go Up
Jump to: