fortran_fortran simple math - wikibooks, open books for an open world

2
Fortran/Fortran Simple math Part of the Fortran WikiBook Fortran has the following arithmetic operators: + addition subtraction * multiplication / division ** exponentiation (associates righttoleft) Here are some examples of their use: i = 2 + 3 ! sets i equal to 5 i = 2 * 3 ! sets i equal to 6 i = 2 / 3 ! sets i equal to 0, since 2/3 is rounded down to the integer 0, see mixed mode x = 2 / 3.0 ! sets x approximately equal to 2/3 or 0.666667 i = 2 ** 3 ! sets i equal to 2*2*2 = 8 Fortran has a wide range of functions useful in numerical work, such as sin, exp, and log. The argument of a function must have the proper type, and it is enclosed in parentheses: x = sin(3.14159) ! sets x equal to sin(pi), which is zero The intrinsic math functions of Fortran are elemental, meaning that they can take arrays as well as scalars as arguments and return a scalar or an array of the same shape: real :: x(2),pi=3.14159 x = sin((/pi,pi/2/)) The above program fragment sets the two elements of array x, x(1) and x(2), equal to sin(pi) and sin(pi/2) respectively. Retrieved from "http://en.wikibooks.org/w/index.php? title=Fortran/Fortran_Simple_math&oldid=2050329" This page was last modified on 19 February 2011, at 01:34. Text is available under the Creative Commons AttributionShareAlike License.; additional terms may apply. By using this site, you agree to the Terms of Use and Privacy Policy.

Upload: goce-vasilevski

Post on 16-Nov-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

  • 3/23/2015 Fortran/FortranSimplemathWikibooks,openbooksforanopenworld

    http://en.wikibooks.org/wiki/Fortran/Fortran_Simple_math 1/2

    Fortran/FortranSimplemath

    PartoftheFortranWikiBook

    Fortranhasthefollowingarithmeticoperators:

    +additionsubtraction*multiplication/division**exponentiation(associatesrighttoleft)

    Herearesomeexamplesoftheiruse:

    i=2+3!setsiequalto5i=2*3!setsiequalto6i=2/3!setsiequalto0,since2/3isroundeddowntotheinteger0,seemixedmodex=2/3.0!setsxapproximatelyequalto2/3or0.666667i=2**3!setsiequalto2*2*2=8

    Fortranhasawiderangeoffunctionsusefulinnumericalwork,suchassin,exp,andlog.Theargumentofafunctionmusthavethepropertype,anditisenclosedinparentheses:

    x=sin(3.14159)!setsxequaltosin(pi),whichiszero

    TheintrinsicmathfunctionsofFortranareelemental,meaningthattheycantakearraysaswellasscalarsasargumentsandreturnascalaroranarrayofthesameshape:

    real::x(2),pi=3.14159x=sin((/pi,pi/2/))

    Theaboveprogramfragmentsetsthetwoelementsofarrayx,x(1)andx(2),equaltosin(pi)andsin(pi/2)respectively.

    Retrievedfrom"http://en.wikibooks.org/w/index.php?title=Fortran/Fortran_Simple_math&oldid=2050329"

    Thispagewaslastmodifiedon19February2011,at01:34.TextisavailableundertheCreativeCommonsAttributionShareAlikeLicense.additionaltermsmayapply.Byusingthissite,youagreetotheTermsofUseandPrivacyPolicy.

    http://en.wikibooks.org/w/index.php?title=Fortran/Fortran_Simple_math&oldid=2050329http://creativecommons.org/licenses/by-sa/3.0/http://wikimediafoundation.org/wiki/Privacy_policyhttp://wikimediafoundation.org/wiki/Terms_of_Usehttp://en.wikibooks.org/wiki/Fortran

  • 3/23/2015 Fortran/FortranSimplemathWikibooks,openbooksforanopenworld

    http://en.wikibooks.org/wiki/Fortran/Fortran_Simple_math 2/2