home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!stanford.edu!EE.Stanford.EDU!sierra!mcgrant
- From: mcgrant@rascals.stanford.edu (Michael C. Grant)
- Subject: the 'standard' complex class
- Message-ID: <MCGRANT.92Aug26143400@rascals.stanford.edu>
- Sender: usenet@EE.Stanford.EDU (Usenet)
- Organization: Information Systems Laboratory, Stanford University
- Date: 26 Aug 92 14:34:00
- Lines: 48
-
-
- I am rather frustrated with the approach that AT&T took with its complex
- class, specifically the decision to make the real and imaginary fields
- private. (GNU's libg++ makes them protected, which is similarly frustrating
- but not quite as much so).
-
- Because the class does not need to be specially notified when the .re or
- .im fields change, why not make them public? It certainly allows for
- maximum flexibility; it won't break a single function already written
- for complex numbers.
-
- It also seems silly that in <math.h> we have
- double log( double );
- but in <complex.h> it is declared as a friend function:
- friend complex log( complex )
-
- I rarely if ever use public data members in my classes. But, if there is no
- need for my class to 'know' that a member was changed (if I don't change
- my behavior based on that field, and if I don't need to check if its value
- is legal, for instance), often I just make it public. I say 'often' because
- sometimes in the effort of providing a consistent interface I supply set
- and get methods for those fields too.
-
- In this case, though, there are only TWO fields, in a class whose behavior
- is invariant to changes in those fields, whose values you cannot assign
- 'illegal' values. I suppose complex infinites and NaN's are a tricky issue
- but to handle them inside the class would be to slow things down way too
- much anyway. So, I see no reason besides some overly-dogmatic
- information-hiding argument to keep the fields private
-
- I'm complaining because I'm trying to really tighten some inner loops in my
- code (routines on the level of BLAS). Having to go through the various
- public double->complex conversions, plus the fact that because complex is
- two doubles wide and can't be stored in a register, hinder the optimization.
- When I open up the complex class by altering the header file, I can easily
- make things just as fast as I expect they can be by allowing me to treat
- the real and complex fields separately. Since this is supposed to act like
- a basic data type, these optimization differences should not occur; and,
- if making the fields public and letting me treat them individually is the
- way to make things faster, I say so be it.
-
- Well, I have said way too much on this simple issue. I'd like to hear
- some other opinions. Believe me, I virutally never use public data fields,
- so I am indeed advocating this as more of an exception rather than the
- rule. What does everyone else think?
-
- Michael C. Grant
-
-