Peijen's Programming Thread 1.0

Posts you want to find years later go here.
quantus
Tenth Dan Procrastinator
Posts: 4891
Joined: Fri Jul 18, 2003 3:09 am
Location: San Jose, CA

Post by quantus »

Jason wrote:
Dwindlehop wrote:
quantus wrote:courses? what are those?
you don't have any left?
maybe that's why he keeps failing his PhD qualifier.
To keep failing requires that I fail multiple times which is not the case. Besides, I don't see you trying to take them.

Jonathan
Grand Pooh-Bah
Posts: 6722
Joined: Tue Sep 19, 2006 8:45 pm
Location: Portland, OR
Contact:

Post by Jonathan »

Peijen wrote:
Dwindlehop wrote:Fuck that noise. There is no testNumEq in numeric_limits. As far as I can tell, the code listing only exists in the print version of that article and isn't available online.
he did outline what you need to do. If you want to wait I will code it up next week, and I will send you a copy since I will need to use it too.

Or you can code it up and send me a copy.
Did you ever write anything like this?

Peijen
Minion to the Exalted Pooh-Bah
Posts: 2790
Joined: Fri Jul 18, 2003 2:28 pm
Location: Irvine, CA

Post by Peijen »

dont think so, I have been assign another project and they told me to put the first one on hold until I finish the new one.

quantus
Tenth Dan Procrastinator
Posts: 4891
Joined: Fri Jul 18, 2003 3:09 am
Location: San Jose, CA

Post by quantus »

heh, don't get too attached to this project either. Chances are they'll tell you to put this one on hold for something else. Iterate 'til you have infinite projects and nothing finished.

The only chance you have of finishing anything is if you get an intern :wink:
Have you clicked today? Check status, then: People, Jobs or Roads

Peijen
Minion to the Exalted Pooh-Bah
Posts: 2790
Joined: Fri Jul 18, 2003 2:28 pm
Location: Irvine, CA

Post by Peijen »

quantus wrote:heh, don't get too attached to this project either. Chances are they'll tell you to put this one on hold for something else. Iterate 'til you have infinite projects and nothing finished.

The only chance you have of finishing anything is if you get an intern :wink:
hehe, too bad it's not intern season.

Jonathan
Grand Pooh-Bah
Posts: 6722
Joined: Tue Sep 19, 2006 8:45 pm
Location: Portland, OR
Contact:

Post by Jonathan »

Argh. Trying to recompile code under libstdc++-v3 that was originally written with libstdc++-v2. What the hell are the non-deprecated io header files now? I can't fuckin' figure it out.

Jonathan
Grand Pooh-Bah
Posts: 6722
Joined: Tue Sep 19, 2006 8:45 pm
Location: Portland, OR
Contact:

Post by Jonathan »

http://annwm.lbl.gov/~leggett/Atlas/gcc-3.2.html

Going to try the steps detailed here.

Jonathan
Grand Pooh-Bah
Posts: 6722
Joined: Tue Sep 19, 2006 8:45 pm
Location: Portland, OR
Contact:

Post by Jonathan »

Well, that worked. Now I gotta figure out the differences between <fstream> and <fstream.h> because these deprecated messages are annoying the hell out of me.

Peijen
Minion to the Exalted Pooh-Bah
Posts: 2790
Joined: Fri Jul 18, 2003 2:28 pm
Location: Irvine, CA

Post by Peijen »

Dwindlehop wrote:Well, that worked. Now I gotta figure out the differences between <fstream> and <fstream.h> because these deprecated messages are annoying the hell out of me.
let me know when you do.

standardization is wonderful until it breaks your code!!

VLSmooth
Tenth Dan Procrastinator
Posts: 3055
Joined: Fri Jul 18, 2003 3:02 am
Location: Varies
Contact:

Post by VLSmooth »

Don't know if it should go in this thread, but why not.

You can pass functions as parameters in C++ !!!

example:
http://www.csm.astate.edu/~rossa/cs3543/fparm.html

I was thinking about using this for a timing wrapper (evaluates how long an arbitrary method takes). However, since it requires explicitly stating the method signature, and the methods I'm evaluating have different signatures, I'm SOL (shit out of luck).

Sigh, time for repetitive code I guess.

VLSmooth
Tenth Dan Procrastinator
Posts: 3055
Joined: Fri Jul 18, 2003 3:02 am
Location: Varies
Contact:

Post by VLSmooth »

Ok, I've just been told this isn't a big deal (and will shortly be on the way to borrow a Stroustrup book). Anyone know off the top of their head how to pass an arbitrary method and parameters to another method while I search?

Peijen
Minion to the Exalted Pooh-Bah
Posts: 2790
Joined: Fri Jul 18, 2003 2:28 pm
Location: Irvine, CA

Post by Peijen »

fn(arg1, arg2, ...)

or something like that, ... tells it there are more argument but we dont know how many now.

Peijen
Minion to the Exalted Pooh-Bah
Posts: 2790
Joined: Fri Jul 18, 2003 2:28 pm
Location: Irvine, CA

Post by Peijen »

ok, no "," before the "..." so

fn(arg1 ...) or fn(arg1, arg2 ...) etc

add number of argument as required

quantus
Tenth Dan Procrastinator
Posts: 4891
Joined: Fri Jul 18, 2003 3:09 am
Location: San Jose, CA

Post by quantus »

Isn't there an include statement too, like xargs.h or something?
Have you clicked today? Check status, then: People, Jobs or Roads

Jonathan
Grand Pooh-Bah
Posts: 6722
Joined: Tue Sep 19, 2006 8:45 pm
Location: Portland, OR
Contact:

