COHERENT manpages

This page displays the COHERENT manpage for sin() [Calculate sine].

List of available manpages
Index


sin() -- Mathematics function (libm)

Calculate sine
#include <math.h>
double sin(radian) double radian;

sin() calculates the  sine of its argument radian, which  must be in radian
measure.

Example

The following example uses the functions  sin() and cos() to paint sine and
cosine on the screen.  It is by Dmitry Gringauz (dmitry@golem.com).

#include <math.h>
#include <stdio.h>

#define MAX_X 79 /* X dimension of screen */
#define MAX_Y 23 /* Y dimension of screen */
char screen[MAX_X][MAX_Y]; /* the screen matrix */

main()
{
    double pi = 3.14159, i, result;
    int x = 0, y = 0, mid_x = (MAX_X-1)/2, mid_y = (MAX_Y-1)/2;

    /* blank (dot) out the screen */
    for (y = 0; y < MAX_Y; y++)
        for (x = 0; x < MAX_X; x++)
            screen[x][y] = '.';

    /* build the "axis" */
    for (x=0; x < MAX_X; x++)
        screen[x][mid_y] = '-';
    for (y = 0; y < MAX_Y; y++)
        screen[mid_x][y] = '|';

    /* make center and arrows */
    screen[mid_x][mid_y] = '+';
    screen[mid_x][0] = '^';
    screen[MAX_X-1][mid_y] = '>';

    /* do the sin() and cos() thing */
    for (i = -pi; i <= pi; i = i + 2.0 / (MAX_X)) {
        result = sin(i) ;

        x = i*mid_x/pi + mid_x;
        y = mid_y*(-1.0*result) + mid_y;

        if (x >= MAX_X)
            x = MAX_X - 1;

        if (y >= MAX_Y)
            y = MAX_Y - 1;

        screen[x][y] = '*';
        result = cos(i) ;

        x = i*mid_x/pi + mid_x;
        y = mid_y*(-1.0*result) + mid_y;

        if (x >= MAX_X)
            x = MAX_X - 1;

        if (y >= MAX_Y)
            y = MAX_Y - 1;
        screen[x][y] = '*';
    } /* i */

    /* print the screen */
    for (y = 0; y < MAX_Y; y++) {
        for (x = 0; x < MAX_X; x++)
            printf("%c", screen[x][y]);
        printf("\n");
    } /* y */
}

See Also

cos(),
cosh(),
libm,
sinh()
ANSI Standard, §7.5.2.6
POSIX Standard, §8.1