Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!csd4.milw.wisc.edu!indri!uflorida!haven!umbc3!mbph!hybl From: hybl@mbph.UUCP (Albert Hybl Dept of Biophysics SM) Newsgroups: comp.lang.fortran Subject: Re: Character aliases are Satanic extensions Summary: A nutritious way to read/write character data to and from a real array Keywords: Using SAP inclusions Message-ID: <600@mbph.UUCP> Date: 31 May 89 17:50:43 GMT References: <598@mbph.UUCP> <599@mbph.UUCP> Organization: University of Maryland, School of Medicine, Baltimore, MD 21201 Lines: 77 In article <1515@hudson.acc.virginia.edu> from gl8f@bessel.acc.Virginia.EDU (Greg Lindahl), "A challenge" >If you can come up with an ANSI-Standard Approved Nutritious Scheme for >writing character data into binary files with real numbers, or putting >character data into commons with other data, let me know. ... ]khb%chiba@Sun.COM (Keith Bierman - SPD Languages Marketing -- MTS) ]in message <105811@sun.Eng.Sun.COM> responds: ]?? this is perfectly allowed; viz. ] write(10) character*10,real*8,character*10 ]is legal in x3.9-1978. ] ], or putting ]>character data into commons with other data, let me know ] ]this (and equivalence) is what was disallowed. ] From the porting trenches, let me ask: can we read/write character information to and from a real array? First, let's answer: WHY DO IT? Scientist use names for the molecules with which they're working; they attach names to the residues from which the molecules are built; they assign names to the the atoms from which the residues are built; they identify the atom type with a atomic symbol. Because of the unpredictable size of a problem, the authors of the programs I am porting implemented a dynamic storage scheme that uses two large arrays--one REAL*4 and the other INTEGER*2. Since many of the names are four characters long, they were stashed into the REAL*4 array along with the atomic coordinates and all the other real REAL parameters. Ok, now lets see how this can be done without rewriting the dynamic storage scheme and without choking a standards conforming compiler. Let's try: CHARACTER*4 NAME REAL X(10) NAME = 'Na+ ' OPEN (UNIT=10,FORM='UNFORMATTED',FILE='F10',STATUS='NEW') WRITE (UNIT=10) NAME REWIND (UNIT=10) READ (UNIT=10) X(4) WRITE (*,FMT='(1X,A4)') X(4) ! Probably not standard and * is used here only for testing. CLOSE (UNIT=10,STATUS='DELETE') END This is, of course, an over simplified example. The binary write could be in a different program unit or a different program from the unit that does the binary read. The binary file is not portable between differing hardware types but is the source code? My SVS-fortran compiler think it's ok. The binary WRITE has expunged the character identification from the variable NAME; it becomes just a bit string. If the length of the bit string is compatible with the length of a REAL*4 variable, the READ binary stashes the bits into the REAL array element. -- We did it -- Right ??? .... Maybe not--from a private communication, I learned that there exists hardware specifically designed for floating point operations. Certain floating point registers will immediately normalize the values loaded into them because that is what they were designed to do. NORMALIZATION OF A REAL VARIABLE CONTAINING STASHED CHARACTER INFORMATION CAN DESTROY THE CHARACTER DATA! The FORTRAN compiler can be written to suppress the use of these registers when no floating point calculations are specified thus allowing a programmer to move character data around by using REAL or DOUBLE PRECISION variables. But will the next compiler you use be so thoughtful? The moral is: DON'T USE CHARACTER ALIASES! ---------------------------------------------------------------------- Albert Hybl, PhD. Office UUCP: uunet!mimsy!mbph!hybl Department of Biophysics Home UUCP: uunet!mimsy!mbph!hybl!ah University of Maryland CoSy: ahybl School of Medicine Baltimore, MD 21201 Phone: (301) 328-7940 (Office) ----------------------------------------------------------------------