Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: porting C code to Perl

by Monk::Thomas (Friar)
on Oct 23, 2017 at 16:48 UTC ( [id://1201909]=note: print w/replies, xml ) Need Help??


in reply to porting C code to Perl

Hello

I commented some parts of the code to make it clearer what the C code is trying to do.

#define N 100 int len = floor(10 * N/3) + 1; // define an array, in C the size must be specified during compile // time (just use @a and let the loop populate the array with <len> // elements) int A[len]; for(int i = 0; i < len; ++i) { A[i] = 2; } int nines = 0; int predigit = 0; for(int j = 1; j < N + 1; ++j) { // iterate over the reversed array, transform each value // and calculate q -> 'map {...} reverse @a' ? int q = 0; for(int i = len; i > 0; --i) { int x = 10 * A[i-1] + q*i; A[i-1] = x % (2*i - 1); q = x / (2*i - 1); } A[0] = q%10; q = q/10; if (9 == q) { ++nines; } else if (10 == q) { printf("%d", predigit + 1); // print '0'x$nines; for (int k = 0; k < nines; ++k) { printf("%d", 0); } predigit, nines = 0; } else { printf("%d", predigit); predigit = q; if (0 != nines) { // print '9'x$nines; for (int k = 0; k < nines; ++k) { printf("%d", 9); } nines = 0; } } } printf("%d", predigit);

Replies are listed 'Best First'.
Re^2: porting C code to Perl
by marioroy (Prior) on Oct 24, 2017 at 03:11 UTC

    Hi Monk::Thomas,

    The predigit value may set to 0 magically with some C compilers, I'm not sure. It's not the case on the Mac.

    predigit, nines = 0;

    I changed the line to this.

    predigit = 0, nines = 0;

    Basing on Discipulus's example, I too am interested and will try a plain Perl version. Then, will try parallelization via a number sequence for j. Thank you for the fun, fellow monks.

    Regards, Mario

      The predigit value may set to 0 magically with some C compilers

      Since this value is defined as a global variable (not inside a function) and is an integral data type (not part of an object/struct) it is safe to assume 'predigit' is initialized as zero by the compiler. I guess the author of the C code wasn't sure and tried to initialize both variables (just in case) ... and got it wrong. However it doesn't matter, because the '= 0' doesn't actually add anything.

      test #1 - define at global scope

      #include <stdio.h> int value1, value2 = 9; void main(void) { printf("%d %d\n", value1, value2); }
      prints '0 9', not 9 9

      test #2 - define in function

      #include <stdio.h> void func(void) { int value1, value2 = 9; printf("%d %d\n", value1, value2); } void main(void) { func(); }
      may print '996873600 9' or '-1944996480 9' or '-51174016 9'

        However it doesn't matter, because the '= 0' doesn't actually add anything.

        From testing, it seems to matter on the Mac platform including CentOS 7.3. I compared the output with this site. No warnings are emitted on CentOS 7.3.

        Without '= 0'

        predigit, nines = 0; -- Mac OS X, Apple LLVM version 7.3.0 (clang-703.0.31) demo.c:41:13: warning: expression result unused [-Wunused-value] predigit, nines = 0; ^~~~~~~~ 1 warning generated. 0314159265358979323846264338327954288419716939937510582097494459230781 +6406286208998628734825342117067 | + | 4 + 7 -- Linux, gcc version 4.8.5 20150623 0314159265358979323846264338327954288419716939937510582097494459230781 +6406286208998628734825342117067 | + | 4 + 7

        With '= 0'

        predigit = 0, nines = 0; 0314159265358979323846264338327950288419716939937510582097494459230781 +6406286208998628034825342117067 | + | 0 + 0
        it is safe to assume 'predigit' is initialized as zero by the compiler

        One should be careful with this assumption, I've seen several C compilers for microcontrollers that don't do this by default, in order to save on instruction memory (which can matter on tiny uC's).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1201909]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-25 11:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found