Author Topic: Programming Megathread  (Read 144344 times)

why does everyone forget HLSL
So casual, doesn't even mention GLSL. You know, the language Blockland's shaders run on.

The standard Arduino IDE is awful for a lot of reasons. It exists as a first timer's tool to compiling and flashing hardware. It works but it is very limiting once you're ready to explore deeper, and customization of the environment is nonexistent.

I know there is some Eclipse plugins for Arduino if that's up your alley. I don't have much experience with Arduino development as an Arduino. I've used the boards before because they were available and easy, but I was flashing my own programs to them and not using the Arduino libraries, which once again have a lot of problems for people looking to go beyond the hobbyist level.

I mean I'm still only doing hobbyist stuff and fun little projects but I just really hate the Arduino IDE. I've never used eclipse that much except recently for a networking project in Java and I found eclipse to be pretty nice. I'll take a look at the eclipse one. I found an Arduino IDE plugin for Visual Studio which is what I have the most experience in but it seems like it's just cobbled together.

Code: [Select]
/*

A program that adds up the sum of fractions from 2 to 30 and compares it to 1, using nested loops.
Written by Silico Deoxy on October 31, 2015
Version: 1.0
Language: C (gcc target)

*/

#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<31;d++){

sum = 0;
itsum = 1;
while (itsum <= d){

sum = sum + (1 / d);
itsum++;

}
if (sum == 1)
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;

}


So we are doing nested loops in our class and I wrote the code above.

For some odd reason, I get some weird results for numbers that do not add up evenly into 1.

Compile it and look for yourself, it's the craziest stuff.

Yeah, precision of numbers is finite. If you replace your == with
Code: [Select]
if (sum - 1 < 0.000000000000001 && sum - 1 > -0.000000000000001)it gives the expected result.

Yeah, precision of numbers is finite. If you replace your == with
Code: [Select]
if (sum - 1 < 0.000000000000001 && sum - 1 > -0.000000000000001)it gives the expected result.

Heh, yeah, kinda wished our prof taught us that now.

Yeah, precision of numbers is finite.
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
« Last Edit: October 31, 2015, 11:22:19 PM by Headcrab Zombie »

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
« Last Edit: October 31, 2015, 11:40:26 PM by Foxscotch »

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

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



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
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)

talk("richard mondays");


System.out.println("you're all gay");
« Last Edit: November 01, 2015, 01:35:05 AM by Jairo »