Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C Programming

Name: Anonymous 2012-09-27 14:06

Hi /prog/,
I'm writing a small program in C to calculate the value of pi using the Montecarlo algorithm.
What can I do to improve it ?

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

long EVALPI(long);

#ifdef EBUG
#define DEBUG
#endif

/* README:
        you can add -DEBUG to your CFLAGS.
        Usage: ./montecarlo [number of loops] [rand seed]
        If the number of loops is not provided, it'll be asked on the command line.
        If the rand seed is not provided, it'll use the current timestamp using time() (3).
*/

int main(int argc, char **argv){
        long c, p;

        if(argc > 1){
                p = atoi(argv[1]);
        }else{
                printf("Nombre d'iterations: ");
                scanf("%ld", &p);
        }

        if(argc > 2)
                srand(atoi(argv[2]));
        else
                srand(time(NULL));

        c = EVALPI(p);
        printf("pi ~= 4*%ld/%ld ~= %f\n", c, p, (double)(c<<2)/p);
        return 0;
}

long EVALPI(long p){
        const long CENTRE = RAND_MAX >> 1;
        const long CENTRE_2 = CENTRE * CENTRE;
        long x, y, c = 0;
#ifdef DEBUG
        long old = p, foo = p/4;
#endif
        do{
                x = CENTRE - rand();
                x *= x;
                y = CENTRE - rand();
                y *= y;
                if(x+y < CENTRE_2) c++;
#ifdef DEBUG
                if(p % foo == 1) printf("%f\n", (double)(c<<2)/(old-p));
#endif
        }while(p--);

        return c;
}

Name: Anonymous 2012-09-27 17:47

Poe, E.
Near a Raven

Midnights so dreary, tired and weary.
    Silently pondering volumes extolling all by-now obsolete lore.
During my rather long nap - the weirdest tap!
    An ominous vibrating sound disturbing my chamber's antedoor.
        "This", I whispered quietly, "I ignore".

Perfectly, the intellect remembers: the ghostly fires, a glittering ember.
    Inflamed by lightning's outbursts, windows cast penumbras upon this floor.
Sorrowful, as one mistreated, unhappy thoughts I heeded:
    That inimitable lesson in elegance - Lenore -
        Is delighting, exciting...nevermore.

Ominously, curtains parted (my serenity outsmarted),
    And fear overcame my being - the fear of "forevermore".
Fearful foreboding abided, selfish sentiment confided,
    As I said, "Methinks mysterious traveler knocks afore.
        A man is visiting, of age threescore."

Taking little time, briskly addressing something: "Sir," (robustly)
    "Tell what source originates clamorous noise afore?
Disturbing sleep unkindly, is it you a-tapping, so slyly?
    Why, devil incarnate!--" Here completely unveiled I my antedoor--
        Just darkness, I ascertained - nothing more.

While surrounded by darkness then, I persevered to clearly comprehend.
    I perceived the weirdest dream...of everlasting "nevermores".
Quite, quite, quick nocturnal doubts fled - such relief! - as my intellect said,
    (Desiring, imagining still) that perchance the apparition was uttering a whispered "Lenore".
        This only, as evermore.

Silently, I reinforced, remaining anxious, quite scared, afraid,
    While intrusive tap did then come thrice - O, so stronger than sounded afore.
"Surely" (said silently) "it was the banging, clanging window lattice."
    Glancing out, I quaked, upset by horrors hereinbefore,
        Perceiving: a "nevermore".

Completely disturbed, I said, "Utter, please, what prevails ahead.
    Repose, relief, cessation, or but more dreary 'nevermores'?"
The bird intruded thence - O, irritation ever since! -
    Then sat on Pallas' pallid bust, watching me (I sat not, therefore),
        And stated "nevermores".

Bemused by raven's dissonance, my soul exclaimed, "I seek intelligence;
    Explain thy purpose, or soon cease intoning forlorn 'nevermores'!"
"Nevermores", winged corvus proclaimed - thusly was a raven named?
    Actually maintain a surname, upon Pluvious seashore?
        I heard an oppressive "nevermore".

My sentiments extremely pained, to perceive an utterance so plain,
    Most interested, mystified, a meaning I hoped for.
"Surely," said the raven's watcher, "separate discourse is wiser.
    Therefore, liberation I'll obtain, retreating heretofore -
        Eliminating all the 'nevermores' ".

Still, the detestable raven just remained, unmoving, on sculptured bust.
    Always saying "never" (by a red chamber's door).
A poor, tender heartache maven - a sorrowful bird - a raven!
    O, I wished thoroughly, forthwith, that he'd fly heretofore.
        Still sitting, he recited "nevermores".

The raven's dirge induced alarm - "nevermore" quite wearisome.
    I meditated: "Might its utterances summarize of a calamity before?"
O, a sadness was manifest - a sorrowful cry of unrest;
    "O," I thought sincerely, "it's a melancholy great - furthermore,
        Removing doubt, this explains 'nevermores' ".

Seizing just that moment to sit - closely, carefully, advancing beside it,
    Sinking down, intrigued, where velvet cushion lay afore.
A creature, midnight-black, watched there - it studied my soul, unawares.
    Wherefore, explanations my insight entreated for.
        Silently, I pondered the "nevermores".

"Disentangle, nefarious bird! Disengage - I am disturbed!"
    Intently its eye burned, raising the cry within my core.
"That delectable Lenore - whose velvet pillow this was, heretofore,
    Departed thence, unsettling my consciousness therefore.
        She's returning - that maiden - aye, nevermore."

Since, to me, that thought was madness, I renounced continuing sadness.
    Continuing on, I soundly, adamantly forswore:
"Wretch," (addressing blackbird only) "fly swiftly - emancipate me!"
    "Respite, respite, detestable raven - and discharge me, I implore!"
        A ghostly answer of: "nevermore".

" 'Tis a prophet? Wraith? Strange devil? Or the ultimate evil?"
    "Answer, tempter-sent creature!", I inquired, like before.
"Forlorn, though firmly undaunted, with 'nevermores' quite indoctrinated,
    Is everything depressing, generating great sorrow evermore?
        I am subdued!", I then swore.

In answer, the raven turned - relentless distress it spurned.
    "Comfort, surcease, quiet, silence!" - pleaded I for.
"Will my (abusive raven!) sorrows persist unabated?
    Nevermore Lenore respondeth?", adamantly I encored.
        The appeal was ignored.

"O, satanic inferno's denizen -- go!", I said boldly, standing then.
    "Take henceforth loathsome "nevermores" - O, to an ugly Plutonian shore!
Let nary one expression, O bird, remain still here, replacing mirth.
    Promptly leave and retreat!", I resolutely swore.
        Blackbird's riposte: "nevermore".

So he sitteth, observing always, perching ominously on these doorways.
    Squatting on the stony bust so untroubled, O therefore.
Suffering stark raven's conversings, so I am condemned, subserving,
    To a nightmare cursed, containing miseries galore.
        Thus henceforth, I'll rise (from a darkness, a grave) -- nevermore!

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List