Ooops
November 28, 2020, 10:40:28 AM

Author Topic: [bmx] Leap years by xlsior [ 1+ years ago ]  (Read 498 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bmx] Leap years by xlsior [ 1+ years ago ]
« on: June 29, 2017, 12:28:42 AM »
Title : Leap years
Author : xlsior
Posted : 1+ years ago

Description : Pretty self-explanatory: Pass a year, and find out whether or not it's a Leap Year.

The date range in this example was chosen to show that it correctly identifies 1900 as being not a leap year. (The year 2000 on the other hand is one)


Code :
Code: BlitzMax
  1. '
  2. ' IsLeap - Returns TRUE for Leap Years, FALSE for non-Leap Years
  3. '
  4. ' By Marc van den Dikkenberg
  5. '
  6. Strict
  7.  
  8. For Local Year:Int=1890 To 1920
  9.         Print "Year "+year+" "+IsLeap(Year)
  10. Next
  11.  
  12. Function IsLeap(SomeDate:String)   ' Pass either just the year, or a "DD MMM YYYY" BlitzMax data string
  13.         Local Jaar:Int=Int(Right$(SomeDate,4))
  14.                 If ((Jaar Mod 4) = 0 And (Jaar Mod 100) <> 0) Or ((Jaar Mod 4) = 0 And (Jaar Mod 400) = 0) Then
  15.                 ' Leap Year     -- Any year divisible by 4, except the centuries unless they are multiples of 400
  16.                 Return True
  17.         Else
  18.                 ' Not a Leap Year
  19.                 Return False
  20.         End If
  21. End Function


Comments :


Yo! Wazzup?(Posted 1+ years ago)

 Converted to BlitzBasic, wasn't hard :DI'm getting the hang of Blitzmax so doing this is helpful for me.Had to take out some comments, sorry.I tested by typing in 2008 then 2007, works, I think...
Code: [Select]
;
; IsLeap - Returns True For Leap Years, False For non-Leap Years
;
; By Marc van den Dikkenberg
; Conversion to BlitzBasic by Mortifer Nex
;

year$=Input("Enter in a year: ")
If IsLeap(year$) Print "That year is a leap year" Else Print "That year is not a leap year."

Function IsLeap(SomeDate$)
Local Jaar%=Right$(SomeDate,4)
Return ((Jaar Mod 4) = 0 And (Jaar Mod 100) <> 0) Or ((Jaar Mod 4) = 0 And (Jaar Mod 400) = 0)
End Function
The following code gets the date using CurrentDate() and uses it...
Code: [Select]
If IsLeap(CurrentDate()) Print "This year is a leap year" Else Print "This year is not a leap year."

Function IsLeap(SomeDate$)
Local Jaar%=Right$(SomeDate,4)
Return ((Jaar Mod 4) = 0 And (Jaar Mod 100) <> 0) Or ((Jaar Mod 4) = 0 And (Jaar Mod 400) = 0)
End Function



_PJ_(Posted 1+ years ago)

 I would add a (* Jaar) to the Return values, to prevent failing by giving a True result for year '0'.


Floyd(Posted 1+ years ago)

 The year 0 is a leap year according to the current rules.A leap year is a year divible by 4. An exception is made for "century years", meaning the year is divisible by 100. In that case you divide by 100 and apply the rule to what remains. Thus 2000 is a leap year but 1900 is not, as is seen by considering 20 and 19.That makes 0 a leap year. Of course, you could argue that leap years don't really apply prior to 1600 because the Gregorian calendar was not yet in effect.


Warpy(Posted 1+ years ago)

 Many people make the claim that there was no year 0 - It went from 1BC straight to 1AD. These are the same people who were at home being boring on Millennium Eve, though...


Streaksy(Posted 1+ years ago)

 I was home bored on milennium eve like a sad bugger but i know there was a year 0 in gregorian calanders ;/


_PJ_(Posted 1+ years ago)

 Certainly the math is correct, and by those rules, yes, ywear 0 would be a leaop year, had it existed, but Warpy's right. There was no 'Year 0'.Though my reasons for omitting it were more aimed at error prevention in case of null values being passed in, rather than specific checks for a year '0'.There's another, possible issue with attempts to identify BC years with negative numbers, since these would be incorrect.For example, 1600 BC was NOT a leap year, but 1599 BC was.


Jimmy(Posted 1+ years ago)

 Nice trick, you could also shorten that without modifying it like so:If ((year Mod 4)=0) And ((year Mod 100)<>0) Or ((year Mod 400)=0) Then Print "Leapyear!"


_PJ_(Posted 1+ years ago)

 The prooblem is, after a while, making things too short makes them unreadable for when trying to understand what the code is doing.
Code: [Select]
If (Not((year Mod 4)*(year Mod 400)) * (year Mod 100)) Then Print "Leapyear!"

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal