Weird Math Error


g'day all,

i'm trying current time rtc (ie 21:15:33) , return long (ie 211533).

i thought simple maths should achieve this, alas, i'm stumped output.  value of 'lnewhour' should 210000 (21 * 10000), yet it's coming 13392. minute calculation seems working correctly (15*100=15000).

this dumb multiplication, must have missed something, can't see what.

any , assistance appreciated.

cheers....

code: [select]

unsigned long getclocktimeaslong()
{
  datetime dadate = rtc.now();
  int lhour=dadate.hour();
  int lminute=dadate.minute();
  int lsecond=dadate.second();
 
  serial.print("lhour=");
  serial.println(lhour);
 
  serial.print("lminute=");
  serial.println(lminute);

  serial.print("lsecond=");
  serial.println(lsecond);

  long lnewhour=lhour*10000;
  long lnewminute=lminute*100;
 
  serial.print("lnewhour=");
  serial.println(lnewhour);
 
  serial.print("lnewminute=");
  serial.println(lnewminute);

  serial.print("lsecond=");
  serial.println(lsecond);

  long ltime=lnewhour + lnewminute + lsecond; 
  //long ltime=(lhour*10000) + (lminute * 1000) + lsecond; 
  //long ltime=0;
  return ltime;
}



the output :
lhour=21
lminute=15
lsecond=48
lnewhour=13392
lnewminute=1500
lsecond=48

the compiler doing integer (16 bit) calculations overflowed giving strange results. can fix putting l suffix on constants, or replace int variables long.


Arduino Forum > Using Arduino > Programming Questions > Weird Math Error


arduino

Comments

Popular posts from this blog

Convierte tu Raspberry en un NAS. Firmware fvdw-sl 15.3 - Raspberry Pi Forums

How to format a Get Request

avrdude: verification error, first mismatch at byte 0x0000 0x0c != 0x62