Off Topic > Off Topic

Programming Megathread

Pages: << < (23/241) > >>

Headcrab Zombie:


--- Quote from: ZSNO on October 31, 2015, 11:08:56 PM ---Yeah, precision of numbers is finite.

--- End quote ---
This, plus floating-point precision is binary.
So just like in decimal, where you can't divide by 3 very wear (10/3 = 3.33333333333), there's some divisions in binary that repeat infinitely, and it throws stuff off when divisions that work fine in decimal don't work in binary. So the numbers have to be approximations, and the inaccuracies between the approximation and the real value stacks up every time to do math to it

I don't know C enough but look if there's a decimal datatype, which handles it in decimal so it matches up

Foxscotch:

this is mostly personal preference, but I suggest [tt] over code tags
[tt] sets the font to a monospaced font, code doesn't. plus it's just prettier

#include <stdio.h>

int main(void) {
    int itsum;  /* Used to mark the iteration at which the sum loop is at */
    double sum, /* The sum of the fractions */
           d;   /* The denominator */

    for (d = 2; d <= 30; d++) {
        sum = 0;
        itsum = 1;

        while (itsum <= d) {
            sum = sum + (1 / d);
            itsum++;
        }

        if (sum - 1 < 0.000000000000001 && sum - 1 > -0.000000000000001)
            printf("Adding %lf 1/%lf's gives a result equal to 1\n", d, d);
        else if (sum < 1)
            printf("Adding %lf 1/%lf's gives a result less than 1\n", d, d);
        else
            printf("Adding %lf 1/%lf's gives a result greater than 1\n", d, d);
    }

    return 0;
}

changes (at least, the ones I remember), from top to bottom:
changed indentation in the first three lines of main(), so that the comments line up and d lines up with sum
in the for loop, uh, thing, the bit between the parentheses, I added some spaces. aaand changed "d < 31" to "d <= 30" because it's more clear. out of all of my own changes that's probably the only one that I think really matters
I added zsno's correction (probably never would've figured that out myself)
also I removed the spaces before the apostrophes in the strings you're printing. idk if you just didn't know that would work, but it wasn't very attractive the way that it was before. and even now it's still grammatically off but whatever
I think the last thing is removing the extra blank lines before the closing brackets

most of it was cosmetic. but I do think the for loop condition is genuinely better this way

in marginally related news



why does that happen when I press the close button on the console window that was running it, as opposed to just pressing "any key to continue"?
when I just press spacebar it ends normally, with 0, but when I use the exit button it doesn't? in either case, the program itself ends normally, which is shown in the console window



it's just... like, the console window itself that seems to cause the problem. if it matters, and I'm sure it does, I'm using code::blocks
it doesn't cause any problems or anything, it's just weird. it's always like that, not only with this particular code

Kochieboy:

Damn, my prof really didn't teach me jack stuff.

DJ Charizard:

Anybody mind posting some resources so people could interactively learn code? All I know/remember is codecademy.

ZSNO:


--- Quote from: Foxscotch on October 31, 2015, 11:36:58 PM ---

why does that happen when I press the close button on the console window that was running it, as opposed to just pressing "any key to continue"?
when I just press spacebar it ends normally, with 0, but when I use the exit button it doesn't? in either case, the program itself ends normally, which is shown in the console window



it's just... like, the console window itself that seems to cause the problem. if it matters, and I'm sure it does, I'm using code::blocks
it doesn't cause any problems or anything, it's just weird. it's always like that, not only with this particular code

--- End quote ---
It's because you're terminating the program before it has a chance to return 0, probably returning the last used value (I have no idea the value it returns)

Pages: << < (23/241) > >>

Go to full version