Wednesday, January 18, 2012

Math Functions in Dynamics NAV


I have created the Math functions codeunit 50010
Use when you want any calculations using Math functions


OBJECT Codeunit 50010 Math Functions
{
  OBJECT-PROPERTIES
  {
    Date=31/01/11;
    Time=[ 6:59:10 PM];
    Modified=Yes;
    Version List=Math_SR;
  }
  PROPERTIES
  {
    OnRun=BEGIN
          END;

  }
  CODE
  {

    PROCEDURE "!"@1000000000(x@1000000000 : Decimal) Factorial : Decimal;
    BEGIN
      Factorial := x;
      WHILE x > 1 DO BEGIN
        x -= 1;
        Factorial := Factorial * x
      END;
    END;

    PROCEDURE Sin@1000000001(x@1000000000 : Decimal) : Decimal;
    BEGIN
      EXIT(x*(1 - (x*x)*(1 - (x*x)*(1 - (x*x)*(1 - (x*x)*(1 - (x*x)*(1 - (x*x)*(1 - (x*x)*(1 - (x*x)
      /(16*17) )/(14*15) )/(12*13) )/(10*11) )/(8*9) )/(6*7) ) / (4*5) ) /(2*3) ));
    END;

    PROCEDURE Cos@1000000003(x@1000000000 : Decimal) Cos : Decimal;
    BEGIN
      EXIT(Sin(x+Pi/2));
    END;

    PROCEDURE Tan@1000000013(x@1000000000 : Decimal) : Decimal;
    BEGIN
      EXIT(Sin(x) / Cos(x));
    END;

    PROCEDURE ArcSin@1000000005(x@1000000000 : Decimal) a : Decimal;
    BEGIN
      IF x < 0 THEN
        EXIT(-ArcSin(-x))
      ELSE
        IF x = 1 THEN
          EXIT(Pi / 2)
        ELSE
          EXIT(ArcTan(x / Sqrt(1 - x * x)));
    END;

    PROCEDURE ArcCos@1000000006(x@1000000000 : Decimal) a : Decimal;
    BEGIN
      IF x < 0 THEN
        EXIT(Pi - ArcCos(-x))
      ELSE
        IF x = 0 THEN
          EXIT(Pi / 2)
        ELSE
        EXIT(ArcTan(Sqrt(1 - x * x) / x));
    END;

    PROCEDURE ArcTan@1000000008(x@1000000000 : Decimal) ATan : Decimal;
    VAR
      Sum@1000000001 : Decimal;
      i@1000000002 : Integer;
      Inverting@1000000003 : Boolean;
    BEGIN

      Inverting :=  (x < -1) OR (x > 1);
      IF Inverting THEN
        x := 1/x;

      CLEAR(ATan);
      FOR i := 1 TO 100 DO BEGIN
        ATan += (POWER(x,i) / i) - (POWER(x,i+2) / (i+2));
        i += 3;
      END;

      IF Inverting THEN
        IF x > 0 THEN
          EXIT(Pi/2-ATan)
        ELSE
          EXIT(-Pi/2-ATan)
    END;

    PROCEDURE Pi@1000000004() : Decimal;
    BEGIN
      EXIT(3.14159265358979323);
    END;

    PROCEDURE Sqrt@1000000009(x@1000000000 : Decimal) : Decimal;
    BEGIN
      EXIT(POWER(x,0.5));
    END;

    BEGIN
    {
      Austral Sameera 23.12.2010 --  Math Functions to Use Store Locator                    
    }
    END.
  }
}

No comments:

Post a Comment