PROGRAM Cosine(Input, Output);
    {-----------------------------------------------------------------
        Program 4.6 - Compute the cosine using the expansion:
        Cos(X) = 1 - Sqr(X) / (2*1) + Sqr(X) * Sqr(X) / 4*3*2*1) - ...
    ------------------------------------------------------------------}

    CONST
        Epsilon = 1e-7;
    VAR
        Angle: Real;        { radians }
        ASquared: Real;     { angle squared }
        Series: Real;       { cosine series }
        Term: Real;         { next term in series }
        I, N: Integer;      { number of cosine to compute }
        Power: Integer;     { power of next term }

BEGIN
    ReadLn(Input, N);
    FOR I := 1 TO N DO
    BEGIN
        ReadLn(Input, Angle);
        Term := 1;
        Power := 0;
        Series := 1;
        ASquared := Sqr(Angle);
        WHILE Abs(Term) > Epsilon * Abs(Series) DO
        BEGIN
            Power := Power + 2;
            Term := -Term * ASquared / (Power * (Power-1));
            Series := Series + Term;
        END;
        WriteLn(Output, Angle:24, Series:24, Power DIV 2:10);
        { = terms of convergence }
    END
END.
