All of your C variables are ints, so you need to change the two divisions in your program like so:

$q = int( $x / (2 * $i - 1) ); ... $q = int( $q / 10 );

And then the two programs produce the same output (Update: "03141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067"*, in case anyone was wondering). Alternatively, you can stick a use integer; at the top of the Perl file (of course this only applies when all the variables in the program are integers).

Also, although this doesn't seem to affect your program, note that there are differences between C's modulo % and Perl's % (use integer; also affects this).

* Update 2: The C compiler (gcc -std=c99 -Wall -Wextra -Wpedantic 1201848.c) complained about the line predigit, nines = 0;: "warning: left-hand operand of comma expression has no effect", so initially I adjusted it to your interpretation, predigit=0; nines=0;, but actually that causes a slight variation in the output (first one doesn't change predigit, second one does set it to zero):

0314159265358979323846264338327954288419716939937510582097494459230781 +6406286208998628734825342117067 0314159265358979323846264338327950288419716939937510582097494459230781 +6406286208998628034825342117067 here ^ + and here ^

In either case, making the equivalent change to the Perl program produces the same output. Since these are the digits of π, obviously the second one is the correct one. BTW, I guess you took the C code from here?

Update 3: Minor ninja edits to the above. I'm done for now :-)


In reply to Re: porting C code to Perl (updated!) by haukex
in thread porting C code to Perl by Discipulus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.