[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: ntpsec as server questions



On Fri, Dec 08, 2023 at 04:12:54PM +0000, Bonno Bloksma wrote:
> Hi Greg,
> > bool isleapyear (int year) {
> >     if (year % 400 == 0) return true;
> >     if (year % 100 == 0) return false;
> >     if (year % 4 == 0) return true;
> >     return false;
> > }
> 
> Except, I think it should be in reverse order because now 2000 first gets a true, then a false but then a true again and after that EVERYTHING gets a false ;-)

No, because "return" is immediate.  It's not "set the return value to this,
and then that'll be the thing the function returns when you reach the end".
It's "return this value RIGHT NOW and don't go any farther".

> So, in pseudo code
>   bool isleapyear (int year) {
>       return false;
>       if (year % 4 == 0) return true;
>       if (year % 100 == 0) return false;
>       if (year % 400 == 0) return true;
>   }

Your way should be:

bool isleapyear (int year) {
    bool ret = false;
    if (year % 4 == 0) ret = true;
    if (year % 100 == 0) ret = false;
    if (year % 400 == 0) ret = true;
    return ret;
}

Your way does three modulus operations every time, no matter what.  Mine
will do fewer than 3 if it gets lucky (input is a multiple of 100).


Reply to: