G
Guest
I am collecting a sort of phrase books with equivalent "things" in
various languages.
I am now considering the indication of constants as hex, oct or binary
constants. I have considered fortran, C, Java, IDL and (my)sql. I have a
question on the first three.
As a reference I started in Fortran with cases like these (I did also
byte, short and 64bit integers, not shown) :
integer j
j=somevalue
write(*,3)j,j,j,j
3 format(I11.10,1X,B32.32,1X,O11.11,1X,Z8.8)
real r
r=somevalue
write(*,4)r,r,r,r
4 format(G13.7 ,1X,B32.32,1X,O11.11,1X,Z8.8)
character*4 s
equivalence (j,s)
s=somestring
write(*,5)s,s,s,s
5 format(A4 ,1X,B32.32,1X,O11.11,1X,Z8.8)
I tried first with some explicit values (1, -1, 32767, 1.0, 2.0,
huge(0.0), tiny(0.0), "ABCD") and obtained perfectly meaningful binary,
oct and hex values. Then I replaced the assignment using bin, oct or hex
constants
j=z'7FFE' ! gives 32866
j=o'37777777776' ! give -2
j=b'11111111111111111111111111111110'
r=z'80800000' ! gives tiny
j=z'44434241' ! gives "ABCD"
Everything works perfectly. I can assign directly a binary
representation to an integer and to a real, and I can even assign it to
a string if this is in equivalence with an appropriate integer.
I then moved to C, I tried statements like
short i ;
i=somevalue;
printf("%6.5d %6.6o %4.4X %d \n",i,i,i,sizeof(i)) ;
int j ;
j=somevalue ;
printf("%11.10d %11.11o %8.8X %d \n",j,j,j,sizeof(j)) ;
float f ;
f=somevalue ;
printf("%13.7g %11.11o %8.8X %d \n",f,f,f,sizeof(f)) ;
Here I find two problems, a minor and a major one
The minor one is that a positive short is displayed OK
But a negative short is displayed with too many oct and hex digits
i=32767 ; /* 32767 077777 7FFF */
i=-1; /* -00001 37777777777 FFFFFFFF */
although a sizeof says that i is 2 bytes
The major problem is that an assignment to a float gives an
unexpected result (no compile or runtime error)
f=0X40800000; /* should return 4.0 */
f=0X80800000; /* should return tiny */
For instance the latter returns 2.155872e+09, the former 2.155872e+09
and BOTH THE SAME 00000000000 as octal and 41E01000 as hex (different
from the one I assigned)
In Java I tried
int i ;
System.out.format("i is: %11d %11o %8x %n", i,i,i);
float f ;
System.out.format("f is: %e %n", f );
Here format does not support the o and x descriptors for float argument.
Anyhow I tested assignments for integer and they do work
i=037777777776 ; /* -2 */
i=0X7FFE ; /* 32766 */
while assignment for real do not work (as in C)
f=0X3F800000; /* should be 1.0 */
f=0X40800000; /* should be 4.0 */
f=0X80800000; /* should by tiny */
(the compiler does not complain, there are no runtime exceptions, but
the result is not what expected
for instance the latter returns -2.139095e+09 and the one for 4.0
returns 1.082130e+09
I haven't tested assignment of hex or oct constants to strings in
neither of the two latter languages (I guess Java will be using unicode,
not plain ascii)
various languages.
I am now considering the indication of constants as hex, oct or binary
constants. I have considered fortran, C, Java, IDL and (my)sql. I have a
question on the first three.
As a reference I started in Fortran with cases like these (I did also
byte, short and 64bit integers, not shown) :
integer j
j=somevalue
write(*,3)j,j,j,j
3 format(I11.10,1X,B32.32,1X,O11.11,1X,Z8.8)
real r
r=somevalue
write(*,4)r,r,r,r
4 format(G13.7 ,1X,B32.32,1X,O11.11,1X,Z8.8)
character*4 s
equivalence (j,s)
s=somestring
write(*,5)s,s,s,s
5 format(A4 ,1X,B32.32,1X,O11.11,1X,Z8.8)
I tried first with some explicit values (1, -1, 32767, 1.0, 2.0,
huge(0.0), tiny(0.0), "ABCD") and obtained perfectly meaningful binary,
oct and hex values. Then I replaced the assignment using bin, oct or hex
constants
j=z'7FFE' ! gives 32866
j=o'37777777776' ! give -2
j=b'11111111111111111111111111111110'
r=z'80800000' ! gives tiny
j=z'44434241' ! gives "ABCD"
Everything works perfectly. I can assign directly a binary
representation to an integer and to a real, and I can even assign it to
a string if this is in equivalence with an appropriate integer.
I then moved to C, I tried statements like
short i ;
i=somevalue;
printf("%6.5d %6.6o %4.4X %d \n",i,i,i,sizeof(i)) ;
int j ;
j=somevalue ;
printf("%11.10d %11.11o %8.8X %d \n",j,j,j,sizeof(j)) ;
float f ;
f=somevalue ;
printf("%13.7g %11.11o %8.8X %d \n",f,f,f,sizeof(f)) ;
Here I find two problems, a minor and a major one
The minor one is that a positive short is displayed OK
But a negative short is displayed with too many oct and hex digits
i=32767 ; /* 32767 077777 7FFF */
i=-1; /* -00001 37777777777 FFFFFFFF */
although a sizeof says that i is 2 bytes
The major problem is that an assignment to a float gives an
unexpected result (no compile or runtime error)
f=0X40800000; /* should return 4.0 */
f=0X80800000; /* should return tiny */
For instance the latter returns 2.155872e+09, the former 2.155872e+09
and BOTH THE SAME 00000000000 as octal and 41E01000 as hex (different
from the one I assigned)
In Java I tried
int i ;
System.out.format("i is: %11d %11o %8x %n", i,i,i);
float f ;
System.out.format("f is: %e %n", f );
Here format does not support the o and x descriptors for float argument.
Anyhow I tested assignments for integer and they do work
i=037777777776 ; /* -2 */
i=0X7FFE ; /* 32766 */
while assignment for real do not work (as in C)
f=0X3F800000; /* should be 1.0 */
f=0X40800000; /* should be 4.0 */
f=0X80800000; /* should by tiny */
(the compiler does not complain, there are no runtime exceptions, but
the result is not what expected
for instance the latter returns -2.139095e+09 and the one for 4.0
returns 1.082130e+09
I haven't tested assignment of hex or oct constants to strings in
neither of the two latter languages (I guess Java will be using unicode,
not plain ascii)