Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.forth > #34570 > unrolled thread
| Started by | mhx@iae.nl |
|---|---|
| First post | 2014-11-23 02:45 -0800 |
| Last post | 2014-11-23 13:23 -0800 |
| Articles | 8 — 4 participants |
Back to article view | Back to comp.lang.forth
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
| From | mhx@iae.nl |
|---|---|
| Date | 2014-11-23 02:45 -0800 |
| Subject | Why I like Forth |
| Message-ID | <87d988fc-26c2-492c-8e51-f0ede5758a69@googlegroups.com> |
========================================
Scilab 5.5.0 (Apr 11 2014, 12:13:09)
-->abs(int8(-128))
ans =
-128
========================================
marcel@me-tarzan ~
$ echo "int main (void) { char a=-128; return printf("%d",abs(a)); }" > a.c; gcc a.c 2&>err; ./a
128
========================================
iForth version 4.0.1782, generated 21:06:07, March 6, 2014.
x86_64 binary, native floating-point, extended precision.
Copyright 1996 - 2014 Marcel Hendrix.
FORTH> -128 $FF AND abs . 128 ok
========================================
32bit Forth for Windows 95, 98, and NT
Compiled: October 18th, 2006, 0:19am
Version: 4.2 Build: 0545 Rebuilt by User
Platform: Windows NT Version: 6.1 Build: 7601
-128 $ff and .s abs . [1] 128 128 ok
========================================
Gforth 0.7.0, Copyright (C) 1995-2008 Free Software Foundation, Inc.
Gforth comes with ABSOLUTELY NO WARRANTY; for details type `license'
Type `bye' to exit
-128 $ff and .s abs . <1> 128 128 ok
========================================
[toc] | [next] | [standalone]
| From | anton@mips.complang.tuwien.ac.at (Anton Ertl) |
|---|---|
| Date | 2014-11-23 13:29 +0000 |
| Message-ID | <2014Nov23.142910@mips.complang.tuwien.ac.at> |
| In reply to | #34570 |
mhx@iae.nl writes:
>========================================
>Scilab 5.5.0 (Apr 11 2014, 12:13:09)
>
>-->abs(int8(-128))
> ans =
>
> -128
>========================================
>marcel@me-tarzan ~
>$ echo "int main (void) { char a=-128; return printf("%d",abs(a)); }" > a.c; gcc a.c 2&>err; ./a
>128
>========================================
>iForth version 4.0.1782, generated 21:06:07, March 6, 2014.
>x86_64 binary, native floating-point, extended precision.
>Copyright 1996 - 2014 Marcel Hendrix.
>
>FORTH> -128 $FF AND abs . 128 ok
>========================================
And your point is?
ABS does not always produce a "positive" number even in Forth (and C):
-9223372036854775808 abs . -9223372036854775808 ok
However, one can consider "ABS ." to be a type error. If you improve
this to
-9223372036854775808 abs u.
you get the right result (on a 64-bit system). The difference in type
definitions between Forth and C is interesting, though:
|6.1.0690 ABS abs CORE ( n -- u )
vs.
|int abs(int j);
|Trying to take the absolute value of the most negative integer is not
|defined.
- anton
--
M. Anton Ertl http://www.complang.tuwien.ac.at/anton/home.html
comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
New standard: http://www.forth200x.org/forth200x.html
EuroForth 2014: http://www.euroforth.org/ef14/
[toc] | [prev] | [next] | [standalone]
| From | mhx@iae.nl |
|---|---|
| Date | 2014-11-23 06:58 -0800 |
| Message-ID | <d29afd9a-e5c0-41ff-bea3-f4cce9f0359c@googlegroups.com> |
| In reply to | #34573 |
On Sunday, November 23, 2014 2:39:09 PM UTC+1, Anton Ertl wrote: > mhx@iae.nl writes: > >======================================== > >Scilab 5.5.0 (Apr 11 2014, 12:13:09) > > > >-->abs(int8(-128)) > > ans = > > > > -128 [..] > And your point is? On MATLAB R2014a: >> abs(int8(-128)) ans = 127 I do understand this result and that of C and all those Forths, but not Scilab's (it seems earlier MATLABs also say -128). -marcel
[toc] | [prev] | [next] | [standalone]
| From | anton@mips.complang.tuwien.ac.at (Anton Ertl) |
|---|---|
| Date | 2014-11-23 15:28 +0000 |
| Message-ID | <2014Nov23.162843@mips.complang.tuwien.ac.at> |
| In reply to | #34578 |
mhx@iae.nl writes:
>On Sunday, November 23, 2014 2:39:09 PM UTC+1, Anton Ertl wrote:
>> mhx@iae.nl writes:
>> >========================================
>> >Scilab 5.5.0 (Apr 11 2014, 12:13:09)
>> >
>> >-->abs(int8(-128))
>> > ans =
>> >
>> > -128
>[..]
>> And your point is?
>
>On MATLAB R2014a:
>
>>> abs(int8(-128))
>ans =
> 127
>
>I do understand this result and that of C and all those Forths, but not Scilab's (it seems earlier MATLABs also say -128).
My guess for the -128 result is that abs is an int8->int8 function
there, and the result corresponds to "-9223372036854775808 abs" on
64-bit signed integers. I.e., int8(-128) is $80, and the result of
abs(int8(-128)) is $80 as well, and that is output as -128.
The 127 result is more surprising to me. I guess it is coming from
saturation arithmetics, and it's surprising, because I rarely
encounter saturation arithmetics.
- anton
--
M. Anton Ertl http://www.complang.tuwien.ac.at/anton/home.html
comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
New standard: http://www.forth200x.org/forth200x.html
EuroForth 2014: http://www.euroforth.org/ef14/
[toc] | [prev] | [next] | [standalone]
| From | Julian Fondren <julian.fondren@gmail.com> |
|---|---|
| Date | 2014-11-23 09:55 -0800 |
| Message-ID | <b1b55173-bc60-4565-9279-3dcb5948c10d@googlegroups.com> |
| In reply to | #34579 |
On Sunday, November 23, 2014 4:45:13 AM UTC-6, m...@iae.nl wrote:
> ========================================
> Scilab 5.5.0 (Apr 11 2014, 12:13:09)
>
> -->abs(int8(-128))
> ans =
>
> -128
> ========================================
> marcel@me-tarzan ~
> $ echo "int main (void) { char a=-128; return printf("%d",abs(a)); }" > a.c; gcc a.c 2&>err; ./a
> 128
> ========================================
> iForth version 4.0.1782, generated 21:06:07, March 6, 2014.
> x86_64 binary, native floating-point, extended precision.
> Copyright 1996 - 2014 Marcel Hendrix.
>
> FORTH> -128 $FF AND abs . 128 ok
The focus seems to be on ABS , but what strikes me is . (dot) and
printf("%d",...), versus what I take as an attempt by Scilab to
display a signed two's-complement int8, rather than whatever the
int8's bits look like as a 64-bit or whatever number. It has
a typeful REPL, and 'int8' is one of its types, in other words.
So:
: int8 ( n -- 8-bit-n ) $FF and ;
: .8 ( n -- )
$FF and dup $80 and if 1- invert $FF and 0 <# #s [char] - hold #> type space
else . then ;
-128 int8 abs .8 -128 ok
$FF int8 abs .8 -1 ok
: signed-int8s ( -- ) 256 0 do cr i .8 loop ;
signed-int8s
0
1
2
3
...
125
126
127
-128
-127
-126
...
-3
-2
-1 ok
-- Julian
[toc] | [prev] | [next] | [standalone]
| From | "Rod Pemberton" <buzzoff@nonamewhichexists.cmm> |
|---|---|
| Date | 2014-11-23 14:33 -0500 |
| Message-ID | <op.xpslx7ermk81un@localhost> |
| In reply to | #34570 |
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
[toc] | [prev] | [next] | [standalone]
| From | Julian Fondren <julian.fondren@gmail.com> |
|---|---|
| Date | 2014-11-23 12:35 -0800 |
| Message-ID | <dd71d4c3-9200-43f8-bb37-a7f7410117f6@googlegroups.com> |
| In reply to | #34584 |
More to like about Forth: Rod's 2b would translate to shrieking
about how some code didn't have textual boilerplate. Criticism
that is 'pedantic', of a body of C code, becomes 'willfully crazy
ranting' when applied to Forth. "Where are you declaring your
ENVIRONMENTAL DEPENDENCIES?!?! Where?! I don't see them! If
c@ fetches a 128-bit value then your code IS BROKEN!"
But, back to ABS vs. printf("%d\n",...) and . (dot): I figure
the 127 result is due to some carefully int8 understanding of
ABS.
-- Julian
[toc] | [prev] | [next] | [standalone]
| From | mhx@iae.nl |
|---|---|
| Date | 2014-11-23 13:23 -0800 |
| Message-ID | <15224092-20ae-4119-8f7e-660636af2460@googlegroups.com> |
| In reply to | #34585 |
On Sunday, November 23, 2014 9:35:04 PM UTC+1, Julian Fondren wrote:
[..]
> But, back to ABS vs. printf("%d\n",...) and . (dot): I figure
> the 127 result is due to some carefully int8 understanding of
> ABS.
>> help int8
int8 Convert to signed 8-bit integer.
I = int8(X) converts the elements of the array X into signed 8-bit
integers. X can be any numeric object, such as a DOUBLE. The values
of an int8 range from -128 to 127, or INTMIN('int8') to INTMAX('int8').
Values outside this range saturate on overflow, namely they are mapped
to -128 or 127 if they are outside the range.
>> int8(-129)
ans =
-128
>> int8(+129)
ans =
127
>> abs(int8(+129))
ans =
127
***** I understand this.
>> help abs
abs Absolute value.
abs(X) is the absolute value of the elements of X.
***** I understand this.
>> abs(-129)
ans =
129
>> x=int8(-129)
x =
-128
>> whos x
Name Size Bytes Class Attributes
x 1x1 1 int8
>> abs(x)
ans =
127
\ ***** <Light goes on> -128 is a valid int8. ABS makes it +128, which
\ ***** isn't a valid positive int8, and therefore MATLAB saturates it
\ ***** to +127.
Scilab (a Matlab clone) is wrong.
But I stand by the title of this thread :-)
-marcel
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.forth
csiph-web