P
Pelle Beckman
Hello,
This is OT, but since everybody here seems to have
a great knowledge of C++ I thought I'd ask.
I have this vector with n^2 elements (where n usually
is somewhere between 100 and 1000...)
My goal is to loop through these values (this is not
a real-time computation...) 5000 times or so, select a
random element from the vector and do some simple
operations on the most nearby neighbors - meaning on top,
under, left and right in the vector.
However there is this problem - when at the edge
of the vector, not all nearby neighbors can be computed
(since they are not in the vector).
This algorithm is based on a mathematical one which
states that if the are no nearby neighbors, then go
with 0 (zero) instead.
But, this stuff needs to be fast even though it's not
in real-time.
Here's what I've got so far:
S - the vector in question
S_size - total number of elements in S
for(int T = 0; T < T_repeat; T += 10) {
for(int l = 0; l < l_repeat; l += 10) {
for(int q = 0; q < q_repeat; q++) {
xrand = rand() % S_size + 1;
yrand = rand() % S_size + 1;
try {
U1 =(-S_values.at(xrand).at(yrand)) *
S_values.at(xrand).at(yrand);
U2 = S_values.at(xrand).at(yrand) *
S_values.at(xrand + 1).at(yrand);
} catch(...) {
cout << "borked" << endl;
}
/* Do some computing here */
dU = J * (float)(U2 - U1);
}
}
}
See the try/catch-stuff? Theres my problem.
Can I handle this is some nifty way by letting
the exception tell me what overflowed and at
what elements?
Its late at night here and I really should get to bed
- if there's something thats not clear here
I'd be pleased to answer.
Cheers.
-- Pelle
This is OT, but since everybody here seems to have
a great knowledge of C++ I thought I'd ask.
I have this vector with n^2 elements (where n usually
is somewhere between 100 and 1000...)
My goal is to loop through these values (this is not
a real-time computation...) 5000 times or so, select a
random element from the vector and do some simple
operations on the most nearby neighbors - meaning on top,
under, left and right in the vector.
However there is this problem - when at the edge
of the vector, not all nearby neighbors can be computed
(since they are not in the vector).
This algorithm is based on a mathematical one which
states that if the are no nearby neighbors, then go
with 0 (zero) instead.
But, this stuff needs to be fast even though it's not
in real-time.
Here's what I've got so far:
S - the vector in question
S_size - total number of elements in S
for(int T = 0; T < T_repeat; T += 10) {
for(int l = 0; l < l_repeat; l += 10) {
for(int q = 0; q < q_repeat; q++) {
xrand = rand() % S_size + 1;
yrand = rand() % S_size + 1;
try {
U1 =(-S_values.at(xrand).at(yrand)) *
S_values.at(xrand).at(yrand);
U2 = S_values.at(xrand).at(yrand) *
S_values.at(xrand + 1).at(yrand);
} catch(...) {
cout << "borked" << endl;
}
/* Do some computing here */
dU = J * (float)(U2 - U1);
}
}
}
See the try/catch-stuff? Theres my problem.
Can I handle this is some nifty way by letting
the exception tell me what overflowed and at
what elements?
Its late at night here and I really should get to bed
- if there's something thats not clear here
I'd be pleased to answer.
Cheers.
-- Pelle