<div>Sorry, to clarify, the LONGINT code does NOT give the correct output, but the INTEGER code DOES.</div><div><br></div>I did not write this C code, but it's output is the same as all the other examples (there is a Java example too, if you want that):<div>
<br></div><div><div>#include <stdio.h></div><div>#include <stdlib.h></div><div>#include <stdint.h></div><div> </div><div>/* should be 64-bit integers if going over 1 billion */</div><div>typedef unsigned long xint;</div>
<div>#define FMT "%lu"</div><div> </div><div>xint total, prim, max_peri;</div><div>xint U[][9] =  {{ 1, -2, 2,  2, -1, 2,  2, -2, 3},</div><div><span class="Apple-tab-span" style="white-space:pre">         </span>{ 1,  2, 2,  2,  1, 2,  2,  2, 3},</div>
<div><span class="Apple-tab-span" style="white-space:pre">              </span>{-1,  2, 2, -2,  1, 2, -2,  2, 3}};</div><div> </div><div>void new_tri(xint in[])</div><div>{</div><div><span class="Apple-tab-span" style="white-space:pre">    </span>int i;</div>
<div><span class="Apple-tab-span" style="white-space:pre">      </span>xint t[3], p = in[0] + in[1] + in[2];</div><div> </div><div><span class="Apple-tab-span" style="white-space:pre">        </span>if (p > max_peri) return;</div>
<div> </div><div><span class="Apple-tab-span" style="white-space:pre">      </span>prim ++;</div><div> </div><div><span class="Apple-tab-span" style="white-space:pre">     </span>/* for every primitive triangle, its multiples would be right-angled too;</div>
<div><span class="Apple-tab-span" style="white-space:pre">      </span> * count them up to the max perimeter */</div><div><span class="Apple-tab-span" style="white-space:pre">     </span>total += max_peri / p;</div><div> </div><div>
<span class="Apple-tab-span" style="white-space:pre"> </span>/* recursively produce next tier by multiplying the matrices */</div><div><span class="Apple-tab-span" style="white-space:pre">      </span>for (i = 0; i < 3; i++) {</div>
<div><span class="Apple-tab-span" style="white-space:pre">              </span>t[0] = U[i][0] * in[0] + U[i][1] * in[1] + U[i][2] * in[2];</div><div><span class="Apple-tab-span" style="white-space:pre">          </span>t[1] = U[i][3] * in[0] + U[i][4] * in[1] + U[i][5] * in[2];</div>
<div><span class="Apple-tab-span" style="white-space:pre">              </span>t[2] = U[i][6] * in[0] + U[i][7] * in[1] + U[i][8] * in[2];</div><div><span class="Apple-tab-span" style="white-space:pre">          </span>new_tri(t);</div><div>
<span class="Apple-tab-span" style="white-space:pre"> </span>}</div><div>}</div><div> </div><div>int main()</div><div>{</div><div><span class="Apple-tab-span" style="white-space:pre">   </span>xint seed[3] = {3, 4, 5};</div>
<div> </div><div><span class="Apple-tab-span" style="white-space:pre">      </span>for (max_peri = 10; max_peri <= 100000000; max_peri *= 10) {</div><div><span class="Apple-tab-span" style="white-space:pre">              </span>total = prim = 0;</div>
<div><span class="Apple-tab-span" style="white-space:pre">              </span>new_tri(seed);</div><div> </div><div><span class="Apple-tab-span" style="white-space:pre">               </span>printf(<span class="Apple-tab-span" style="white-space:pre">     </span>"Up to "FMT": "FMT" triples, "FMT" primitives.\n",</div>
<div><span class="Apple-tab-span" style="white-space:pre">                      </span>max_peri, total, prim);</div><div><span class="Apple-tab-span" style="white-space:pre">      </span>}</div><div><span class="Apple-tab-span" style="white-space:pre">    </span>return 0;</div>
<div>}</div><div><br></div><div>The output from that code is:</div><div><br></div><div><div>Up to 10: 0 triples, 0 primitives.</div><div>Up to 100: 17 triples, 7 primitives.</div><div>Up to 1000: 325 triples, 70 primitives.</div>
<div>Up to 10000: 4858 triples, 703 primitives.</div><div>Up to 100000: 64741 triples, 7026 primitives.</div><div>Up to 1000000: 808950 triples, 70229 primitives.</div><div>Up to 10000000: 9706567 triples, 702309 primitives.</div>
<div>Up to 100000000: 113236940 triples, 7023027 primitives.</div></div><div><br></div><div>Note this output corresponds with the INTEGER output.</div><br><div class="gmail_quote">On Thu, Jul 14, 2011 at 5:17 PM, Jay K <span dir="ltr"><<a href="mailto:jay.krell@cornell.edu">jay.krell@cornell.edu</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">



