Evaluating arithmetic
youstra
Posts: 135
In declaring an array for a timeline, I do this in DEFINE_VARIABLE:
long alDailyRegTimeline[4] =
{
(1000 * 60 * 60 * 4), // 4 AM - Towel Warm On
(1000 * 60 * 60 * 9), // 9 AM - Towel Warm Off
(1000 * 60 * 60 * 15), // 3 PM - Towel Warm On
(1000 * 60 * 60 * 21) // 9 PM - Towel Warm Off
}
I get the wrong numbers in the resulting array: 47616, 25216, 63872, 36992
Same thing w/ or w/o paren around the multiplication.
If I just do:
long alDailyRegTimeline[4] = { 14400000, 32400000, 54000000, 75600000 }
then everything's fine.
Is there something I'm missing about having the compiler do the calc? I try to break it down for readability and easy changes down the road.
Thanks,
Bill
long alDailyRegTimeline[4] =
{
(1000 * 60 * 60 * 4), // 4 AM - Towel Warm On
(1000 * 60 * 60 * 9), // 9 AM - Towel Warm Off
(1000 * 60 * 60 * 15), // 3 PM - Towel Warm On
(1000 * 60 * 60 * 21) // 9 PM - Towel Warm Off
}
I get the wrong numbers in the resulting array: 47616, 25216, 63872, 36992
Same thing w/ or w/o paren around the multiplication.
If I just do:
long alDailyRegTimeline[4] = { 14400000, 32400000, 54000000, 75600000 }
then everything's fine.
Is there something I'm missing about having the compiler do the calc? I try to break it down for readability and easy changes down the road.
Thanks,
Bill
0
Comments
1000 * 60 % $10000 * 60 % $10000 * 4 % $10000
which equals 47616
Try the below statement (I haven't tested it on a master but it should work)
DEFINE_CONSTANT
LONG MSECS_IN_A_SECOND = 1000
LONG SECONDS_IN_A_MINUTE = 60
LONG MINUTES_IN_AN_HOUR = 60
LONG TOWEL_WARM_ON_AM = 4
LONG TOWEL_WARM_OFF_AM = 9
LONG TOWEL_WARM_ON_PM = 15
LONG TOWEL_WARM_OFF_PM = 21
DEFINE_VARIABLE
long alDailyRegTimeline[4] =
{
(MSECS_IN_A_SECOND
* SECONDS_IN_A_MINUTE
* MINUTES_IN_AN_HOUR
* TOWEL_WARM_ON_AM ), // 4 AM - Towel Warm On
(MSECS_IN_A_SECOND
* SECONDS_IN_A_MINUTE
* MINUTES_IN_AN_HOUR
* TOWEL_WARM_OFF_AM), // 9 AM - Towel Warm Off
(MSECS_IN_A_SECOND
* SECONDS_IN_A_MINUTE
* MINUTES_IN_AN_HOUR
* TOWEL_WARM_ON_PM ), // 3 PM - Towel Warm On
(MSECS_IN_A_SECOND
* SECONDS_IN_A_MINUTE
* MINUTES_IN_AN_HOUR
* TOWEL_WARM_OFF_PM) // 9 PM - Towel Warm Off
}
I know you can't typecast literals. I'm just surprised when I'm assigning into a long that the compiler assumes num literals are anything but long. When I do a direct assignment, it's ok...it's only if I have an arith expression that I guess it thinks the operands are not long. weird.
Guess I'll have to define another slew of constants...or maybe I'll just do the math and comment it for the maintenance guys to tweak down the road.
-Bill