Post by Jonathan »

Another method is to specify a void * as the argument to your function. Then, if you know how to decide what to cast the pointer to, you can pass any damn thing you want.

Peijen
Minion to the Exalted Pooh-Bah
Posts: 2790
Joined: Fri Jul 18, 2003 2:28 pm
Location: Irvine, CA

Post by Peijen »

quantus wrote:Isn't there an include statement too, like xargs.h or something?
<cstdarg>

but he only asked about how to do it, not how to use it 8)

VLSmooth
Tenth Dan Procrastinator
Posts: 3055
Joined: Fri Jul 18, 2003 3:02 am
Location: Varies
Contact:

Post by VLSmooth »

Dwindlehop wrote:Another method is to specify a void * as the argument to your function. Then, if you know how to decide what to cast the pointer to, you can pass any damn thing you want.
This shows promise, although an example would be even better 8)

VLSmooth
Tenth Dan Procrastinator
Posts: 3055
Joined: Fri Jul 18, 2003 3:02 am
Location: Varies
Contact:

Timing Fun

Post by VLSmooth »

Okay, I'm missing something basic here. For some reason, the duration is exactly 0, and I definitely notice the sleep delay.
Note1: sleeptime_variable() is to test passing a method with a different signature in the future.
Note2: not sure the CLOCKS_PER_SEC is set correctly

funct_passing.cpp:

Code: Select all

#include <stdio.h>


void sleeptime() {
  printf("  sleeptime(): start\n");
  sleep(5);
  printf("  sleeptime(): done!\n");
}

void sleeptime_variable(int i) {
  printf("  sleeptime_variable(%d): start\n");
  sleep(i);
  printf("  sleeptime_variable(%d): done!\n");
}

void timer2(void f()) {
  printf("\ntimer2(): start\n");
  clock_t start = clock();
  f();
  printf("timer2(): done! (%lf ms)\n",
	 (clock() - start)/(CLOCKS_PER_SEC / (double) 1000.0));
}

void timer(void f()) {
  printf("\ntimer(): start\n");
  clock_t start = clock();
  f();
  clock_t end = clock();
  printf("timer(): done! (%lf ms)\nNote: ", end-start);
  if (start == end) {
    printf("start == end\n");
  } else {
    printf("start != end\n");
  }
}

void main() {
  timer(sleeptime);
  timer2(sleeptime);
}
Here's the output:

Code: Select all

timer(): start
  sleeptime(): start
  sleeptime(): done!
timer(): done! (0.000000 ms)
Note: start == end

timer2(): start
  sleeptime(): start
  sleeptime(): done!
timer2(): done! (0.000000 ms)
Last edited by VLSmooth on Wed Oct 22, 2003 11:29 pm, edited 1 time in total.

VLSmooth
Tenth Dan Procrastinator
Posts: 3055
Joined: Fri Jul 18, 2003 3:02 am
Location: Varies
Contact:

Post by VLSmooth »

On the other hand, this works: (I wrote this earlier for fun). This explains why I'm confused about funct_passing.cpp above. Note that I use a very similar timing method above (just changed the signature), in addition to a new one that differs by a start,end comparison.

vector_speed.cpp:

Code: Select all

#include <stdio.h>



// this is a wrapper that takes in a method
// note: the method's signature must be completely specified
//       in the wrapper's signature
void timer(vector<int> v, void print_method(vector<int>)) {
  clock_t start = clock();
  print_method(v);
  printf("time: %lf ms\n",
	 (clock() - start)/(CLOCKS_PER_SEC / (double) 1000.0));
}

void print_vector_slow(vector<int> v) {
  for(int i=0;i<v.size();i++) {
    //    printf(" %d", v[i]);
  }
  printf("\ndone! (print_vector_slow)\n");
}

void print_vector_medium(vector<int> v) {
  vector<int>::iterator v_iter = v.begin();
  while(v_iter != v.end()) {
    //    printf(" %d", *(v_iter++));
    v_iter++;
  }
  printf("\ndone! (print_vector_medium)\n");
}

void print_vector_fast(vector<int> v) {
  vector<int>::iterator v_iter = v.begin();
  int * v_end = v.end();
  while(v_iter != v_end) {
    //    printf(" %d", *(v_iter++));
    v_iter++;
  }
  printf("\ndone! (print_vector_fast)\n");
}

void main() {
  vector<int> v_x;
  int i = 0; while (i<5000000) { v_x.push_back(i++); }
  timer(v_x,print_vector_slow);
  timer(v_x,print_vector_medium);
  timer(v_x,print_vector_fast);
}
Here's the output:

Code: Select all

done! (print_vector_slow)
time: 380.000000 ms

done! (print_vector_medium)
time: 150.000000 ms

done! (print_vector_fast)
time: 110.000000 ms

Jonathan
Grand Pooh-Bah
Posts: 6722
Joined: Tue Sep 19, 2006 8:45 pm
Location: Portland, OR
Contact:

Post by Jonathan »

Code: Select all

struct {
  int A;
  int B;
  double C;
} arg1_t;

struct {
  char X;
  char Y;
} arg2_t;

void fn1(void * args) {
   dostuff((arg1_t*)args);
}

void fn2(void * args) {
   dodifferentthings((arg2_t*)args);
}

void callmyfunction(void genericfn(void * args), void * thisargs, SomeType otherarg) {
  calculatestuff(otherarg);
  genericfn(thisargs);
}

int main() {
  arg1_t *penis = initialize();
  callmyfunction(fn1, (void *)penis, iamtirednow)
  return LOVE_FOR_VINNY;
}
You get the idea.

Post Reply