Home AMX User Forum NetLinx Studio
Options

Evaluating arithmetic

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

Comments

  • Options
    champchamp Posts: 261
    The numbers are being seen by the compiler as integers so it is rounding down the result. This is what the compiler is doing:

    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
    }
  • Options
    youstrayoustra Posts: 135
    Thanks, Champ for figuring this out.

    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
  • Options
    DHawthorneDHawthorne Posts: 4,584
    Yeah, this is an irritating issue for me as well, the way you can't force literals to a specific data type all the time. The format of the literal will often do this for you (like typing in 20.0 for 20 as a DOUBLE), but you can't use it in every situation (your case being the perfect example).
Sign In or Register to comment.