float division returns zero

:-[ Please point out the stupid mistake because im getting getting nowhere of looking for it

essentially im doing x = x + (y / z) but it stubbornly remains zero. All are floats except z which im casting as one heatEnergyPumped & elecEnergyUsedPumping seem to return values, so where am i going wrong?


//calculated info
float heatEnergyPumped = 0; //heat energy added to tank KWh
float elecEnergyUsedPumping = 0; //elec usage in pumping KWh
float totalPumpedHours = 0; //hours spent pumping

  void updateTotalsInfo() { //times are mS
  if (debug) {Serial.println("updateTotalsInfo() On: " + String(pumpOnTime) + ", Off: " + String(pumpOffTime));}
  newPumpedTime();  //record elapsed time in S 
  totalPumpedHours = totalPumpedHours + (nPT / (float)3600);  //PROBLEM WITH DATA TYPES?!
  if (debug) {Serial.println("****  new  totalPumpedHours: " + String(totalPumpedHours));}
  if (debug) {Serial.println("        nPT in Hrs: "+String(nPT / (float)3600));}
  heatEnergyPumped = (float)heatEnergyPumped + heatEnergyTransfered(nPT);
  elecEnergyUsedPumping = elecEnergyUsedPumping + (nPT * pumpWattage); //energy for pumping
  if (debug) {Serial.println(" ElecUsed:" + String(elecEnergyUsedPumping));}

//calc the elapse time for pumping, accounts for counter overflow of millis()
void newPumpedTime() {
  if (pumpOffTime >= pumpOnTime) {
    nPT = (pumpOffTime - pumpOnTime); //time pumped in S
  }else {
    nPT = ((pumpOnTime - longLimit) + pumpOffTime);
    if (debug) {Serial.println("millis() counter long limit exceeded");}
  if (debug) {Serial.println("****  NEWPUMPEDTIME()  nPT: " + String(nPT));}
  //if (debug) {Serial.println("        diff: "+String(pumpOffTime - pumpOnTime));  }
  //if (debug) {Serial.println("****   old totalPumpedHours: " + String(totalPumpedHours));}

You think you have a problem with data types but don't post enough to see the types? (eg. nPT).


Make a compilable sketch that demonstrates the problem, don't just post a snippet and claim it happens. For example, one of the numbers might be NaN.

I don't see the definition of nPT. What is the type of this variable? For your line:

  totalPumpedHours = totalPumpedHours + (nPT / (float)3600);  //PROBLEM WITH DATA TYPES?!

I will do:

  totalPumpedHours = totalPumpedHours + (nPT / 3600.0);  //PROBLEM WITH DATA TYPES?!

Why use the cast if you can directly add a float constant?

or if you dislike typing...

totalPumpedHours += nPT * 2.77777778e-4; // equals /3600.0 but way faster

my apologies for such a sloppy post Nick,

the missing var is

float nPT = 0; //elapsed pumping time S

some debug text is attached to show values
if I could explain why one calc works and the other dosnt id know where to start with a test script, unfortunately I dont and in need of a suggestion

floatdiv.txt (925 Bytes)

or if you dislike typing…

totalPumpedHours += nPT * 2.77777778e-4; // equals /3600.0 but way faster

dont mind the typing but I do like a neater way of doing it, Thanks

unfortunately the problem remains, whole script now attached

PumpTest_2.ino (17.1 KB)

Can the problem be only in the conversion function:


and not in the calc it self?

I think you solved it.

if(totalPumpedHours == 0){
    Serial.println(" totalPumpedHours ==0");
  }else if (totalPumpedHours <1){
    Serial.println(" totalPumpedHours <1"); 

gives totalPumpedHours <1
the value of totalPumpedHours should be about 0.00x, so why dosnt this value convert to a string? Surely any value should convert correctly, I must not understand something basic here
more ifs show the value to be 0.001 - 0.002

The "conversion" is made only with 2 decimal places. If your value was 0.005 it will show 0.01. If you want more than 2 decimal places you need to find another way to convert that value to string.

float totalPumpedHours = 0.0012345; //hours spent pumping

void setup ()
  Serial.begin (115200);
  Serial.println ();
  Serial.println (totalPumpedHours);
  Serial.println (totalPumpedHours, 7);
  }  // end of setup

void loop () { }



... it stubbornly remains zero ...

To the default number of decimal places, it might look like zero, yes.