What? Bool() problem..

Just starting out? Need help? Post your questions and find answers here.
User avatar
utopiomania
Addict
Addict
Posts: 1655
Joined: Tue May 10, 2005 10:00 pm
Location: Norway

What? Bool() problem..

Post by utopiomania »

Code: Select all

N(1) = N(1) + (Yr % 4 = 0) | (Yr % 100 = 0) | (Yr % 400 = 0)
X Line 8: Boolean comparisons can only be used with test operators
like If, While, Repeat. Use Bool() instead.

?? Worked in the past.. :)
User avatar
STARGÅTE
Addict
Addict
Posts: 2266
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: What? Bool() problem..

Post by STARGÅTE »

Code: Select all

N(1) = N(1) + Bool(Yr % 4 = 0) | Bool(Yr % 100 = 0) | Bool(Yr % 400 = 0)
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
utopiomania
Addict
Addict
Posts: 1655
Joined: Tue May 10, 2005 10:00 pm
Location: Norway

Re: What? Bool() problem..

Post by utopiomania »

Ok, thanks... :)
Little John
Addict
Addict
Posts: 4812
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: What? Bool() problem..

Post by Little John »

It looks as if this piece of code should calculate a leap day -- but it does not do so correctly.
For calculating a leap day, use e.g. the following code

Code: Select all

N(1) + Bool((year % 4 = 0 And year % 100 <> 0) Or year % 400 = 0)
In order to see the different results of both code snippets, try e.g. for year = 1900.
User avatar
utopiomania
Addict
Addict
Posts: 1655
Joined: Tue May 10, 2005 10:00 pm
Location: Norway

Re: What? Bool() problem..

Post by utopiomania »

The code calculated leap years correctly until newer versions of PB stopped calculating and throwed a syntax error...

The underlying logic in my program is and was correct. Stargåte just put things right by explaining to me how to use
Bool() to make this new >PB> version understand it :)
Little John
Addict
Addict
Posts: 4812
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: What? Bool() problem..

Post by Little John »

utopiomania wrote:Stargåte just put things right by explaining to me how to use
Bool() to make this new >PB> version understand it :)
Yes, that's good. And your fine code can be even more improved. The following snippet yields exactly the same results as Stargate's code:

Code: Select all

N(1) = N(1) + Bool(Yr % 4 = 0)
However, if you prefer you can also use e.g.

Code: Select all

N(1) = N(1) + Bool(Yr % 4 = 0) - 4 + 7 - 3
;-)
utopiomania wrote:The code calculated leap years correctly until newer versions of PB stopped calculating and throwed a syntax error...
I don't think that many people care whether you privately use some flawed code which just works by chance in particular cases.
But when you post wrong code in a public forum, it shouldn't be too much of a surprise when someone corrects it.
User avatar
utopiomania
Addict
Addict
Posts: 1655
Joined: Tue May 10, 2005 10:00 pm
Location: Norway

Re: What? Bool() problem..

Post by utopiomania »

I don't think that many people care whether you privately use some flawed code which just works by chance in particular cases.
But when you post wrong code in a public forum, it shouldn't be too much of a surprise when someone corrects it.
The algorithm and code is and was correct, and is not flawed, the problem had to do with syntax, not flawed logick or algorithms.

And, you are on my ignore list, so go away :) Bye.
Little John
Addict
Addict
Posts: 4812
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: What? Bool() problem..

Post by Little John »

utopiomania wrote:The algorithm and code is and was correct, and is not flawed [...]

And, you are on my ignore list, so go away :) Bye.
Obviously, logical thinking and sense of reality are also on your ignore list. :lol:
Boy, you made my day.
User avatar
Brandon Parker
User
User
Posts: 19
Joined: Fri Mar 30, 2012 2:30 am
Location: Scottsville, Virginia

Re: What? Bool() problem..

Post by Brandon Parker »

For cimplicity's sake, to test for a leap year (within PB's applicable Date range) you could just use the built in Date() function

Returns -1 if the given Date() parameters do not exist....

Code: Select all

Debug Date(2013, 02, 29, 12, 00, 00)
;Returns -1
or

Returns the date value expressed by the passed parameters

Code: Select all

Debug Date(2013, 02, 28, 12, 00, 00)
;Returns 1362009600
That's how I would do it anyway....let PureBasic decide when a leap year exists.

{:0)

Brandon
Last edited by Brandon Parker on Mon Feb 25, 2013 9:55 am, edited 1 time in total.
Windows 7 Home Premium 64-bit Intel(R) Quad Core(TM) i5 CPU M 430 @ 2.27GHz 4GB DDR3 RAM
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Re: What? Bool() problem..

Post by rsts »

For cimplicity's sake, to test for a leap year you could just use the built in Date() function
For dates within the limits of PB's date functions.
User avatar
Brandon Parker
User
User
Posts: 19
Joined: Fri Mar 30, 2012 2:30 am
Location: Scottsville, Virginia

Re: What? Bool() problem..

Post by Brandon Parker »

rsts wrote:
For cimplicity's sake, to test for a leap year you could just use the built in Date() function
For dates within the limits of PB's date functions.

I thought that was a given....... :wink:

{:0)

Brandon
Windows 7 Home Premium 64-bit Intel(R) Quad Core(TM) i5 CPU M 430 @ 2.27GHz 4GB DDR3 RAM
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Re: What? Bool() problem..

Post by rsts »

Brandon Parker wrote: I thought that was a given....... :wink:

{:0)

Brandon
It wasn't
in order to see the different results of both code snippets, try e.g. for year = 1900.
User avatar
Brandon Parker
User
User
Posts: 19
Joined: Fri Mar 30, 2012 2:30 am
Location: Scottsville, Virginia

Re: What? Bool() problem..

Post by Brandon Parker »

rsts wrote:
Brandon Parker wrote: I thought that was a given....... :wink:

{:0)

Brandon
It wasn't
in order to see the different results of both code snippets, try e.g. for year = 1900.

Yeah.....while that quote is about something completely different; I get it. I just assumed that someone using the Date() function would know that what I posted wouldn't work outside of PB's applicable date range. I know.... I know.... one should not assume that anyone ever reads the documentation before trying something.....

{:0)

Brandon
Windows 7 Home Premium 64-bit Intel(R) Quad Core(TM) i5 CPU M 430 @ 2.27GHz 4GB DDR3 RAM
User avatar
utopiomania
Addict
Addict
Posts: 1655
Joined: Tue May 10, 2005 10:00 pm
Location: Norway

Re: What? Bool() problem..

Post by utopiomania »

The code I posted calculates leap years correctly from 1562 to 3999.
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Re: What? Bool() problem..

Post by rsts »

As Little John noted, it fails in some circumstances. e.g. 1900, which it reports as a leap year, but was not.

Little John's code is correct.

cheers
Post Reply