Peijen's Programming Thread 1.0
-
- 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
Nothing to see here yet.
-
- Minion to the Exalted Pooh-Bah
- Posts: 2790
- Joined: Fri Jul 18, 2003 2:28 pm
- Location: Irvine, CA
damn you -1 !!!
don't ever do this
Code: Select all
float rate[2];
float get_rate(bool smokes)
{
return rate[smokes];
}
-
- Minion to the Exalted Pooh-Bah
- Posts: 2790
- Joined: Fri Jul 18, 2003 2:28 pm
- Location: Irvine, CA
what happends if you call delete on a memory location twice?
Trying to figure out how to manage my memory and organize object creation/deletion
Code: Select all
int *a, *b;
a = new int;
b = a;
delete a;
delete b;
-
- Grand Pooh-Bah
- Posts: 6722
- Joined: Tue Sep 19, 2006 8:45 pm
- Location: Portland, OR
- Contact:
According to Stroustrup,Peijen wrote:what happends if you call delete on a memory location twice?
Trying to figure out how to manage my memory and organize object creation/deletionCode: Select all
int *a, *b; a = new int; b = a; delete a; delete b;
On my system, which is Redhat 7 running gcc 3.2, it tends to segfault in non-trivial cases.lExactly 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.
-
- Grand Pooh-Bah
- Posts: 6722
- Joined: Tue Sep 19, 2006 8:45 pm
- Location: Portland, OR
- Contact:
Here's a question. What's the canonical way to test if a float or double is equal to zero?
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?
Code: Select all
double a;
a = calculations();
// a = 3.34912e-317
if (???) {
// a is 0.0
}
-
- Minion to the Exalted Pooh-Bah
- Posts: 2790
- Joined: Fri Jul 18, 2003 2:28 pm
- Location: Irvine, CA
i think fabs might be a actual function
epsilon is your tolerable margin of error, so you are basically right ...
Code: Select all
float fabs(float f)
{
return (f>0)?f:f*-1;
}
// stuff
if (fabs(a-b) < epsilon) {
//...
}
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.Peijen wrote:what happends if you call delete on a memory location twice?
i think it also just bitches if i try to delete a NULL pointer.
-
- Grand Pooh-Bah
- Posts: 6722
- Joined: Tue Sep 19, 2006 8:45 pm
- Location: Portland, OR
- Contact:
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;
}
-
- Tenth Dan Procrastinator
- Posts: 4891
- Joined: Fri Jul 18, 2003 3:09 am
- Location: San Jose, CA
http://www.adtmag.com/joop/crarticle.asp?ID=396Dwindlehop wrote:Is this equivalent?
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.
-
- Grand Pooh-Bah
- Posts: 6722
- Joined: Tue Sep 19, 2006 8:45 pm
- Location: Portland, OR
- Contact:
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.
-
- Minion to the Exalted Pooh-Bah
- Posts: 2790
- Joined: Fri Jul 18, 2003 2:28 pm
- Location: Irvine, CA
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.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.
Or you can code it up and send me a copy.