[comp.arch] Comparison operators

magnus@THEP.LU.SE (Magnus Olsson) (07/30/90)

In article <25681@cs.yale.edu>, zenith@cs.yale.edu writes:
>a := b	means "assign the value of b to a".
>a = b	means "a is equal to b".
>
>The use of := distinguishes assignment from equality, thus prevents
>overloading a single operator and IMHO is a much nicer solution to the
>C hack == used to ovecome the same problem.

The people behind C *deliberately* changed the Algol operators
'=' and ':=' to '==' and '=' to save keystrokes - in most programs,
assignment is done much more often than comparison for equality, so,
quite in line with the philosophy behind C, they chose the shorter form
for the most common operation.

IMHO, using '=' for assignment isn't too bad - after all, that's the way
you do it in Fortran. The *real* drawback is that it's just too easy
to write
if (y=0) printf ("Division by zero"); else z=x/y;
instead of
if (y==0) ...
and get a quite unexpected result...

[For those who don't know C: The assignment 'y=0' is a perfectly legal
logical operator, with the value 0 (i.e. FALSE). So the code will
first assign 0 to y, then proceed to divide x by y...]

If you want to see a *really* ugly solution, have a look at Fortran,
where the comparison operators are written .EQ., .GT. and so on.

BTW, does anyone know the reason for this? In Algol and C, you *must*
have different operators, since e.g. the Algol statements

a := b := 1;  and  a := b = 1; 

mean two entirely different things ("assign 1 to both a and b" and 
"assign the logical result of the comparison b=1 to a", respectively).


Magnus Olsson		     	| \e+ 	   /_	      
Dept. of Theoretical Physics 	|  \  Z	  / q	      
University of Lund, Sweden	|   >----<	      
Internet: magnus@thep.lu.se	|  /	  \===== g    
Bitnet: THEPMO@SELDC52 		| /e-	   \q	      

steve@taumet.com (Stephen Clamage) (07/31/90)

magnus@THEP.LU.SE (Magnus Olsson) writes:

>If you want to see a *really* ugly solution, have a look at Fortran,
>where the comparison operators are written .EQ., .GT. and so on.

>BTW, does anyone know the reason for this? In Algol and C, you *must*
>have different operators ...

By "this", I assume you mean the FORTRAN operators .EQ. etc.  We must
remember the kinds of I/O equipment in use when FORTRAN was developed
in the mid-1950's.  There was no ASCII character set, and manufacturers
used their own (incompatible) character sets and encodings.  There
were usually none of the characters < > : ; ' " nor others that we are
now accustomed to.  Beyond this, the FORTRAN rule was that spaces did
not matter, and could be inserted or deleted anywhere without changing
the program except in Hollerith fields (literal character strings).
You could not write
	IF (A < B) ...
but
	IF (A LT B) ...
had to mean the same as
	IF(ALTB) ...
This was made unambiguous by using dots in the operators which looked
like identifiers (.EQ. .GT. .AND. .OR. etc).
	IF(A.LT.B) ...
-- 

Steve Clamage, TauMetric Corp, steve@taumet.com

news@ism780c.isc.com (News system) (08/01/90)

In article <9007301417.AA21099@thep.lu.se> magnus@THEP.LU.SE (Magnus Olsson) writes:
>If you want to see a *really* ugly solution, have a look at Fortran,
>where the comparison operators are written .EQ., .GT. and so on.
>
>BTW, does anyone know the reason for this?

FORTRAN (actually FORTRAN/II, FORTRAN did not have .EQ.) was designed when
input devices (key punches) and printers had only 48 characters in the
character set.  The characters were:

     26 capital letters
     10 decimal digits
	space
     11 special characters  + - / * ( ) . , $ = @  (scientific set)
or for some printers
     11 special characters  & - / * % ^ . , $ # @  (commercial set)

The ^ shown above was not the actual commercial graphic.  The graphic was
a pillow shaped character.  Also my memory is slightly fuzzy about the @ in
scientific set.  I may have been something else.  But in any event the
characters < and > were not available.

If you were unlucky and had a printer with the commercial character set, a
FORTRAN statement like:

      Y(I)=X(J)+10.0

Would print as:

      Y%I^#X%J^&10.0   (where the ^ is really the pillow)

And since no one used spaces, the programs were almost imposible to read.

davidsen@crdos1.crd.ge.COM (Wm E Davidsen Jr) (08/02/90)

In article <9007301417.AA21099@thep.lu.se> magnus@THEP.LU.SE (Magnus Olsson) writes:

| The people behind C *deliberately* changed the Algol operators
| '=' and ':=' to '==' and '=' to save keystrokes - in most programs,

  But the change was made in B, not C, and just carried over. It may
well have been in BCPL, but I haven't written any in so long I'll have
to look it up.

  When I generated a tiny implementation language I went back to := to
avoid typing mistakes, and used <> instead of != because a lot more
people could read it first time.

  The language ran on a number of CPU's, including the GE600 (now
Honelwell) and Intel 8080, and 8085 using the undocumented instructions
for stack access.

  Historical note: when GE made terminet printers, some versions used
the 8085 and those special instructions. GE had a contract with Intel to
supply the CPU with the instructions working.
-- 
bill davidsen	(davidsen@crdos1.crd.GE.COM -or- uunet!crdgw1!crdos1!davidsen)
            "Stupidity, like virtue, is its own reward" -me

jlg@lanl.gov (Jim Giles) (08/03/90)

From article <2377@crdos1.crd.ge.COM>, by davidsen@crdos1.crd.ge.COM (Wm E Davidsen Jr):
> [...]
>   When I generated a tiny implementation language I went back to := to
> avoid typing mistakes, and used <> instead of != because a lot more
> people could read it first time.

Well, you've actually got back to a comp.arch topic.  The problem here
is that <> doesn't mean the same thing as !=.  The first means (intuitively
anyway) less-than or greater-than, while the second means not equal.
Now, suppose you have two NANs:

      NAN1 <> NAN2
      NAN1 != NAN2

The first of these conditions is false whicl the second is true.  NANs
are unordered (so they can't be "less-than or greater-than" each other),
but they also always compare as "not equal".

J. Giles

nigel@modcomp.UUCP (Nigel Gamble) (08/03/90)

davidsen@crdos1.crd.ge.COM (Wm E Davidsen Jr) writes:
>In article <9007301417.AA21099@thep.lu.se> magnus@THEP.LU.SE (Magnus Olsson) writes:
>| The people behind C *deliberately* changed the Algol operators
>| '=' and ':=' to '==' and '=' to save keystrokes - in most programs,
>  But the change was made in B, not C, and just carried over. It may
>well have been in BCPL, but I haven't written any in so long I'll have
>to look it up.

No, BCPL uses ':=' for assignment.  It uses '=' for equality, but
interestingly (to me, anyway) it uses neither '!=' nor '<>' for
inequality.  Instead, the compiler will accept either '~=' or '\='
as the inequality operator.  The latter is suggestive of the
mathematical symbol.
-- 
Nigel Gamble					uunet!modcomp!nigel
MODCOMP, an AEG company				modcomp!nigel@uunet.UU.NET
1650 W McNab Rd,
Ft Lauderdale, FL 33340-6099