[comp.lang.pascal] Help Me!!! What's wrong with this simple problem?

mpe@shamash.cdc.com (Mike Ebsen) (11/28/90)

What's wrong with this problem in Turbo Pascal 5.0...

      Program test;
      var
        i : integer;
        j : word;
      begin
        j:=3;
        i:=j-10;
        writeln(j,'     ',i);
      end.

My compiler tells me that the assignment of -7 into i (an integer) 
is an error.

mcastle@mcs213f.cs.umr.edu (Mike Castle (Nexus)) (11/28/90)

In article <28893@shamash.cdc.com> mpe@shamash.UUCP (Mike Ebsen) writes:
>What's wrong with this problem in Turbo Pascal 5.0...
>
>      Program test;
>      var
>        i : integer;
>        j : word;
>      begin
>        j:=3;
>        i:=j-10;
>        writeln(j,'     ',i);
>      end.
>
>My compiler tells me that the assignment of -7 into i (an integer) 
>is an error.

Well, it first tries to evaluate j-10.  Since j is a word, it tries to 
evaluate j-10 as a word.  A word ranges from 0..65535.  So -7 is an error.
That's the way pascal works, I assume, as opposed to an error.

If it put 3 into i first, and THEN subtracted 7, no problem, but it doesn't
work that way.

You can defeat this by putting in the compiler directive {$R-} or changing
it range checking to off from the options menu, but I wouldn't advise it.
At least, not until you've gotten all the other bugs worked out and you wanted
to save some speed and size.

By turning off the range checking, I got it to print out 3 and -7, but that's
no guarantee it would work all the time.

In short, try this:

    i := j;
    i := i-7;

Always pays to look up the run time errors in the manual too....  :->
-- 
Mike Castle (Nexus) S087891@UMRVMA.UMR.EDU (preferred)       | ERROR:  Invalid
                mcastle@mcs213k.cs.umr.edu (unix mail-YEACH!)| command 'HELP'
Life is like a clock:  You can work constantly, and be right | try 'HELP'
all the time, or not work at all, and be right twice a day.  |

ts@uwasa.fi (Timo Salmi) (11/28/90)

In article <28893@shamash.cdc.com> mpe@shamash.UUCP (Mike Ebsen) writes:
>What's wrong with this problem in Turbo Pascal 5.0...
>
>      Program test;
>      var
>        i : integer;
>        j : word;
>      begin
>        j:=3;
>        i:=j-10;
>        writeln(j,'     ',i);
>      end.
>
>My compiler tells me that the assignment of -7 into i (an integer) 
>is an error.

Yes, but j-10 is a word, and you cannot assign a negative value to
to it.  As far as I know i := j - 10 puts the value of the
right-hand side into the left-hand side only after j-10 has been
calculated. 

...................................................................
Prof. Timo Salmi        (Moderating at anon. ftp site 128.214.12.3)
School of Business Studies, University of Vaasa, SF-65101, Finland
Internet: ts@chyde.uwasa.fi Funet: gado::salmi Bitnet: salmi@finfun

John G. Spragge <SPRAGGEJ@QUCDN.QueensU.CA> (11/28/90)

In article <28893@shamash.cdc.com>, mpe@shamash.cdc.com (Mike Ebsen) says:
>
>What's wrong with this problem in Turbo Pascal 5.0...
>
>      Program test;
>      var
>        i : integer;
>        j : word;
>      begin
>        j:=3;
>        i:=j-10;
>        writeln(j,'     ',i);
>      end.
>
>My compiler tells me that the assignment of -7 into i (an integer)
>is an error.

The expression j-10 is evaluated as a word expression. The result
of subtracting 10 from 3 in a word will not be -7, since the
word is unsigned: it is 65536-7, or 65529. Since integers only
range from -32768 to 32767, the compiler detects an error.

dslg0849@uxa.cso.uiuc.edu (Daniel S. Lewart) (11/28/90)

