[M3devel] LONGINT problem?
Rodney M. Bates
rodney_bates at lcwb.coop
Mon Jul 18 17:42:16 CEST 2011
I just checked in a fix to Convert.m3 for this into the release branch.
It now gives the expected results for LONGINT. To rebuild, go into
the scripts subdirectory and type "./do-cm3-min.sh buildship".
This problem has already been fixed in the head in a different and more
efficient way.
On 07/14/2011 04:26 PM, Ken Durocher wrote:
> 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;
>
> BEGIN
> i := 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 Primitives
> 1000: 325 Triples, 70 Primitives
> 10000: 0858 Triples, 703 Primitives
> 100000: 40701 Triples, 7024 Primitives
> 1000000: 808950 Triples, 70229 Primitives
>
> However, if I just use INTEGER on a 64 bit machine, I get the proper output:
>
> 100: 17 Triples, 7 Primitives
> 1000: 325 Triples, 70 Primitives
> 10000: 4858 Triples, 703 Primitives
> 100000: 64741 Triples, 7026 Primitives
> 1000000: 808950 Triples, 70229 Primitives
>
> Note how 10000 and 100000 are different. The code is literally exactly the same, only with LONGINT replaced by INTEGER.
More information about the M3devel
mailing list