OpenVMS Source Code Demos
BASIC_QUAD_MOD_BUG
//==============================================================================
// Title : BASIC_QUAD_MOD_BUG.C
// Author : Neil Rieck
// Created: 2012-08-31
// Purpose: Used to check BASIC_QUAD_MOD_BUG.BAS
// 1. where the modulus function produces incorrect values with quad
// data larger than 2^31 when compiled with HP Alpha BASIC 1.7-000)
// 2. A friend verified that the problem also exists on Itanium
//==============================================================================
#include <stdio> //
//
// note: long long is the industry standard way to declare a 64-bit integer
// (also known as a quad)
//
unsigned long long tmp0,
tmp1,
div1,
mod1,
pow1;
//------------------------------------------------------------------------------
// quad_power (no floats; no negatives)
//------------------------------------------------------------------------------
unsigned long long quad_power(long long base,long long power) {
long long junk;
junk = 1;
while (power>=1) {
junk = junk * base;
power = power - 1;
}
return(junk);
}
//------------------------------------------------------------------------------
// main
//------------------------------------------------------------------------------
int main() { //
printf("C Version\n"); //
// for (pow1=30;pow1<64;pow1++) { // sign changes at 64
for (pow1=30;pow1<63;pow1++) { // this seems okay
printf("====================\n"); //
printf("pow1: %lld\n",pow1); // format: percent-el-el-dee
tmp0 = quad_power(2,pow1); // raise 2 to the power of pow1
printf("tmp0: %lld\n",tmp0); //
tmp1 = tmp0 + 1; // add one to test modulus
printf("tmp1: %lld\n",tmp1); //
div1 = tmp1 / 2; // let's try divide
printf("div1: %lld\n",div1); //
mod1 = tmp1 % 2; // let's try modulus
printf("mod1: %lld\n",mod1); //
} //
return(1); // VMS-S
}