So - no matter what I do to narrow this down, I always get a "nan" returned from this function.
The Serial.print statements show that all the input variables are ok.
It's just after the long fltMoistureTemp = equation that I get the nan.
If I give fltMoistureTemp a static number 1, everything works fine.
What am I doing wrong?
//*******************************
double ComputeMoisture(double dblHumidityTemp, double dblTemperatureTemp) {
float fltMoistureTemp = 0.0;
// constants
float fltK = .000086541;
float fltN = 1.8634;
float fltC = 49.81;
Serial.println(fltK,10);
Serial.println(fltN,10);
Serial.println(fltC,10);
// input variables
float fltRH = (float)dblHumidityTemp;
float fltT = (float)((dblTemperatureTemp - 32) / 1.8); //Convert F to C degrees
Serial.println(fltRH,10);
Serial.println(dblTemperatureTemp);
Serial.println(fltT,10);
fltMoistureTemp = (0.01 * pow(((log(1 - fltRH)/(-1 * fltK * (fltT + fltC)))),(1 / fltN)));
Serial.println(fltMoistureTemp);
double dblMoistureTemp = (double)fltMoistureTemp;
//fltMoistureTemp = 1;
Serial.println(dblMoistureTemp);
return dblMoistureTemp;
}
It is becase you have posted code without using code tags. This creates certain problems and obstacles for other forum members. The code tags make the code look
like this
when posting source code files. It makes it easier to read, and can be copied with a single mouse click.
If you have already posted without using code tags, open your message and select "modify" from the pull down menu labelled, "More", at the lower left corner of the message. Highlight your code by selecting it (it turns blue), and then click on the "</>" icon at the upper left hand corner. Click on the "Save" button. Code tags can also be inserted manually in the forum text using the [code] and [/code] metatags.
When you are finished that, please read these two posts:
How to use this forum - please read. and
Read this before posting a programming question ...
Thanks aarg
(original post modified with code tags)
R
It's not necessary to post all your code because the problem is contained in a function. But you should post a self contained test sketch that calls the function with values that cause it to fail. It will actually be easier for us than sifting through your entire app.
Here is a simple sketch that uses real values and calls the function I am having issues with.
You may be wondering why all the "includes." The sketch I am working on is pretty large meaning that it has many .INO files and functionality for: LCD, RTC, Ethernet (w/web), SD, and DHT11. I left the includes in this test sketch just to show everything I'm using in the "real" sketch. I'm running this on a MEGA and compiling the "real" sketch shows that I am using about 57% of its dynamic memory.
When I run this test sketch, the compiler shows that I am using 13% of dynamic memory.
The Serial.print gives me three "nan"s.
//****INCLUDES******************************************
#include <Arduino.h>
#include <SPI.h>
#include <SD.h>
#include <dht.h>
//#include <TimeLib.h>
#include <Time.h>
#include <DS1307RTC.h>
//#include <RTClib.h>
#include <Ethernet.h>
//#include <Adafruit_LiquidCrystal.h>
#include <Wire.h>
#include <Adafruit_RGBLCDShield.h>
#include <utility/Adafruit_MCP23017.h>
float fltRH;
float fltT;
void setup() {
//****SERIAL COMMUNICATION SETUP******************************************
// Open serial communications and wait for port to open:
Serial.begin(9600);
//Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print(ComputeMoisture(50,70));
}
void loop() {
// nothing here
}
double ComputeMoisture(double dblHumidityTemp, double dblTemperatureTemp) {
float fltMoistureTemp = 0.0;
//constants
float fltK = .000086541;
float fltN = 1.8634;
float fltC = 49.81;
//Serial.println(fltK,10);
//Serial.println(fltN,10);
//Serial.println(fltC,10);
//inputs
fltRH = (float)dblHumidityTemp;
fltT = (float)((dblTemperatureTemp - 32) / 1.8); //Convert F to C degrees
//Serial.println(fltRH,10);
//Serial.println(dblTemperatureTemp);
//Serial.println(fltT,10);
fltMoistureTemp = (0.01 * pow(((log(1 - fltRH)/(-1 * fltK * (fltT + fltC)))),(1 / fltN)));
Serial.println(fltMoistureTemp, 10);
double dblMoistureTemp = (double)fltMoistureTemp;
//fltMoistureTemp = 1;
Serial.println(dblMoistureTemp);
return dblMoistureTemp;
}
The problem is that the formula, or your use of it, is wrong. You can't take the log of a negative number (fltRH = 50 in the example).
log(1 - fltRH)
On Arduino, "double" is the same as "float" and both store only 6-7 meaningful decimal digits.
@aarg:
It's not necessary to post all your code because the problem is contained in a function.
The problem was contained in the arguments supplied by the calling program.
THANKS aarg and jremington
The fltRH in my formula was supposed to be a Relative Humidity value (ie. %). Having that in my original formula as a >1 value was the problem.
Good catch!
R