Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #34584

Re: Why I like Forth

From "Rod Pemberton" <buzzoff@nonamewhichexists.cmm>
Newsgroups comp.lang.forth
Subject Re: Why I like Forth
Date 2014-11-23 14:33 -0500
Organization Aioe.org NNTP Server
Message-ID <op.xpslx7ermk81un@localhost> (permalink)
References <87d988fc-26c2-492c-8e51-f0ede5758a69@googlegroups.com>

Show all headers | View raw


On Sun, 23 Nov 2014 05:45:12 -0500, <mhx@iae.nl> wrote:

> $ echo "int main (void) { char a=-128; return printf("%d",abs(a)); }" >  
> a.c; gcc a.c 2&>err; ./a
> 128

This C code is NOT the same as the Forth code ...

> I do understand this result and that of C [...]

Do you?  Do you truly understand the C result?

1) You used the stack in Forth to solve the problem.  You didn't use
VARIABLE in Forth to create a variable, nor did you create a stack
in C to use a stack in C.  I.e., these examples aren't the same, but
are similar.

2) You didn't bitwise-and '&' the variable 'a' with 0xFF, i.e.,
abs(a&0xFF), as you did in your Forth examples.  If you had done
so, or if you correct your code to do so, there will be two errors
in C:

2a) abs() takes an 'int'.  Without a cast to 'int', C does a type
conversion from 'char' to 'int', sometimes referred to as an
"implicit cast".  This conversion is "value preserving".  For signed
values, that means there is a representation change since one bit of
the integer is used for the sign.  The position of the sign-bit will
move due to the conversion, since the sizeof(char) in bits must be at
least 8-bits while sizeof(int) in bits must be at least 16-bits.
I.e., if sizeof(char) in bits is 8-bits and sizeof(int) in bits is
16-bits, then the integer's sign-bit would move from bit 7 to bit 15
to preserve the signed integer value, and similarly to bit 31 for
32-bits.  This is not a problem with your code as it is.  But, it is
a problem if you were to correct your code to bitwise-and the variable
'a' with 0xFF, as you did in your Forth examples.  The 0xFF would
clear out the sign-bit in ALL cases prior to the abs() call, i.e.,
abs() would not do anything since it would always be passed an
unsigned value.

2b) You've assumed the sizeof(char) is 8-bits, hence the $FF AND in
the Forth examples.  The sizeof(char) in bits for C may be larger
than 8-bits.  8-bits is a minimum without any maximum.  If you fix
your code to bitwise-and 'a' with 0xFF to match what you did in the
Forth examples, then the code would clear the sign-bit for char's
of 9-bits, 10-bits, 11-bits, ... etc, but would preserve the sign-bit
for 8-bit chars.  I.e., it would work for 8-bits, but fail for larger
bit sizes.  You didn't do the bitwise-and in C, which is good.  But,
the C code is no longer the functionally the same as the Forth code.

So, your C code is correct, but it doesn't match the Forth.
The correct way to match the Forth is to create an array of
int's in C and use it as a stack.


Rod Pemberton

Back to comp.lang.forth | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Why I like Forth mhx@iae.nl - 2014-11-23 02:45 -0800
  Re: Why I like Forth anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2014-11-23 13:29 +0000
    Re: Why I like Forth mhx@iae.nl - 2014-11-23 06:58 -0800
      Re: Why I like Forth anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2014-11-23 15:28 +0000
        Re: Why I like Forth Julian Fondren <julian.fondren@gmail.com> - 2014-11-23 09:55 -0800
  Re: Why I like Forth "Rod Pemberton" <buzzoff@nonamewhichexists.cmm> - 2014-11-23 14:33 -0500
    Re: Why I like Forth Julian Fondren <julian.fondren@gmail.com> - 2014-11-23 12:35 -0800
      Re: Why I like Forth mhx@iae.nl - 2014-11-23 13:23 -0800

csiph-web