in reply to porting C code to Perl
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 :-)
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: porting C code to Perl -- solved
by Discipulus (Canon) on Oct 23, 2017 at 16:12 UTC | |
by haukex (Archbishop) on Oct 23, 2017 at 19:40 UTC | |
by Discipulus (Canon) on Oct 24, 2017 at 07:12 UTC | |
by haukex (Archbishop) on Oct 24, 2017 at 18:22 UTC | |
by marioroy (Prior) on Oct 24, 2017 at 04:30 UTC | |
by soonix (Chancellor) on Oct 23, 2017 at 18:41 UTC | |
by Discipulus (Canon) on Oct 23, 2017 at 19:59 UTC | |
by Monk::Thomas (Friar) on Oct 24, 2017 at 10:08 UTC |