Path: utzoo!utgpu!water!watmath!uunet!labrea!agate!ucbvax!ihnp4!ihlpg!bgb From: bgb@ihlpg.ATT.COM (Beuning) Newsgroups: unix-pc.general Subject: 3b1 ulrem() bug Keywords: 3b1 "unsigned long remainder" ulrem bug Message-ID: <5552@ihlpg.ATT.COM> Date: 20 Jul 88 00:18:27 GMT Distribution: unix-pc Organization: AT&T Bell Laboratories - Naperville, Illinois Lines: 46 3b1 owners, I believe I have found a bug in unsigned long remainder arithmetic. If you compile and execute this program on your 3b1, the value of 'c' is incorrect. ---------------------------------------------------------------- main() { unsigned int a = 5 * 536870912; unsigned int b = 1073741823; unsigned int c, d; c = a % b; /* slow but sure method */ d = a; while( d >= b ) d -= b; printf( "a = %u b = %u c = %u d = %u\n", a, b, c, d ); exit( 0 ); } ---------------------------------------------------------------- The output on my 3b1 is (commas added for readability) a = 2,684,354,560 b = 1,073,741,823 c = 1,610,612,737 d = 536,870,914 The value of 'd' is correct, the value of 'c' is incorrect. An "unsigned int" should be able to contain all values from 0 to 4,294,967,295 (2 ^ 32 - 1). The line that reads "c = a % b" generates assembly code that calls the ulrem() function to compute the remainder. It looks like there is a bug in ulrem(). I have only seen the problem with very large numbers. Brian Beuning ..!ihnp4ihlpn!bgb