Pages: [1]   Go Down
Author Topic: pow() function issue  (Read 1155 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: 241
Posts: 24480
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: 72
Posts: 1642
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: 92
Posts: 3969
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

Why not visit my eBay shop? http://stores.ebay.co.uk/Majenko-Technologies
Replacement for the Arduino IDE: UECIDE - Proper serial terminal, graphing facilities, plugins, overhauled internals.
Java isn't bad in itself, but it has enabled morons to write programs.

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 241
Posts: 24480
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: 92
Posts: 3969
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

Why not visit my eBay shop? http://stores.ebay.co.uk/Majenko-Technologies
Replacement for the Arduino IDE: UECIDE - Proper serial terminal, graphing facilities, plugins, overhauled internals.
Java isn't bad in itself, but it has enabled morons to write programs.

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: 178
Posts: 12289
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: