[M3devel] LONGINT problem?

Jay K jay.krell at cornell.edu
Fri Jul 15 00:17:27 CEST 2011


Can you isolate if the problem is the formating/printing, or if it is in the computation?
i.e.:

Use "unsafe" and print out a little hex dump of the integer/longint variables?

As well, what does LONGINT on a 64bit machine do? Eh..well, it might work, it might not.
Doesn't really matter. Let's focus on non-working 32bit machine with LONGINT.

Also, please confirm which is the right, i.e. by writing it in C.
Also, maybe just try to format as unsigned/hex using the safe interfaces?
Also, this will be good to add to our automated tests. Assuming it doesn't run too slowly.
Assuming it reproduces for anyone else (sorry, a bit rude of me).

Later, thank you,
 - Jay


Date: Thu, 14 Jul 2011 16:26:05 -0500
From: kcdurocher at gmail.com
To: m3devel at elegosoft.com
Subject: [M3devel] LONGINT problem?

I was writing a program to calculate "Pythagorean triples" recursively, and ran into a problem.  Here's the program:
MODULE PyTriples EXPORTS Main;
IMPORT IO, Fmt;

VAR tcnt, pcnt, max, i: LONGINT;
PROCEDURE NewTriangle(a, b, c: LONGINT; VAR tcount, pcount: LONGINT) =  VAR perim := a + b + c;        BEGIN    IF perim <= max THEN
      pcount := pcount + 1L;      tcount := tcount + max DIV perim;      NewTriangle(a-2L*b+2L*c, 2L*a-b+2L*c, 2L*a-2L*b+3L*c, tcount, pcount);      NewTriangle(a+2L*b+2L*c, 2L*a+b+2L*c, 2L*a+2L*b+3L*c, tcount, pcount);
      NewTriangle(2L*b+2L*c-a, b+2L*c-2L*a, 2L*b+3L*c-2L*a, tcount, pcount);    END;  END NewTriangle;
BEGINi := 100L;
REPEAT
  max := i;  tcnt := 0L;  pcnt := 0L;  NewTriangle(3L, 4L, 5L, tcnt, pcnt);  IO.Put(Fmt.LongInt(i) & ": " & Fmt.LongInt(tcnt) & " Triples, " &
    Fmt.LongInt(pcnt) & " Primitives\n");  i := i * 10L;UNTIL i = 10000000L;
END PyTriples.
This outputs:

100: 17 Triples, 7 Primitives1000: 325 Triples, 70 Primitives10000: 0858 Triples, 703 Primitives100000: 40701 Triples, 7024 Primitives1000000: 808950 Triples, 70229 Primitives

However, if I just use INTEGER on a 64 bit machine, I get the proper output:
100: 17 Triples, 7 Primitives1000: 325 Triples, 70 Primitives10000: 4858 Triples, 703 Primitives
100000: 64741 Triples, 7026 Primitives1000000: 808950 Triples, 70229 Primitives
Note how 10000 and 100000 are different. The code is literally exactly the same, only with LONGINT replaced by INTEGER.

 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://m3lists.elegosoft.com/pipermail/m3devel/attachments/20110714/460ba984/attachment-0002.html>


More information about the M3devel mailing list