I have found out why the code gave incorrect results on pre 1585 dates, just a pure simple typo.
The original code in the Pascal version of the MJD function was
THEN B:=-2+TRUNC((YEAR+4716)/4)-1179
The converted was
b=2+INTEGER((year%+4716)/4)-1179
I have underlined & made bold the difference, Just a simple "-" sign was missing & a very easy mistake to make thanks to := being used in Pascal. Here is the amended version & I have tested it on the dates you had problems with & they return the correct values back
LOCAL day%,month%,year%,hour%
DEBUG MJD(24,5,1500,12)+"\n"
CALDAT(-130923.5,day%,month%,year%,hour%)
DEBUG day%+" "+month%+" "+year%+" "+hour%
FUNCTION MJD:day%,month%,year%,hour
LOCAL a,b%
a=10000.0*year%+100.0*month%+day%
IF month%<=2
INC month%,12
DEC year%
ENDIF
IF a<=15821004.1
b=-2+INTEGER((year%+4716)/4)-1179
ELSE
b=INTEGER(year%/400)-INTEGER(year%/100)+INTEGER(year%/4)
ENDIF
a=365.0*year%-679004.0
RETURN a+b+INTEGER(30.6001*(month%+1))+day%+hour/24.0
ENDFUNCTION
FUNCTION CALDAT%:value,BYREF day%,BYREF month%,BYREF year%,BYREF hour%
LOCAL b%,d%,f%
LOCAL jd,jd0,c,e
jd=value+2400000.5
jd0=INTEGER(jd+0.5)
IF jd0<2299161.0
c=jd0+1524.0
ELSE
b%=INTEGER((jd0-1867216.25)/36524.25)
c=jd0+(b-INTEGER(b%/4))+1525.0
ENDIF
d%=INTEGER((c-122.1)/365.25); e=365.0*d+INTEGER(d%/4)
f%=INTEGER((c-e)/30.6001)
day%=INTEGER(c-e+0.5)-INTEGER(30.6001*f%); month%=f%-1-12*INTEGER(f%/14)
year%=d%-4715-INTEGER((7+month%)/10); hour%=24.0*(jd+0.5-jd0)
ENDFUNCTION
Lee