Velocity Reviews > How to redefine arithmetic operators.

# How to redefine arithmetic operators.

Keith Thompson
Guest
Posts: n/a

 11-29-2005
jacob navia <(E-Mail Removed)> writes:
> Keith Thompson a écrit :
>> with
>> it, you can do without it (with ordinary function calls).

>
> This is of course true.
>
> It is just a matter of measure.
>
> Consider this:
> qfloat a;
> qfloat q = (sqrt(a+1)/sqrt(a-1))*(cos(a+1)/cos(a-1));
>
> compared with:
>
> tmp2 = sqrtq(tmp1);
> tmp3 = qsub(a,1);
> tmp4 = sqrt(tmp3);
> tmp5 = qdiv(tmp1,tmp3);
> tmp6 = cosq(tmp1);
> tmp7 = cosq(tmp3);
> tmp8 = qdiv(tmp6,tmp7);
> result = qmul(tmp8,tmp5);
>
> This does the *same* stuff but which expression would
> you prefer???

I prefer the one that's valid C, and that I can use on systems other
than Win32.

In the context of this newsgroup (now listen carefully), *C does not
carefully -- but those languages have their own newsgroups, as does
the lcc compiler.

And jacob, please don't send me e-mail copies of Usenet followups.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

Dik T. Winter
Guest
Posts: n/a

 11-30-2005
In article <(E-Mail Removed)> jacob navia <(E-Mail Removed)> writes:
....
> Consider this:
> qfloat a;
> qfloat q = (sqrt(a+1)/sqrt(a-1))*(cos(a+1)/cos(a-1));
>
> compared with:
>
> tmp2 = sqrtq(tmp1);
> tmp3 = qsub(a,1);
> tmp4 = sqrt(tmp3);
> tmp5 = qdiv(tmp1,tmp3);
> tmp6 = cosq(tmp1);
> tmp7 = cosq(tmp3);
> tmp8 = qdiv(tmp6,tmp7);
> result = qmul(tmp8,tmp5);
>
> This does the *same* stuff but which expression would
> you prefer???

This is obviously a distortion of what is possible, how many of those
tmp's do you really need?
asub1 = qsub(a, 1);
result = qmul(qdiv(qsqrt(aplus1),qsqrt(asub1)),
qdiv(qcos(aplus1),qcos(asub1)));

Does your compiler calculate a+1 and a-1 only once? I have no idea, and
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/

Eric Sosman
Guest
Posts: n/a

 11-30-2005
Dik T. Winter wrote:

> In article <(E-Mail Removed)> jacob navia <(E-Mail Removed)> writes:
> ...
> > Consider this:
> > qfloat a;
> > qfloat q = (sqrt(a+1)/sqrt(a-1))*(cos(a+1)/cos(a-1));
> >
> > compared with:
> >
> > tmp2 = sqrtq(tmp1);
> > tmp3 = qsub(a,1);
> > tmp4 = sqrt(tmp3);
> > tmp5 = qdiv(tmp1,tmp3);
> > tmp6 = cosq(tmp1);
> > tmp7 = cosq(tmp3);
> > tmp8 = qdiv(tmp6,tmp7);
> > result = qmul(tmp8,tmp5);
> >
> > This does the *same* stuff but which expression would
> > you prefer???

>
> This is obviously a distortion of what is possible, how many of those
> tmp's do you really need?
> asub1 = qsub(a, 1);
> result = qmul(qdiv(qsqrt(aplus1),qsqrt(asub1)),
> qdiv(qcos(aplus1),qcos(asub1)));
>
> Does your compiler calculate a+1 and a-1 only once? I have no idea, and

It's very old-fashioned of me, I know, but I cringe at
code that calculates a product or quotient of square roots.

Other spine-tinglers include products and quotients of
power functions and exponentials, sums of logarithms, and
trig functions applied to arctangents. All such expressions
ought to be simplified unless there's a *very* good reason
not to, usually having to do with extraordinary care about
precision: sqrt(x)/sqrt(y) need not equal sqrt(x/y) exactly.
But if that sort of thing is a problem, the code needs an
extensive comment to explain why the expression should not
be simplified; the default behavior should be to simplify
such things, always.

--
Eric Sosman
(E-Mail Removed)lid