mpe@shamash.cdc.com (Mike Ebsen) writes:
> What's wrong with this problem in Turbo Pascal 5.0...
>
>       Program test;
>       var
>         i : integer;
>         j : word;
>       begin
>         j:=3;
>         i:=j-10;
>         writeln(j,'     ',i);
>       end.
>
> My compiler tells me that the assignment of -7 into i (an integer) 
> is an error.

You could use typecasting to fix the offending line as follows:
        i := Integer(j) - 10;

Daniel Lewart
d-lewart@uiuc.edu

decomyn@penguin.uss.tek.com (11/29/90)

In article <28893@shamash.cdc.com> mpe@shamash.UUCP (Mike Ebsen) writes:
>What's wrong with this problem in Turbo Pascal 5.0...
>
>      Program test;
>      var
>        i : integer;
>        j : word;
>      begin
>        j:=3;
>        i:=j-10;
>        writeln(j,'     ',i);
>      end.
>
>My compiler tells me that the assignment of -7 into i (an integer) 
>is an error.

Looks like a minor TP bug.  What appears (to me) to be happening is that
the internal representation of j-10 is being treated as arithmetic on a 
word value.  Since -7 is an illegal value for a word variable, the 
resulting code is returning an error (you didn't say exactly what error
code TP was giving you, so this is just a guess.)

