home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / fortran / 2870 < prev    next >
Encoding:
Text File  |  1992-07-29  |  4.9 KB  |  162 lines

  1. Path: sparky!uunet!usc!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!linac!unixhub!slacvx.slac.stanford.edu!fairfield
  2. From: fairfield@slacvx.slac.stanford.edu
  3. Newsgroups: comp.lang.fortran
  4. Subject: Re: Question for C ==> Fortran
  5. Message-ID: <1992Jul29.134901.1@slacvx.slac.stanford.edu>
  6. Date: 29 Jul 92 21:49:01 GMT
  7. References: <1992Jul28.075514.28683@debbie.cc.nctu.edu.tw>
  8. Sender: news@unixhub.SLAC.Stanford.EDU
  9. Organization: Stanford Linear Accelerator Center
  10. Lines: 149
  11. Nntp-Posting-Host: slacvx.slac.stanford.edu
  12.  
  13. Sorry to post to the net; the MAIL message I sent bounced...  Also, please
  14. don't flame me for use VAX/VMS extensions, etc., the poster said s/he was
  15. using a VAX ;-)
  16.  
  17. In article <1992Jul28.075514.28683@debbie.cc.nctu.edu.tw>, linsbin@cc.nctu.edu.tw (Lin shyh bin) writes:
  18. >    Hi, I am unfamiliar to FORTRAN, but I want to write a FORTRAN program as
  19. >
  20. >like the follow C program.
  21. >
  22. >    Could anyone tell me how to transfer it on VAX FORTRAN ?
  23. >
  24. >    #include <stdio.h>
  25. >    #include <math.h>
  26. >
  27. >    #define  MASK 0x1f800000
  28. >
  29. >    union dandi {
  30. >          int     i[2];
  31. >          double  d;
  32. >    } di;
  33. >
  34. >    main()
  35. >    {
  36. >     float  f;
  37. >     double d;
  38. >
  39. >     for (f=0 ; f<1 ; f+=0.000001) {
  40. >         d = (double) f;
  41. >         di.d = sin(d);
  42. >         if ((di.i[1] & MASK) == 0) printf("\n Sine in hex : %x", di.i[1]);
  43. >     }
  44. >    }
  45. >
  46. >
  47. >    Thanks in advance !!
  48.  
  49.     This is straight-forward in VMS Fortran, since VMS Fortran supports
  50. hexadecimal (as well as octal and binary) constants.  Also, VMS Fortran
  51. supports UNIONs within STRUCTUREs, which should be transparent to a C
  52. programmer.  However, I'll give an example using EQUIVALENCE, first, since 
  53. that is much more standard (the STRUCTURE stuff is part of Fortran 90, but
  54. F90 uses a different syntax than VMS Fortran, so let's not confuse the
  55. issue...)
  56.  
  57.     The fragment of code above would translate as (note that the exclamation
  58. point is a comment delimitor in VMS Fortran):
  59.  
  60.         PROGRAM TEST
  61.  
  62.     IMPLICIT NONE    ! Force all variables to be explicitly declared
  63.  
  64.         INTEGER    MASK
  65.         PARAMETER (MASK='1F800000'X)    ! You can use the PARAMETER statement
  66.  
  67.         INTEGER             II(2)
  68.         DOUBLE PRECISION    DI
  69.         EQUIVALENCE        (II,DI)
  70.  
  71.         REAL                F
  72.         DOUBLE PRECISION    D
  73.  
  74.  
  75.         DO F=0.0, 0.999999, 0.000001
  76.           D  = DBLE(F)          ! "Cast", rather convert, F to a double.
  77.           DI = SIN(D)           ! Compiler automatically chooses the double-
  78.                                 ! precision version of the SIN function.
  79.           IF ((II(2) .AND. MASK) .EQ. 0)  ! Fortran indices start at 1, not 0.
  80.      >      WRITE (6,100) II(2)
  81.         ENDDO
  82. 100     FORMAT (' '/,' Sine in hex :', Z8.8)
  83.  
  84.         CALL EXIT
  85.         END
  86.  
  87. The "CALL EXIT" at the bottom is optional (more-or-less). A "STOP" statement
  88. would do just as well.  Also, there are a variety of ways of doing the FORMAT
  89. statement; I often write it as an explicit string in the WRITE statement thus
  90. doing away with the label 100 and the FORMAT statement altogether.  By the
  91. way, using non-integer loop variables is generally to be dicouraged due to
  92. possible round-off errors in calculating the number of trips through the loop. 
  93.  
  94. Here's an alternative coding, just by way of example:
  95.  
  96.         PROGRAM TEST1
  97.  
  98.         IMPLICIT NONE   ! Force all variables to be explicitly declared
  99.  
  100.         STRUCTURE /SS/
  101.           UNION
  102.             MAP
  103.               INTEGER*4 I(2)
  104.             END MAP
  105.             MAP
  106.               REAL*8    D(2)
  107.             END MAP
  108.           END UNION
  109.         END STRUCTURE
  110.  
  111.         RECORD /SS/ DI
  112.  
  113.         INTEGER*4 I
  114.         REAL*4    F
  115.         REAL*8    D
  116.  
  117.         DO I=1,100000
  118.           F = (I-1)*0.000001
  119.           D = F                 ! Compiler "doubles" F automatically
  120.           DI.D = SIN(D)
  121.  
  122.           IF ((DI.I(2).AND.'1F800000'X) .EQ. 0)
  123.      >      WRITE (6,'('' '',/,'' Sine in hex :'', Z8.8)') DI.I(2)
  124.         ENDDO
  125.  
  126.         STOP
  127.         END
  128.  
  129.  
  130. The shortest version might be:
  131.  
  132.         PROGRAM TEST3
  133.  
  134.         INTEGER             II(2), I
  135.         DOUBLE PRECISION    DI
  136.         EQUIVALENCE        (II,DI)
  137.  
  138.         DO I=1,100000
  139.           DI = SIN(DBLE(FLOAT(I-1)*0.000001))
  140.           IF ((II(2).AND.'1F800000'X) .EQ. 0)
  141.      >      WRITE (6,'('' '',/,'' Sine in hex :'', Z8.8)') II(2)
  142.         ENDDO
  143.  
  144.         STOP
  145.         END
  146.  
  147.  
  148. Note that in TEST2 and TEST3, the explicit format in the WRITE statement
  149. uses doubled singled-quote marks to get an explicit single-quote in the
  150. quoted string.  Also note that the formate Z8.8 prints the number in hex
  151. with leading zeros; "Z" without "8.8" would not print the leading zeros.
  152.  
  153.     Questions anyone? :-)
  154.  
  155.         Cheers, Ken
  156. --
  157.  Dr. Kenneth H. Fairfield    |  Internet: Fairfield@Slacvx.Slac.Stanford.Edu
  158.  SLAC, P.O.Box 4349, MS 98   |  DECnet:   45537::FAIRFIELD (45537=SLACVX)
  159.  Stanford, CA   94309        |  BITNET    Fairfield@Slacvx
  160.  ----------------------------------------------------------------------------
  161.  These opinions are mine, not SLAC's, Stanford's, nor the DOE's...
  162.