Peijen's Programming Thread 1.0

Posts you want to find years later go here.
Peijen
Minion to the Exalted Pooh-Bah
Posts: 2790
Joined: Fri Jul 18, 2003 2:28 pm
Location: Irvine, CA

Peijen's Programming Thread 1.0

Post by Peijen »

Nothing to see here yet.

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

Post by Peijen »

Any one has any suggestion about code documentation format/tool? Something like javadoc for C++ would be nice. if it can be integrated into visual studio that would be good too.

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

damn you -1 !!!

Post by Peijen »

don't ever do this

Code: Select all

float rate[2];
float get_rate(bool smokes)
{
    return rate[smokes];
}

Jason
Veteran Doodler
Posts: 1520
Joined: Fri Jul 18, 2003 12:53 am
Location: Fairfax, VA

Post by Jason »

why? :D

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

Post by Peijen »

Jason wrote:why? :D
because true is -1 not 1, unless you don't mind that you are reading the stack in the wrong direction

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

Post by Peijen »

what happends if you call delete on a memory location twice?

Code: Select all

int *a, *b;

a = new int;
b = a;

delete a;
delete b;
Trying to figure out how to manage my memory and organize object creation/deletion

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

Post by Jonathan »

Peijen wrote:what happends if you call delete on a memory location twice?

Code: Select all

int *a, *b;

a = new int;
b = a;

delete a;
delete b;
Trying to figure out how to manage my memory and organize object creation/deletion
According to Stroustrup,
Exactly how arrays and individual objects are allocated is implementation-dependent. Therefore, different implementations will react differently to incorrect uses of the delete and delete[] operators. In simple and uninteresting cases like the previous one, a compiler can detect the problem, but generally something nasty will happen at run time.
On my system, which is Redhat 7 running gcc 3.2, it tends to segfault in non-trivial cases.l

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

Post by Peijen »

damnit, but I don't wnat to write my own memory manager ...
[/code]

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

Post by Jonathan »

Here's a question. What's the canonical way to test if a float or double is equal to zero?

Code: Select all

double a;
a = calculations();
// a = 3.34912e-317
if (???) {
  // a is 0.0
}
I'm doing some kind of math which results in values very very close to zero. I don't know what precisely those values will be, but I do know I want to treat them as zero. The answer isn't > -SMALL_FRACTION and < SMALL_FRACTION, is it?

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

Post by Peijen »

i think fabs might be a actual function

Code: Select all

float fabs(float f)
{
  return (f>0)?f:f*-1;
}

// stuff

if (fabs(a-b) < epsilon) {
//...
}
epsilon is your tolerable margin of error, so you are basically right ...

bob
Poser
Posts: 344
Joined: Fri Jul 18, 2003 1:26 am
Location: p-town, pa
Contact:

Post by bob »

Peijen wrote:what happends if you call delete on a memory location twice?
on my mac, it throws up on stderr, telling me that i've tried to delete an already freed memory region. but, at least in debug mode, it continues without crashing. until later, that is.

i think it also just bitches if i try to delete a NULL pointer.

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

Post by Jonathan »

Is this equivalent?

Code: Select all

bool twiddles(real a, real b,
	      real eps)		// default = 1.e-12
{
    if (a == b || 2*abs(a-b) <= eps*(abs(a)+abs(b)))
	return true;
    else
	return false;
}

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

Post by quantus »

you should use the ? : notation... it's cooler and makes it one line.

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

Post by Jonathan »

Noooo.... to be one line, you remove the damn if() {} and just return (expression).

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

Post by quantus »

Dwindlehop wrote:Is this equivalent?
http://www.adtmag.com/joop/crarticle.asp?ID=396

That's not quite good enough actually. I suggest using "the ANSI standard class numeric_limits<T>" as mentioned in the column. There's messy underflow problems you might need to handle otherwise and it's just not worth the hassle when someone's written it for you already.

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

Post by quantus »

Dwindlehop wrote:Noooo.... to be one line, you remove the damn if() {} and just return (expression).
yeah yeah, if you're so smart mr. smarty pants, why didn't you do that when you wrote it?!

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

Post by Jonathan »

I didn't write it. It's part of the library that I'm using, and I thought perhaps it would work. However, this is not the case. If you look at it, it has been designed to work only with reasonably large values of a and b, on the order of epsilon. For a and b smaller than epsilon, it detects this as a real change. It's checking to see not if two things are equal, but if they're equal to some order of magnitude. A and b smaller than that magnitude are not equal. Smart, but the opposite of what I need for my particular problem.

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

Post by quantus »

As I said, just use the ANSI standard class numeric_limits<T>

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

Post by Jonathan »

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.

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: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.

Post Reply