meganursula: (cthullu)
[personal profile] meganursula
Question for object oriented gurus:

I am currently reviewing some code implementing a current standard of an algorithm i frequently use. (I want to examine some modifications to the algorithm, but, i need a good baseline to compare to.) In it we see something like:

struct velocity
{
int size;
double v[D_max]
};


There are a lot of these structs - position, quantum, etc.

Thing is, in my code, i generally declare
num_dim = n; // this is what they are using size for up above
double position[num_dim];
double velocity[num_dim];


(quantum, for the record, appears to be taking the place of what i usually declare as a constant Eps, and is used to get around numerical issues when looking for zero.)

etc. I do not have additional structs. Thing is, i find all this structifying to be sort of pointless and irritating. Pointless because i do not know what the structs are adding to the code. Irritating because i think they add a level of obfuscation, rendering the code not only longer, but also much less readable.

My question - what, if anything, am i missing in this situation? I get, generally, what object oriented-ness does for you. But i haven't used it very much in the past 6 or so years. (Matlab's excuse for object oriented isn't worth bothering with.) Right now i find myself faced with a few examples of modern code that are object oriented up the ass, and it just seems like it all has been taken too far. If i give myself three months will i become a believer? Will i stop feeling like there should be some sort of natural progression through code and adapt to having objects interacting at will?

Date: 2008-09-11 09:11 pm (UTC)
From: [identity profile] bhudson.livejournal.com
Oh crap, I forgot it's poorly written (breaks C aliasing rules, and is only for 32-bit floats). You can use my edit (http://idefix.uchicago.edu/~bhudson/src/float-compare.h), which works. The functions are float_similar(float,float,int) and float_approx_leq, or change float to double. The int parameter is the number of representable values you want to be allow to be fitted in between. If you think you're accurate to within 10 bits, you pass 1<<10 as that parameter.

By "wrong" I mean usually, just saying that two values less than 1e-6 from each other are equal is nonsense. Sometimes, it's sensical, if you know what the units really are. But that's rare in my experience; usually we just make up 1e-6 and move it down to 1e-8 when that causes problems, and maybe that will cause problems too because now two numbers in the billions will test not-equal when they should be equal, etc.

Date: 2008-09-11 09:26 pm (UTC)
From: [identity profile] mh75.livejournal.com
Yeah, this is where it is handy that i mostly write prototypes. Anyway, all i generally use Eps for is to see if a function is reasonably close to a known desirable value (and i know what reasonable is). I honestly haven't figured out what else they use quantum for.

Thanks for the code!

Profile

meganursula: (Default)
Megan Hazen

May 2020

S M T W T F S
     12
3456789
101112 13141516
17181920212223
24252627282930
31      

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Jun. 11th, 2025 08:27 pm
Powered by Dreamwidth Studios