Topic: Anybody got a way to do c**i or c^i?
Author: glenn@bitstream.com (Glenn P. Parker)
Date: 17 Sep 91 13:51:14 GMT Raw View
[Followups to comp.lang.c++]
In article <1991Sep17.100623.2675@cs.ruu.nl> nevries@cs.ruu.nl (Nico de Vries) writes:
> In <91256.164516SMEYER@SLACVM.SLAC.STANFORD.EDU> SMEYER@SLACVM.SLAC.STANFORD.EDU writes:
>
> >I'm looking for a (class) or other implementation of a power
> >function ...
>
> In C++ it is impossible to create (syntactic) NEW operators. You can
> only overload existing operators. If you never use the && operator
> for floats/doubles you could overload it (or another operator).
> If you REALLY insist on using ^ of ** you will have to create
> a parser to convert your new c+++ to c++.
Except that ^ _is_ a C++ operator, remember? Drop this in your compiler:
class foo {
public:
foo(int aa) : a(aa) {}
foo& operator^(const foo& that) { a ^= that.a; return *this; }
private:
int a;
};
main()
{
foo t1(1), t2(2);
t2 = t1 ^ t2;
}
The problem with operator^ is that it retains the low precedence of the
original exclusive-or operator. Therefore, it won't behave as expected in
"normal-looking" C++ arithmetic expressions.
--
Glenn P. Parker glenn@bitstream.com Bitstream, Inc.
uunet!huxley!glenn 215 First Street
BIX: parker Cambridge, MA 02142-1270
Author: nevries@cs.ruu.nl (Nico de Vries)
Date: 23 Sep 91 09:39:20 GMT Raw View
I read someone said ^ is an existing operator so you can
overload it to use it for pow() replacement.
The disadvantage of this is you lose the ^ operator
in its original defenition. The ^ operator is already
defined for int's. You could considder defining
only for float's of cource but then 123.4^7 wouldn't
work (7.0 whould be used). None of these solutions
seems verry neat to me.
Nico de Vries
Author: steve@taumet.com (Stephen D. Clamage)
Date: 23 Sep 91 18:15:45 GMT Raw View
nevries@cs.ruu.nl (Nico de Vries) writes:
>I read someone said ^ is an existing operator so you can
>overload it to use it for pow() replacement.
>The disadvantage of this is you lose the ^ operator
>in its original defenition. The ^ operator is already
>defined for int's. You could considder defining
>only for float's of cource but then 123.4^7 wouldn't
>work (7.0 whould be used). None of these solutions
>seems verry neat to me.
Existing operators may be overloaded only when at least one operand
is of class type. So you could declare, for example, 'class complex'
and overload ^ for use with the base or exponent or both of type
'complex'. Doing so does not eliminate the use of that operator for
existing uses. (That's why it is called 'overloading'.)
You cannot overload any operator for use only with floating-point
or integer operands, nor for use only with non-class types.
--
Steve Clamage, TauMetric Corp, steve@taumet.com
Author: matt@physics.berkeley.edu (Matt Austern)
Date: 26 Sep 91 00:42:26 GMT Raw View
In article <966@taumet.com> steve@taumet.com (Stephen D. Clamage) writes:
> Existing operators may be overloaded only when at least one operand
> is of class type. So you could declare, for example, 'class complex'
> and overload ^ for use with the base or exponent or both of type
> 'complex'.
But you shouldn't. The precedence is wrong: an exponentiation
operator with a precedence lower than addition will lead to too many
hard-to-catch bugs.
If you want exponentiation to be an operator (as I do), you should ask
the companies you buy compilers from to add an exponentiaion operator
as a language extension.
--
Matt Austern matt@physics.berkeley.edu What if everything is an illusion
(415) 644-2618 austern@lbl.bitnet and nothing exists? In that case,
austern@theorm.lbl.gov I definitely overpaid for my carpet.
Author: fjh@mundil.cs.mu.OZ.AU (Fergus James HENDERSON)
Date: 26 Sep 91 06:41:59 GMT Raw View
matt@physics.berkeley.edu (Matt Austern) writes:
>In article <966@taumet.com> steve@taumet.com (Stephen D. Clamage) writes:
>> Existing operators may be overloaded only when at least one operand
>> is of class type. So you could declare, for example, 'class complex'
>> and overload ^ for use with the base or exponent or both of type
>> 'complex'.
>But you shouldn't. The precedence is wrong: an exponentiation
>operator with a precedence lower than addition will lead to too many
>hard-to-catch bugs.
>If you want exponentiation to be an operator (as I do), you should ask
>the companies you buy compilers from to add an exponentiaion operator
>as a language extension.
Previously I posted (half in jest) a solution to the original problem which
involved overloading unary "*" for a complex number to return an object of type
"exponent", and overloading binary "*" for (complex * exponent) to evaluate
as exponentiation. Thus in
Complex x,y;
Complex z = x ** y;
the "x ** y" gets parsed as "x * (*y)", (*y) is of type exponent, and so we
end up with the desired result.
The only response to my post was a single reply ("Sick, very sick.").
Now maybe this is an "obfuscated C++" way of doing it, but I don't see
why it wouldn't work in practice, and I can't see any major precedence
problems. Now you guys are proposing *language extensions* (shock, horror!)
to cope with a problem that can already be solved very simply using the
existing language! Really, I think you should spend our efforts lobbying
compiler vendors to give us really useful, needed features like templates
and exception handling.
Regards,
Fergus.
---
Fergus Henderson /* Real programmers get it right first time... */
fjh@mundil.cs.mu.oz.au #include "/dev/tty"
Author: shankar@fakir.india.hp.com (Shankar Unni)
Date: 25 Sep 91 03:45:52 GMT Raw View
In comp.std.c++, nevries@cs.ruu.nl (Nico de Vries) writes:
> I read someone said ^ is an existing operator so you can
> overload it to use it for pow() replacement.
In any case, the "^" operator is not really suited for use as
an exponentiation operator like in Fortran, for two reasons:
- precendence is wrong: 3 * 10 ^ 5 is parsed as (3*10) ^ 5.
- associativity is wrong: 3 ^ 2 ^ 6 is parsed as (3^2) ^ 6.
Both of these are contrary to what someone used to classical
exponentiation operators would expect.
Bottom line: you could use it, but be sure to exhaustively parenthesize
the expression to prevent nasty surprises.
-----
Shankar Unni E-Mail:
HP India Software Operation, Bangalore Internet: shankar@india.hp.com
Phone : +91-812-265595 UUCP: ...!hplabs!hpda!shankar
Author: jimad@microsoft.com (Jim ADCOCK)
Date: 1 Oct 91 19:18:14 GMT Raw View
Sick, very sick
-- but I kind of like it! :-)
Author: SMEYER@SLACVM.SLAC.STANFORD.EDU
Date: 14 Sep 91 00:45:15 GMT Raw View
I'm looking for a (class) or other implementation of a power
function that can be written with operator overloading such
as c**i or c^i to indicate c to the ith power.
Steven R. Meyer - Stanford Linear Accelerator Center
SLACVX.SLAC.STANFORD.EDU
Author: nevries@cs.ruu.nl (Nico de Vries)
Date: 17 Sep 91 10:06:23 GMT Raw View
In <91256.164516SMEYER@SLACVM.SLAC.STANFORD.EDU> SMEYER@SLACVM.SLAC.STANFORD.EDU writes:
>I'm looking for a (class) or other implementation of a power
>function that can be written with operator overloading such
>as c**i or c^i to indicate c to the ith power.
>Steven R. Meyer - Stanford Linear Accelerator Center
>SLACVX.SLAC.STANFORD.EDU
In C++ it is impossible to create (syntactic) NEW operators. You can
only overload existing operators. If you never use the && operator
for floats/doubles you could overload it (or another operator).
If you REALLY insist on using ^ of ** you will have to create
a parser to convert your new c+++ to c++.
Nico de Vries