<div><div dir="ltr">
Can you isolate if the problem is the formating/printing, or if it is in the computation?<br>i.e.:<br><br>Use "unsafe" and print out a little hex dump of the integer/longint variables?<br><br>As well, what does LONGINT on a 64bit machine do? Eh..well, it might work, it might not.<br>
Doesn't really matter. Let's focus on non-working 32bit machine with LONGINT.<br><br>Also, please confirm which is the right, i.e. by writing it in C.<br>Also, maybe just try to format as unsigned/hex using the safe interfaces?<br>
Also, this will be good to add to our automated tests. Assuming it doesn't run too slowly.<br>Assuming it reproduces for anyone else (sorry, a bit rude of me).<br><br>Later, thank you,<br> - Jay<br><br><br><div><hr>Date: Thu, 14 Jul 2011 16:26:05 -0500<br>
From: <a href="mailto:kcdurocher@gmail.com" target="_blank">kcdurocher@gmail.com</a><br>To: <a href="mailto:m3devel@elegosoft.com" target="_blank">m3devel@elegosoft.com</a><br>Subject: [M3devel] LONGINT problem?<div><div>
</div><div class="h5"><br><br>I was writing a program to calculate "Pythagorean triples" recursively, and ran into a problem.  Here's the program:<div><br></div><div><div>MODULE PyTriples EXPORTS Main;</div>
<div><br></div><div>IMPORT IO, Fmt;</div>
<div><br></div><div>VAR tcnt, pcnt, max, i: LONGINT;</div><div><br></div><div>PROCEDURE NewTriangle(a, b, c: LONGINT; VAR tcount, pcount: LONGINT) =</div><div>  VAR perim := a + b + c;      </div><div>  BEGIN</div><div>    IF perim <= max THEN</div>

<div>      pcount := pcount + 1L;</div><div>      tcount := tcount + max DIV perim;</div><div>      NewTriangle(a-2L*b+2L*c, 2L*a-b+2L*c, 2L*a-2L*b+3L*c, tcount, pcount);</div><div>      NewTriangle(a+2L*b+2L*c, 2L*a+b+2L*c, 2L*a+2L*b+3L*c, tcount, pcount);</div>

<div>      NewTriangle(2L*b+2L*c-a, b+2L*c-2L*a, 2L*b+3L*c-2L*a, tcount, pcount);</div><div>    END;</div><div>  END NewTriangle;</div><div><br></div><div>BEGIN</div><div>i := 100L;</div><div><br></div><div>REPEAT</div><div>

  max := i;</div><div>  tcnt := 0L;</div><div>  pcnt := 0L;</div><div>  NewTriangle(3L, 4L, 5L, tcnt, pcnt);</div><div>  IO.Put(Fmt.LongInt(i) & ": " & Fmt.LongInt(tcnt) & " Triples, " &</div>

<div>    Fmt.LongInt(pcnt) & " Primitives\n");</div><div>  i := i * 10L;</div><div>UNTIL i = 10000000L;</div><div><br></div><div>END PyTriples.</div></div><div><br></div><div>This outputs:</div><div><br></div>

<div><div>100: 17 Triples, 7 Primitives</div><div>1000: 325 Triples, 70 Primitives</div><div>10000: 0858 Triples, 703 Primitives</div><div>100000: 40701 Triples, 7024 Primitives</div><div>1000000: 808950 Triples, 70229 Primitives</div>

</div><div><br></div><div>However, if I just use INTEGER on a 64 bit machine, I get the proper output:</div><div><br></div><div><div>100: 17 Triples, 7 Primitives</div><div>1000: 325 Triples, 70 Primitives</div><div>10000: 4858 Triples, 703 Primitives</div>

<div>100000: 64741 Triples, 7026 Primitives</div><div>1000000: 808950 Triples, 70229 Primitives</div></div><div><br></div><div>Note how 10000 and 100000 are different. The code is literally exactly the same, only with LONGINT replaced by INTEGER.</div>

<div><br></div></div></div></div>                                     </div></div>
</blockquote></div><br></div>