OpenVMS Source Code Demos

BASIC_QUAD_MOD_BUG

1000	!=========================================================================
	! title  : BASIC_QUAD_MOD_PROBLEM.BAS (was: diffie_hellman_demo_100.bas)
	! author : Neil Rieck
	! history:
	! 100 NSR 120722 1. original effort
	! 101 NSR 120910 1. cleanup for more explicit testing
        !                2. testing with: Alpha BASIC V1.7-000 (original compiler)
	! 102 NSR 120914 1. testing with: Alpha BASIC V1.7-001 (test compiler)
	!=========================================================================
	! Sample Output (production compiler - Alpha BASIC V1.7-000):
	!
	!	====================
	!	pow1:  31
	!	tmp0:  2147483648           <-- 2^31
	!	tmp1:  2147483649           <-- tmp0 + 1
	!	div1:  1073741824           <-- tmp1 / 2
	!	mod1:  1                    <-- tmp1 % 2
	!	====================
	!	pow1:  32                   fails at 32
	!	tmp0:  4294967296
	!	tmp1:  4294967297
	!	div1:  2147483648
	!	-e-error: 51 at stage 4
	!	%NONAME-E-NOMSG, Message number 00000002
	!=========================================================================
	! Sample Output (experimental compiler - Alpha BASIC V1.7-001):
	!
	!	====================
	!	pow1:  61                   <-- 2^61
	!	tmp0:  2305843009213693952  <-- tmp0 + 1
	!	tmp1:  2305843009213693953  <-- tmp1 / 2
	!	div1:  1152921504606846976  <-- tmp1 % 2
	!	mod1:  1
	!	====================
	!	pow1:  62                   works to 62
	!	tmp0:  4611686018427387904  Problem fixed
	!	tmp1:  4611686018427387905  Problem fixed
	!	div1:  2305843009213693952  Problem fixed
	!	mod1:  1                    Problem fixed
	!	====================
	!	note: BASIC has no unsigned integers so 2^63 cannot be used
	!=======================================================================
	option	type = explicit			,	!			&
		size = integer	quad		,	!			&
		size = real	xfloat		,	!			&
		size = decimal	(31,0)                  !
	declare long	rc%			,	! return code		&
			stage%			,	! stage register	&
			error%				! error register
	declare quad	tmp0			,	!			&
			tmp1			,	!			&
			div1			,	!			&
			mod1			,	!			&
			pow1
2000	main:						!
	on error goto trap				! old school trapping
	print "Basic version"				!
	when error in					! new school trapping
	    for pow1 = 30 to 62				!
		print "===================="		!
		print "pow1: "; pow1	 		!
		!
		stage% = 1				!
		tmp0 = 2 ^ pow1				! raise 2 to the power of pow1
		print "tmp0: "; tmp0			!
		!
		stage% = 2				!
		tmp1 = tmp0 + 1				! add one to test modulus
		print "tmp1: "; tmp1			!
		!
		stage% = 3				!
		div1 = tmp1 / 2				! lets' try divide
		print "div1: "; div1			!
		!
		stage% = 4				!
		mod1 = mod(tmp1, 2)			! let's try modulus
		print "mod1: "; mod1			!
		!
	     next pow1					!
	     error% = 0					! cool
	use						!
	     error% = err				! oops
	end when					!
	!
	select error%					!
	    case 0					!
		goto fini				!
	    case else					!
		print "-e-error: "+str$(error%)+" at stage "+ str$(stage%)
		goto fini_error				!
	end select					!
	!
30000	trap:						!
	print "error: ";str$(err)			!
	print "line : ";str$(erl)			!
	print "text : ";ert$(err)			!
	resume fini_error				!
	!
	fini_error:					!
	rc% = 2						! VMS-E-
	goto fini_common				!
	!
32000	fini:						!
	rc% = 1						! VMS-S-
	!
	fini_common:					!
	end program rc%					!