Page 1 of 4

Peijen's Programming Thread 1.0

Posted: Fri Jul 18, 2003 2:40 pm
by Peijen
Nothing to see here yet.

Posted: Fri Jul 18, 2003 2:46 pm
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.

damn you -1 !!!

Posted: Fri Jul 18, 2003 8:17 pm
by Peijen
don't ever do this

Code: Select all

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

Posted: Fri Jul 18, 2003 8:18 pm
by Jason
why? :D

Posted: Fri Jul 18, 2003 8:51 pm
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

Posted: Fri Aug 08, 2003 7:28 pm
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

Posted: Fri Aug 08, 2003 7:42 pm
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

Posted: Fri Aug 08, 2003 7:55 pm
by Peijen
damnit, but I don't wnat to write my own memory manager ...
[/code]

Posted: Fri Aug 08, 2003 8:12 pm
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?

Posted: Fri Aug 08, 2003 8:23 pm
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 ...

Posted: Sat Aug 09, 2003 12:02 am
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.

Posted: Sat Aug 09, 2003 12:58 am
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;
}

Posted: Sat Aug 09, 2003 1:56 am
by quantus
you should use the ? : notation... it's cooler and makes it one line.

Posted: Sat Aug 09, 2003 2:04 am
by Jonathan
Noooo.... to be one line, you remove the damn if() {} and just return (expression).

Posted: Sat Aug 09, 2003 2:16 am
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.

Posted: Sat Aug 09, 2003 2:19 am
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?!

Posted: Sat Aug 09, 2003 2:33 am
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.

Posted: Sat Aug 09, 2003 4:23 am
by quantus
As I said, just use the ANSI standard class numeric_limits<T>

Posted: Sat Aug 09, 2003 5:45 pm
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.

Posted: Sat Aug 09, 2003 5:50 pm
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.