Modulo not acting as expected


i'm working on wwvb decoder, , 1 of operations i'm doing adjusting utc local time adding both offset , dst, depending on whether active (part of wwvb signal).  i happen -7 offset utc, , i'm getting odd results when utc hour less 7.  i thought use modulo keep result between 0 , 12,  but returning negative values.  this not how mod behave on calculator have.  suggestions?

i've pulled out mod operation simplify here:
code: [select]
//test modulo
int hours;
int hoursmod;
int dst;

void setup()
{
 serial.begin(9600);
 hours = 1;  //set initial sample value
 dst = -6;  //dst active
}

void loop()
{
 display_time();
 hours +=1;
 delay(1000);
}

void display_time()
{
 hoursmod = (hours + dst) % 12;  //add (dst + utc) offset, mod 12, keep result in 12 hour format
 
 serial.print(hours);
 serial.print(" ");
 serial.println(hoursmod);


results (utc hour, followed adjusted/mod hour):
1 -5
2 -4
3 -3
4 -2
5 -1
6 0
7 1
8 2
9 3
10 4
11 5
12 6
13 7
14 8
15 9
16 10
17 11
18 0

the remainder of (x / y) can negative if x or y negative.  some platforms define modulo operator in other ways, platform supports negative modulo.

using (hours + dst [glow]+ 24[/glow]) % 12 ensure you're not thrown off negative numbers dst.


Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > Modulo not acting as expected


arduino

Comments

Popular posts from this blog

CAN'T INSTALL MAMBELFISH 1.5 FROM DIRECTORY - Joomla! Forum - community, help and support

error: expected initializer before 'void'

CPU load monitoring using GPIO and leds - Raspberry Pi Forums