201-203 Hfd 11 Math Functions

Embed Size (px)

Citation preview

  • 8/8/2019 201-203 Hfd 11 Math Functions

    1/3

    11 Math Functions

    11 MathFunctions

    "Math functions" include the following:> Sine, cosine, tangent> Arc sine, arc cosine, arc tangent> Squaring, square-root extraction> Exponential function to base e,natural logarithm ,All math functions process numbers in data for-mat REAL. Depending on the result, a mathfunction sets status bits CCO, CC1, OV and OSas described in Chapter 15 "Status Bits".The statements for the STL programming lan-guage are described in this chapter. In the SCLprogramming language, the math functions areincluded among the SCL standard functions(Chapter 30.3 "Math Functions").The examples in this chapter arealso on the dis-kette which accompanies the book in the STL_Book library under the "Digital Functions" pro-gram in function block FB 111 or source fileChap_ll.

    11.1 Processinga MathFunctionA math function takes the number in accumula-tor 1 as input value for the function to be exe-cuted and stores the result in accumulator 1.You program a math function according to thefollowing general schematic:

    LoadMath functionTransferAddress; i ' i 'iResult;

    A math function alters only the contents of ac-cumulator 1; the contents of all other accumu-lators remain unchanged. A math function isexecuted without regard to any conditions.Table 11.1 shows three examples of math func-tions. A math function computes in accordancewith the rules governing REAL numbers, evenwhen absolute addressing is used and no datatypes are declared.If accumulator 1 contains an invalid REALnumber at the time the function is executed, themath function returns an invalid REAL numberand sets status bits CCO, CC1, OV and OS to461

    Table 11.1 Examples of Math FunctionsSine

    Squareroot

    Exponent

    The value in memory doubleword MD 110 con-tains an angle in radian measure. The sine of thisangle is generated and stored in memory dou-bleword MD 104.The square root of the value in variable"MathValuel" is generated and stored in thevariable "MathRoot".Variable #Result contains the power of e and#Exponent.

    L MD 110;SIN ;T MD 104;L " Global_DB".MathValuel;SQRT;T " Global_DB".MathRoot;L #Exponent;EX P ;T #Result;

    20 1

  • 8/8/2019 201-203 Hfd 11 Math Functions

    2/3

    11 Math Functions

    11.2 Trigonometric Functions 11.3 ArcFunctionsThe trigonometric functionst> SIN Sine,D > COS Cosine and> TAN Tangentassume an angle in radian measure in form, of aREAL number in accumulator 1.Two units are normally used for the size of anangle: degrees from 0 to 360 and radian mea-sure from 0 to I T T (where T T = +3.141593e+00).Both can be converted proportionally. For ex-ample, the radian measure for a 90 angle is I T /2 or+1.570796e+00.With values greater than 2ir (+6.283185e+00),I T T or a multiple thereof is subtracted until theinput value for the trigonometric function isless than I T T .Example:Computing the idle powerPs = U x I x sin(cp)LSINL*RL*RT

    PHI;

    Current;

    Voltage;

    I_Power;Please note that the angle must be specified inradian measure. If an angle is available in de-grees, you must multiply it by the factor77/180 =+1.745329e-02before you can process it with a trigonometricfunction.Table 11.2 Value Ranges for Arc FunctionsFunctionASINACOSATAN

    Permissible ValueRange-1 to +1-1 to +1Entire range

    Value Returned-IT/2 to +7T/2O t O T T-7T/2 to +7T/2

    The arc functions (inverse trigonometric func-tions)> ASIN Arc sine,> ACOS Arc cosine and> ATAN Arc tangentare the inverse functions of the correspondingtrigonometric functions. They assume a REALnumber in a specific range in accumulator 1,and return an angle in radian measure (Table11.2).If the permissible range is exceeded, the arcfunction returns an invalid REAL number andsets status bits CCO, CC1, OV and OS to "1".Example: In a right-angled triangle, one of theshort sides of the triangle and the hypotenuseform an aspect ratio of 0.343. How big is theangle between them in degrees?Arcsin (0.343) returns the angle in radianmeasure; multiplication with factor 360/277 (=57.2958) gives you the angle in degrees(approx. 20).L 0.343;ASIN ;L 57.2958;*RT Angle_Degree;

    11.4 Other MathFunctionsThe following math functions are also available> SQR Squaring,> SQRT Square-root extraction,> EXP Exponential function to base e and> LN Compute natural logarithm

    (logarithm to base e).

    20 2

  • 8/8/2019 201-203 Hfd 11 Math Functions

    3/3

    11.4 Other Math Functions

    SquaringThe SQR function squares the value in accumu-lator 1.Example:Computing the volume of a cylinder V =Radius;

    Example: Any power can be computed with theformula

    LSQRL*RL*RT

    Height;3.141592;.rVolume;

    Square-root extractionThe SQRT function extracts the square root ofthe value in accumulator 1. If the value in accu-mulator 1 is less than zero, SQRT sets statusbits CCO, CC1, OV and OS to "1" and returnsan invalid REAL number. If accumulator 1 con-tains -0 (minus zero), 0 is returned.Example: c =L #a;SQR ;L #b;SQR ;+R ;SQRT ;T #c;(If a or b is declared as a local variable, it mustbe preceded by# if the compiler is to recognizeit as a local variable; if a or b is a global vari-able, it must be enclosed in quotation marks.)Exponentiation tobase eThe EXP function computes the power frombase e (= 2.718282e+00) and the value in accu-mulator 1(eAccul).

    L Value_a;LN ;L Value_b;*R ;EX P ;T Power;

    Computing the natural logarithmThe LN function computes the natural loga-rithm to base e (= 2.718282e+00) from thenumber in accumulator 1. If accumulator 1 con-tains a value less than or equal to zero, LN setsstatus bits CCO, CC1, OV and OS to "1" and re-turns an invalid REAL number.The natural logarithm is the inverse of the ex-ponential function: If y = Q X then x = In (y).Example: Computing a logarithm to base 10and to any other base.The basic formula is

    where b or n is any base. If you make n = e, youcan compute a logarithm to any base using thenatural logarithm:

    In the special case for base 10, the formula is :lga = In ah T T O = 0.4342945 In a

    20 3