math.h doubt

Hi,

I am trying to insert this equation in arduino ide, but arduino gives only 0.00 as output,

#include<math.h>

double c = -2.438;

double voltage;

void setup() {
  
  Serial.begin(9600);
  analogReference(EXTERNAL);//3.3v
  // put your setup code here, to run once:

}

void loop() {
  
  double a = analogRead(A0);
  
  Serial.println(a);
 double b = pow(a, c);
 
  Serial.println(b);

  
  
  // put your main code here, to run repeatedly:

}

output received in serial monitor is 0.00
0.00
0.00
0.00,

Not sure of what should be done, any help?

My actual equation is y = (38610000*(x^-2.438))

x is known value where y is unknown..

sharp ir for 5.5m.pdf (414 KB)

Not sure of what should be done, any help?

Why do you expect 0 to any power to result in anything other than 0?

Stop doing anonymous printing.

  Serial.print("a = ");
  Serial.println(a);

Takes minimal extra effort, and gives an order of magnitude more information.

Is "a" zero?

I am using sharp ir gp2y0a710k0f sensor which outputs analog values 0 - 1024 but practically values ranges from 200 - 1024 as far as I have seen, so "a" ranges from 200 - 1024

This is a long shot, but you might try printing more digits: Serial.println(a,8);

Does println() print scientific notation?

yeah, i tried with positive value its working and i tried with negative its not working, what might be the problem?

//code

#include<math.h>

double c = 2.438;

double voltage;

void setup() {

Serial.begin(9600);
analogReference(EXTERNAL);//3.3v
// put your setup code here, to run once:

}

void loop() {

double a = analogRead(A0);

Serial.println(a);
double b = pow(a, c);

Serial.println(b);

// put your main code here, to run repeatedly:

}
//code

Output comes for (a^2.438) if value of a is 500 serial monitor prints 3802672.032

Like I said, try printing more digits - it could be that println() does not switch to scientific notation for small numbers.

(You could also try the dtostre() function which will return scientific notation.)

You could also throw in your 3861000 scaling factor and see if that gives the results you expect (print out a, too!)

There is no double support on the Arduino, just float.

I have attached the serial monitor output in which if exponent value is positive and if exponent value is negative, I suppose Arduino IDE is not processing negative exponents in this way, may be there is some other way for it, may I know that?

Consider what values of a in this calculation - pow(a, -2.438)will give you anything other than 0.00, printed to two digits after the decimal. I calculate that a must be from 1 to 8, inclusive, to get anything other than 0.00. If values are from 200 to 1023, think about the magnitude of the result - 1 / (200^2) is 0.000025; that number printed to two decimal places is 0.00, and it will be bigger than 1 / (200^2.438). Numbers larger than 200 will yield even smaller results, and they'll print as 0.00, too.

If you multiply the result by 38610000, you'll get non-zero results for any value the ADC can provide.

If you're uncertain, print some test data:

#include<math.h>
double c = -2.438;

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

  for (uint16_t i = 0; i < 1024; i++)  {
    double a = (double)i;
    Serial.print("a: ");
    Serial.print(a);
    Serial.print("   b: ");
    double b = pow(a, c);
    Serial.print(b);
    Serial.print("   ");
    Serial.print(38610000*b);
    Serial.println();
  }

}

void loop() {}

MarkT:
There is no double support on the Arduino, just float.

double is redefined as float (32 bit)

using double iso float will use 64bit double if available

standard println() does not support scientific notation, however you can do println(a, 40); which will print even the smallest Arduino float. but you need a wide screen . :wink:

you can also check - Proposed update for the printFloat code of print.cpp - Libraries - Arduino Forum - for some patch of printing floats.