home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / std / cplus / 1190 < prev    next >
Encoding:
Text File  |  1992-09-15  |  2.2 KB  |  64 lines

  1. Xref: sparky comp.std.c++:1190 comp.lang.c++:13706
  2. Newsgroups: comp.std.c++,comp.lang.c++
  3. Path: sparky!uunet!cis.ohio-state.edu!zaphod.mps.ohio-state.edu!malgudi.oar.net!caen!uvaarpa!murdoch!virginia.edu!gs4t
  4. From: gs4t@virginia.edu (Gnanasekaran Swaminathan)
  5. Subject: Re: Exponentiation operator proposal
  6. Message-ID: <1992Sep15.201801.26417@murdoch.acc.Virginia.EDU>
  7. Sender: usenet@murdoch.acc.Virginia.EDU
  8. Reply-To: gs4t@virginia.edu (Gnanasekaran Swaminathan)
  9. Organization: University of Virginia
  10. References: <23660@alice.att.com> <1992Sep13.193507.27813@ima.isc.com> <MATT.92Sep14225005@physics2.berkeley.edu>
  11. Date: Tue, 15 Sep 1992 20:18:01 GMT
  12. Lines: 50
  13.  
  14. matt@physics2.berkeley.edu (Matt Austern) writes:
  15.  
  16. : I believe that an exponentiation operator would be of genuine benefit
  17. : to C++ scientific programmers, and I believe that we who write
  18. : scientific code are justified in asking that it be included.  I don't
  19. : think, however, that we are justified in asking for a change to the
  20. : language that makes life harder for other C++ programmers.  Using **
  21. : to denote exponentiation does exactly that: it changes the meaning of
  22. : legal C++ code, and it makes a useful programming technique more
  23. : difficult.
  24. : It is possible to implement an exponentiation operator so that the
  25. : extension to the language will have no effect at all for people who
  26. : don't deliberately use the new feature.  This is a better idea.
  27.  
  28. Did you consider operator () for exponentiation?
  29. It seems to have correct precedence and doesn't need new tokens to be
  30. introduced.
  31.  
  32. // if op () is available for exp
  33. double e= -10(2); // e = -100.0
  34. double d=10;
  35. double *p = &e;
  36.  
  37. e = d(3) - (*p)(2) + d + 3; // e = -8987.0
  38.  
  39. // status quo
  40. class Double {
  41.     double d;
  42. public:
  43.     Double(double f): d(f) {}
  44.     operator double () { return d; }
  45.     Double operator - () { return -d; }
  46.     Double operator () (int i) { ... }
  47.     Double operator () (double f) { return pow(d,f); }
  48.     // other ops ...
  49. };
  50.  
  51. Double a(10);  // a = 10.0
  52. double d=a(5); // d = 10000.0
  53.  
  54. a = a(3.0)*2.5; // a = 2500.0
  55.  
  56. Double b(2); // b = 2
  57. a = -b(4) + 3*b(3) - 2*b(2) + b + 3; // a = 5.0
  58.  
  59. Similary you can define your own Int as well and use it wherever
  60. you want int and exp op. 
  61.  
  62. -Sekar
  63.