Try typecasting j into an integer... (blast, my references are at home.  
I *think* you can do that by entering the following:

	i:=integer(j)-10;

in place of the line giving you the error, but I'm not certain.)

Good Luck

-------------------------------------------------------------------------------
Brendt Hess a.k.a.             | Disclaimer: Opinions?  I don't even work here!
Vergil William de Comyn a.k.a. |-----------------------------------------------
Payne Hirds                    |       Life is not a zero-sum game:
decomyn@penguin.uss.tek.com    |          don't treat it as such.

togood@roger.lerc.nasa.gov (Chris Miller) (11/29/90)

In article <28893@shamash.cdc.com> mpe@shamash.UUCP (Mike Ebsen) writes:
>What's wrong with this problem in Turbo Pascal 5.0...
>
>      Program test;
>      var
>        i : integer;
>        j : word;
>      begin
>        j:=3;
>        i:=j-10;
>        writeln(j,'     ',i);
>      end.
>
>My compiler tells me that the assignment of -7 into i (an integer) 
>is an error.

Because j is of type word, the compiler is attempting to evaluate j-10 as type
word.  You must force it to be an integer.  One way is:
         i:=integer(j-10);

nmouawad@water.uwaterloo.ca (Naji Mouawad) (11/29/90)

In article <1990Nov28.090232.2958@uwasa.fi> ts@uwasa.fi (Timo Salmi) writes:
>In article <28893@shamash.cdc.com> mpe@shamash.UUCP (Mike Ebsen) writes:
>>What's wrong with this problem in Turbo Pascal 5.0...
>>
>>      Program test;
>>      var
>>        i : integer;
>>        j : word;
>>      begin
>>        j:=3;
>>        i:=j-10;
>>        writeln(j,'     ',i);
>>      end.
>>
>>My compiler tells me that the assignment of -7 into i (an integer) 
>>is an error.
>
>Yes, but j-10 is a word, and you cannot assign a negative value to
>to it.  As far as I know i := j - 10 puts the value of the
>right-hand side into the left-hand side only after j-10 has been
>calculated. 
>
>...................................................................
>Prof. Timo Salmi        (Moderating at anon. ftp site 128.214.12.3)
>School of Business Studies, University of Vaasa, SF-65101, Finland
>Internet: ts@chyde.uwasa.fi Funet: gado::salmi Bitnet: salmi@finfun

Or it may be a bug in tp5.0 . I ran this program yesterday and
I ran it today twice (with {$R+}) in tp5.5 and it ran like a
charm. No syntax errors, no sweat.

Anybody out there with tp5.5 could confirm that ? Or is my
version of tp5.5. a freak ?

--Naji
--
         ---------------+-------------------------------------------
        | Naji Mouawad  |       nmouawad@water.waterloo.edu         |
        |  University   |-------------------------------------------|
        | Of Waterloo   |   "Thank God we cannot prove He Exists."  |

kamal@wpi.WPI.EDU (Kamal Z Zamli) (11/29/90)

In article <28893@shamash.cdc.com> mpe@shamash.UUCP (Mike Ebsen) writes:
>What's wrong with this problem in Turbo Pascal 5.0...
>
>      Program test;
>      var
>        i : integer;
>        j : word;
>      begin
>        j:=3;
>        i:=j-10;
>        writeln(j,'     ',i);
>      end.
>
>My compiler tells me that the assignment of -7 into i (an integer) 
>is an error.


The reason that Turbo Pascal gives you an error is that you define
j as of type word.  if you declare a var of type word you must
make sure that its value will never go negative.

  In other word 0<j<64000

Hope that's help

mcastle@mcs213f.cs.umr.edu (Mike Castle (Nexus)) (11/29/90)

In article <1990Nov28.180310.3147@aftermath.waterloo.edu> nmouawad@water.uwaterloo.ca (Naji Mouawad) writes:
>In article <1990Nov28.090232.2958@uwasa.fi> ts@uwasa.fi (Timo Salmi) writes:
>>In article <28893@shamash.cdc.com> mpe@shamash.UUCP (Mike Ebsen) writes:
>>>What's wrong with this problem in Turbo Pascal 5.0...
>>>
(* program we all know by heart now delted *)
>>>
>>>My compiler tells me that the assignment of -7 into i (an integer) 
>>>is an error.
>>
(* Timo's response also deleted *)
>
>Or it may be a bug in tp5.0 . I ran this program yesterday and
>I ran it today twice (with {$R+}) in tp5.5 and it ran like a
>charm. No syntax errors, no sweat.
>
>Anybody out there with tp5.5 could confirm that ? Or is my
>version of tp5.5. a freak ?
>
>--Naji
(* Naji's sig deleted *)

It's a freak.  :->

Seriously, I just ran it on my Turbo 5.5, and it crashes.

I get:  Error 201:  Range check error.

Hmm... I did just discover something unusual , though.  I compiled it with 
just the options changed in the compiler, no compiler directives.  The programs
running depended on the the Range checking option under the 
Options/Compliler  menu.

I Then tried with compiler directives.  Got the same results.  It ran with 
range checking off, giving -7 for i.  Crashed with error with range checking
on.  I then tried toggling the options/compiler/range checking menu option to
see if it had any effect.  It didn't as long as the compiler directive was in
the file.  This is how it should be.  Compiler directives in code take 
precedence over menu chosen compiler options.

Now, I removed the compiler directive from the file.  The option menu still has
no effect!!!  My last source code compiler directive was {$R+} and naturally
it crashed.  I removed it from the file by typing spaces over it.  Now, no 
matter what the options/compiler/range checking option is, it will not run.

I then deleted the entire line, and we're back to normal.  Correction, we're
not.  Still flakey.  Hmm... interesting.  I believe we've found a compiler 
bug.

Never mind, I'm a bone-head.  :->   I just realized that I wasn't recompiling
after I changed the option via the menu, just re-running what was in memory.
But, when I changed the source code to turn on/off range checking, it
automatically recompiled it.  

Well, if nothing else, I've learned to hit alt-f9 first, THEN cntrl-f9.  I 
wonder if there's an option to change it into auto-matically compile
every time it's run.... time to pull out the manuals....

I really should not post this considering what a fool I've made myself, but
maybe it'll point out some problems others are having (yeah, right :-)

*sigh*  and just after a data structures test too....
-- 
Mike Castle (Nexus) S087891@UMRVMA.UMR.EDU (preferred)       | ERROR:  Invalid
                mcastle@mcs213k.cs.umr.edu (unix mail-YEACH!)| command 'HELP'
Life is like a clock:  You can work constantly, and be right | try 'HELP'
all the time, or not work at all, and be right twice a day.  |

mjensen@ccwf.cc.utexas.edu (Marc S. Jensen) (11/29/90)

In article <28893@shamash.cdc.com> mpe@shamash.UUCP (Mike Ebsen) writes:
>What's wrong with this problem in Turbo Pascal 5.0...
>
>      Program test;
>      var
>        i : integer;
>        j : word;
>      begin
>        j:=3;
>        i:=j-10;
>        writeln(j,'     ',i);
>      end.
>
>My compiler tells me that the assignment of -7 into i (an integer) 
>is an error.


The problem is not the -7, but the fact that j is declared as being of
type word. In TP, a word is defined as an unsigned 16-bit data type, 
whereas an integer is a signed 16-bit type. It is therefore possible to
store numbers in a word that cannot possibly fit in an integer. (Numbers
from 32768 through 65535 can be words but not integers.) To safeguard 
against this, the compiler disallows assigning a word value to an integer
variable.
In your example, you could either declare j : integer, or, if you
*need* it to be a word, i : longint or i : word.



-- 

Marc Jensen
mjensen@ccwf.cc.utexas.edu
University of Texas at Austin
-----
"Never attribute to malice that which is adequately explained by stupidity!"

fritz@urz.unibas.ch (11/29/90)

In article <1990Nov28.180310.3147@aftermath.waterloo.edu>, nmouawad@water.uwaterloo.ca (Naji Mouawad) writes:
> In article <1990Nov28.090232.2958@uwasa.fi> ts@uwasa.fi (Timo Salmi) writes:
>>In article <28893@shamash.cdc.com> mpe@shamash.UUCP (Mike Ebsen) writes:
>>>What's wrong with this problem in Turbo Pascal 5.0...
>>>
>>>      Program test;
>>>      var
>>>        i : integer;
>>>        j : word;
>>>      begin
>>>        j:=3;
>>>        i:=j-10;
>>>        writeln(j,'     ',i);
>>>      end.
>>>
>>>My compiler tells me that the assignment of -7 into i (an integer) 
>>>is an error.
>>
>>Yes, but j-10 is a word, and you cannot assign a negative value to
>>to it.  As far as I know i := j - 10 puts the value of the
>>right-hand side into the left-hand side only after j-10 has been
>>calculated. 
>>
>>...................................................................
>>Prof. Timo Salmi        (Moderating at anon. ftp site 128.214.12.3)
>>School of Business Studies, University of Vaasa, SF-65101, Finland
>>Internet: ts@chyde.uwasa.fi Funet: gado::salmi Bitnet: salmi@finfun
> 
> Or it may be a bug in tp5.0 . I ran this program yesterday and
> I ran it today twice (with {$R+}) in tp5.5 and it ran like a
> charm. No syntax errors, no sweat.
> 
> Anybody out there with tp5.5 could confirm that ? Or is my
> version of tp5.5. a freak ?
> 

Sorry, but I cannot confirm that. My TP 5.5 tells me a 
range checking error when I try to compile the program
above with the compiler option {$R+}. Only by setting
it to {$R-} or by changing the compiler option in the
menu I can compile the program.
                           
Perhaps your menu option was RANGE CHECKING OFF and you
could compile the program without any problem. If you insert
{$R+} into the source code you must press F9 afterwards. Then
you will see the error message. If you just press Ctrl-F9 the
previously compiled program, which is still in memory, will be
run!!!



		Oliver Fritz, University of Basel, Switzerland

paulg@bhpmrl.oz.au (Paul Gallagher) (11/30/90)

Geez, everyone's jumping in on this question!

The problem (j is used as accumulator and hence can't legally store -7)
has already been pointed out. Some of the work arounds are just over the
top though. E-Z-est solution is just to use:

     ..
     i:=-10+j;
     ..

This doesn't violate range conditions, so you can compile away with {$R+}
to your hearts content.

More importantly though, this is NOT a compiler error. It's actually documented
in the (TP5.5 at least) manuals, but god knows where!


     /\/\       Paul Gallagher, PC Support Officer,
    / / /\      Computer Systems Group,
   / / /  \     BHP Melbourne Research Laboratories
  / / / /\ \    245 Wellington Rd Mulgrave Vic 3170 AUSTRALIA
  \ \/ / / /    Phone : +61-3-560-7066,  Fax : +61-3-561-6709
   \  / / /     ACSnet  :  paulg@bhpmrl.OZ.AU        
    \/\/\/