C语言(画一个余弦函数)

2018-11-07  本文已影响0人  大飞歌

1.三角函数说明

    余弦函数说明

2.下面我用C语言给大家画一个余弦函数

这是一个彩色打印出来的余弦函数图像,在屏幕上显示了0°~360°的余弦函数图像。没有使用数组输出,而是使用数学库cos函数;通过使用输出余弦函数图像,学会使用cos函数;

#include <stdio.h>

#include <math.h>

#define LIGHT_WHITE_GREEN  "\e[1;47;32m"

#define PURPLE              "\e[0;35m"

#define NONE                "\e[m"

/*在屏幕上显示0~360度的余弦函数cos(x)的曲线

* */

void Display_cos(void){

    double y;

    int x, m;

    for(y = 1; y >= -1; y -= 0.1){  //y为列方向,值从-1到1,步长为0.1

        m = acos(y) * 10;      //计算出y对应的弧度m,乘10为图形放大倍数

        for(x = 1; x < m; x++){

            printf(PURPLE"+"NONE);

        }

        printf(LIGHT_WHITE_GREEN"*"NONE);//printf("(%2d,%2d)", x, m);//控制左侧打印的“*”号

        for(; x < 62 - m; x++){

            printf("-");

        }

        printf(LIGHT_WHITE_GREEN"*"NONE);//printf("(%2d,%2d)", x, m);//控制右侧打印的“*”号

        for(x = 1; x <= m; x++){

            printf(PURPLE"+"NONE);

        }

        puts("");

    }

}

int main(int argc, char* argv[])

{

    Display_cos();

    return 0;

}


COS(3) Linux Programmer's Manual COS(3)

NAME

      cos, cosf, cosl - cosine function

SYNOPSIS

      #include <math.h>

      double cos(double x);

      float cosf(float x);

      long double cosl(long double x);

      Link with -lm.

  Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

      cosf(), cosl():

          _BSD_SOURCE || _SVID_SOURCE || _XOPEN_SOURCE >= 600 || _ISOC99_SOURCE || _POSIX_C_SOURCE >= 200112L;

          or cc -std=c99

DESCRIPTION

      These functions return the cosine of x, where x is given in radians.

RETURN VALUE

      On success, these functions return the cosine of x.

      If x is a NaN, a NaN is returned.

      If x is positive infinity or negative infinity, a domain error occurs, and a NaN is returned.

ERRORS

      See math_error(7) for information on how to determine whether an error has occurred when calling these functions.

      The following errors can occur:

      Domain error: x is an infinity

              errno is set to EDOM (but see BUGS).  An invalid floating-point exception (FE_INVALID) is raised.

ATTRIBUTES

      For an explanation of the terms used in this section, see attributes(7).

┌──────────────────────┬───────────────┬─────────┐

      │Interface            │ Attribute    │ Value  │

      ├──────────────────────┼───────────────┼─────────┤

      │cos(), cosf(), cosl() │ Thread safety │ MT-Safe │

      └──────────────────────┴───────────────┴─────────┘

CONFORMING TO

      C99, POSIX.1-2001, POSIX.1-2008.

      The variant returning double also conforms to SVr4, 4.3BSD.

BUGS

      Before version 2.10, the glibc implementation did not set errno to EDOM when a domain error occurred.

SEE ALSO

      acos(3), asin(3), atan(3), atan2(3), ccos(3), sin(3), sincos(3), tan(3)

COLOPHON

      This  page is part of release 4.04 of the Linux man-pages project.  A description of the project, information about

      reporting bugs, and the latest version of this page, can be found at http://www.kernel.org/doc/man-pages/.

                                                        2015-04-19                                                  COS(3)

man手册中cos函数的英文翻译(仅作参考):

COS(3)Linux程序员手册COS(3)

名称

       cos,cosf,cosl - 余弦函数

概要

       #include <math.h>

       双cos(双x);

       float cosf(float x);

       长双cosl(长双x);

       链接-lm。

   glibc的功能测试宏要求(参见feature_test_macros(7)):

       cosf(),cosl():

           _BSD_SOURCE || _SVID_SOURCE || _XOPEN_SOURCE> = 600 || _ISOC99_SOURCE || _POSIX_C_SOURCE> = 200112L;

           或cc -std = c99

描述

       这些函数返回x的余弦,其中x以弧度给出。

返回值

       成功时,这些函数返回x的余弦值。

       如果x是NaN,则返回NaN。

       如果x为正无穷大或负无穷大,则会发生域错误,并返回NaN。

错误

       有关如何在调用这些函数时确定是否发生错误的信息,请参阅math_error(7)。

       可能发生以下错误:

       域错误:x是无穷大

              errno设置为EDOM(但请参阅BUGS)。引发了无效的浮点异常(FE_INVALID)。

ATTRIBUTES

       有关本节中使用的术语的说明,请参阅属性(7)。

       ┌──────────────────────┬───────────────┬─────────┐

       │接口│属性│值│

       ├──────────────────────┼───────────────┼─────────┤

       │cos(),cosf(),cosl()│螺纹安全│MT-Safe│

       └──────────────────────┴───────────────┴─────────┘

符合

       C99,POSIX.1-2001,POSIX.1-2008。

       返回double的变量也符合SVr4,4.3BSD。

BUGS

       在版本2.10之前,当发生域错误时,glibc实现没有将errno设置为EDOM。

也可以看看

       acos(3),asin(3),atan(3),atan2(3),ccos(3),sin(3),sincos(3),tan(3)

版权页

       该页面是Linux man-pages项目4.04版的一部分。项目描述,信息

       报告错误以及本页面的最新版本可在http://www.kernel.org/doc/man-pages/找到。

                                                        2015-04-19 COS(3)

上一篇下一篇

猜你喜欢

热点阅读