Author Topic: Yey for WTF in C!  (Read 3484 times)

0 Members and 1 Guest are viewing this topic.

Offline Relakuyae

  • Administrator
  • Tree
  • ******
  • Posts: 500
Yey for WTF in C!
« on: October 27, 2009, 07:09:30 AM »
Shit like this....

Easy
Write a function declaration which takes two doubles and returns an int.

Code: [Select]
int foo(double, double);
Harder
Write a function that takes a pointer to a function, which takes two doubles, and returns an int.

Code: [Select]
int foo((*)(double, double));
WTF
Write a function pointer that takes a pointer to a function, which takes an array of doubles, and returns an array of function pointers that return int.

Code: [Select]
int  (*(*foo)((*)(double*)))[];

Froggums

  • Guest
Re: Yey for WTF in C!
« Reply #1 on: October 27, 2009, 07:33:28 AM »
 i just felt something snap inside my head when i tried to read that crap.  i take it it's computer coding of some sort. i can barely power my computer up, much less code or program.  i'm so sorry for you  :(

Offline Raiyon

  • Sprout
  • **
  • Posts: 71
    • http://www.youtube.com/ryancstube
Re: Yey for WTF in C!
« Reply #2 on: October 27, 2009, 10:27:21 AM »
Since we're on the topic of C, I'll just throw this out there if anyone wants to debug this Taylor series function project for sin/cos from my intro to C/C++ class.
(*did a WTF trying to figure this out*)

Code: [Select]
//main.cpp

#include <iomanip>
#include <iostream>
#include <cmath>
using namespace std;
//-------------------
#include "interface.h"
const double threshold = 10e-3;
int main(){
while(true){
cout << "sin(x)/cos(x): ";
double x = 0.0;
cin >> x;

double answerSin = mySin(x, threshold);
double answerCos = myCos(x, threshold);

cout << "sin(" << x << ") = " << setprecision(6) << fixed << answerSin << endl;
cout << "cos(" << x << ") = " << setprecision(6) << fixed << answerCos << '\n' << endl;
}
return 0;
}

Code: [Select]
//functions.cpp

#include <cmath>
#include <iostream>
#include <iomanip>
#include "interface.h"
using namespace std;

/*
=======================
sin(x) = [(-1)^n / (2n + 1)!] * [x^(2n + 1)]

cos(x) = [(-1)^n / (2n)!] * [x^(2n)]

threshold = 10^(-3)
=======================
*/

__int64 myFact(int theFactArg)
{
int tmpVal = theFactArg;
__int64 result = 1;

while(tmpVal>1){
result *= tmpVal--;
}

return result;
}

double mySin(double theSinArg, double threshold)
{
double sumSin = 0.0;
int nSin = 0;
while(true){
double termSin1 = pow(-1.0, nSin) / myFact(2 * nSin + 1);

double termSin2 = pow(theSinArg, 2 * nSin + 1);

double termSin = termSin1 * termSin2;

sumSin += termSin;
if(sumSin < threshold) break;
nSin++;
}
return sumSin;
}

double myCos(double theCosArg, double threshold)
{
double sumCos = 0.0;
int nCos = 0;
while(true){
double termCos1 = pow(-1.0, nCos) / myFact(2 * nCos);

double termCos2 = pow(theCosArg, 2 * nCos);

double termCos = termCos1 * termCos2;

sumCos += termCos;
if(sumCos < threshold) break;
nCos++;
}
return sumCos;
}
Code: [Select]
//interface.h

double mySin(double theSinArg, double threshold);
double myCos(double theCosArg, double threshold);
Instead of getting sin(0.5) = 0.479425, cos(0.5) = 0.87758 and sin(2) = 0.90929 and cos(2) = -0.41614,
it's showing up sin(0.5) = -1.#INF00, cos(0.5) = -1.#INF00 and sin(2) = -2.616808 and cos(2) = -1.000000

(also, oh noes pointers.. they seem uber confusing, i'm really looking forward to that *sarcasm* /sob)
« Last Edit: October 27, 2009, 10:30:03 AM by ryanskim »

Offline Nagase

  • Young Tree
  • ****
  • Posts: 440
Re: Yey for WTF in C!
« Reply #3 on: October 27, 2009, 10:34:17 AM »
Programming is fun if you compare it to the majority of other subjects you need to study in school lul.

<3 Lights!

Offline -Rin-

  • Asgard Staff
  • Sapling
  • ***
  • Posts: 230
Re: Yey for WTF in C!
« Reply #4 on: October 27, 2009, 11:03:03 AM »
i agree.... compare it to math extension 2 here... omg.. thats like

Offline The Ceez

  • Sapling
  • ***
  • Posts: 187
Re: Yey for WTF in C!
« Reply #5 on: October 27, 2009, 12:10:02 PM »
This is why I changed my major from computer science

Offline Raiyon

  • Sprout
  • **
  • Posts: 71
    • http://www.youtube.com/ryancstube
Re: Yey for WTF in C!
« Reply #6 on: October 27, 2009, 08:30:31 PM »
Okay I figured it out,
if(sumCos < threshold)   break;
was supposed to be
if((abs)termCos < threshold)   break;

simple mistake = uber fail

Quote
Programming is fun if you compare it to the majority of other subjects you need to study in school lul.
Yeah, it is pretty fun, up until you get to the complicated stuffs.
« Last Edit: October 29, 2009, 08:19:05 PM by ryanskim »

Offline -Rin-

  • Asgard Staff
  • Sapling
  • ***
  • Posts: 230
Re: Yey for WTF in C!
« Reply #7 on: October 27, 2009, 08:58:41 PM »
O_O

Offline Xemnas

  • Sprout
  • **
  • Posts: 97
  • Prime Minister of the Shu Han Dynasty: Zhuge Liang
Re: Yey for WTF in C!
« Reply #8 on: October 28, 2009, 07:51:56 AM »
lol rin ya look confused

Offline -Rin-

  • Asgard Staff
  • Sapling
  • ***
  • Posts: 230
Re: Yey for WTF in C!
« Reply #9 on: October 28, 2009, 11:42:49 AM »
 :) pardon my ignorance

Offline Relakuyae

  • Administrator
  • Tree
  • ******
  • Posts: 500
Re: Yey for WTF in C!
« Reply #10 on: October 28, 2009, 12:07:51 PM »
I saw far worse in class during the midterm... it was hard. Take for instance, this:

Foo is a pointer to a function which takes an array of pointers to a function that takes two doubles and returns an array of function pointers which take an integer and return a pointer to an integer.

Code: [Select]
int *(*(*foo)((*[])(double, double))[])(int);
I don't even know if this is legal, and if it is, the people who thought it up should be shot... twice.

Offline -Rin-

  • Asgard Staff
  • Sapling
  • ***
  • Posts: 230
Re: Yey for WTF in C!
« Reply #11 on: October 28, 2009, 08:06:29 PM »
is learning coding ro really hard? O_O hard as i need long time to learn the basics? or u can just copy or imitate source codes?

Offline Relakuyae

  • Administrator
  • Tree
  • ******
  • Posts: 500
Re: Yey for WTF in C!
« Reply #12 on: October 28, 2009, 08:29:31 PM »
or, compared to C, RO is a cakewalk.

There's no REAL programming involved, just scripting. Vast difference.

People who write PHP aren't programmers, as much as they claim to be, they aren't.  PHP isn't a programming language, it's a scripting language.  People who program solely in something like that, ASP, ASP.NET, HTML, Javascript, Ruby, or other similar languages aren't programmers, they're all scripting languages, every last one.  Real programmers know C, and C++ (after learning C).

Offline Xemnas

  • Sprout
  • **
  • Posts: 97
  • Prime Minister of the Shu Han Dynasty: Zhuge Liang
Re: Yey for WTF in C!
« Reply #13 on: October 29, 2009, 03:54:50 PM »
You should put up some of those resources D: so maybe if i get somewhat savy i could help ya with asgard =D

Offline -Rin-

  • Asgard Staff
  • Sapling
  • ***
  • Posts: 230
Re: Yey for WTF in C!
« Reply #14 on: October 29, 2009, 08:25:38 PM »
i was thinking the same thing, i mean since u told us that, i am a bit more confident that i can help. Btw, got any links that can help me get started?

Offline Relakuyae

  • Administrator
  • Tree
  • ******
  • Posts: 500
Re: Yey for WTF in C!
« Reply #15 on: October 29, 2009, 10:07:50 PM »
Just Google eA scripting. That's all I did. :P

Offline -Rin-

  • Asgard Staff
  • Sapling
  • ***
  • Posts: 230
Re: Yey for WTF in C!
« Reply #16 on: October 30, 2009, 03:19:47 PM »
right!!

Offline Relakuyae

  • Administrator
  • Tree
  • ******
  • Posts: 500
Re: Yey for WTF in C!
« Reply #17 on: November 29, 2009, 12:44:27 PM »
Yey for more C/C++ pains in the ass... Functors!

Offline -Rin-

  • Asgard Staff
  • Sapling
  • ***
  • Posts: 230
Re: Yey for WTF in C!
« Reply #18 on: November 30, 2009, 10:12:18 AM »
C/C++ is fun to learn xD
takes like 20 lines to draw circles xD

Offline Relakuyae

  • Administrator
  • Tree
  • ******
  • Posts: 500
Re: Yey for WTF in C!
« Reply #19 on: November 30, 2009, 11:19:06 AM »
From homework of mine (slightly modified, may not work):

Code: [Select]
template<typename ObjT, typename ArgT>
class Foo {
  ObjT o;
  ArgT a;
public:
  Foo(const ObjT& _o, const ArgT& _a) : o(_o), a(_a) {}
  typename Obj::ReturnT operator()(typename Obj::ArgumentT2 b) {
    return o(a, b);
  }
};

template<typename ObjT, typename ArgT>
Foo<ObjT, ArgT> Bar(const ObjT& o, const ArgT& a) {
  return Foo<ObjT, ArgT>(o, a);
}

Offline Raiyon

  • Sprout
  • **
  • Posts: 71
    • http://www.youtube.com/ryancstube
Re: Yey for WTF in C!
« Reply #20 on: March 06, 2010, 11:41:19 AM »
i attached a simple calculator i made for fun after learning some about GUIs a while ago  ;)

[attachment deleted by admin]

Offline Relakuyae

  • Administrator
  • Tree
  • ******
  • Posts: 500
Re: Yey for WTF in C!
« Reply #21 on: July 31, 2011, 11:25:03 AM »
Sorry, I removed the exe attachment from this post during the forum upgrades.