Pages: [1]   Go Down
Author Topic: Want to find x^y and maintain sign, ie squaring negative numbers  (Read 361 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all,


I have a program that measures angles - positive and negative.  I'd like to raise them to a power but maintain their sign when I use them later.  To be clear, I need .5 degrees to be squared to .25... 2 becomes 4... but I also want -.5 to become -.25... -2 returns -4... etc.  We all know that a negative times a negative is a positive and naturally the built in "pow" function always returns positive when given a negative base value.  I have been trying all night to write a simple function to handle this for me:  By taking 2 floating inputs (a base and an exponent), checking if the base is < 0 and setting a int variable called "sign" to -1.  Later in the function (after the power has been found) the result of pow gets multiplied by sign to return it to the correct polarity.  I've tried a dozen different permutations of the code below but the result is always the same... positive result return correctly, anything that results in a negative result returns 0.00.  This includes manually multiplying "result" by -1 or removing the "if" code and initializing sign to -1.  I need a second opinion:

Code:
void setup() {
Serial.begin(115200); //Initialize Serial Port
}

void loop() {

  double newval = scale(2.0,2.0);
 
  Serial.println(newval);

  delay(200); 
}

double scale(float input, float factor)
  { 
  int sign = 1.0;

  if (input < 0.0){
  sign = -1.0;
  }

  double result = pow(input, factor);
 
  result = result * sign;

  return result;

}
Logged

Offline Offline
Edison Member
*
Karma: 43
Posts: 1557
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  double newval = scale(-2.0,2.0);
Prints -4.00 for me.

Pete
Logged

Where are the Nick Gammons of yesteryear?

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  Serial.println(newval);
So, you know how to print stuff. Like input, sign, and result. But, you don't. Why not?
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


at least this line is ambiguous
Code:
int sign = 1.0;

Try this variation
Code:
void setup()
{
  Serial.begin(115200);

  for (int i = -50; i <= 50; i++)
  {
    double newval = scale(i * 0.12345, 2.0);
    Serial.println(newval, 4);
  }
  Serial.println("\ndone...");
}

void loop()
{
}

double scale(float input, float factor)
{
  double value = pow(input, factor);
  if (input < 0.0) value = -value;  // only adjust calculated factor when input is negative.
  return value;
}
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Pages: [1]   Go Up
Jump to: