C++数学函数库

本文详细介绍了C语言中的各种数学函数,包括求绝对值、三角函数、指数函数、对数函数等,提供了丰富的示例代码,帮助读者理解和使用这些数学函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

abs

 

原型:extern int abs(int x);

用法:#include <math.h>

功 能:求整数x的绝对值

说明:计算|x|, 当x不为负时返回x,否则返回-x

举例:

      // abs.c
     
      #include <syslib.h>
      #include <math.h>

      main()
      {
        int x;
       
        clrscr();        // clear screen
       
        x=-5;
        printf("|%d|=%d\n",x,abs(x));
        x=0;
        printf("|%d|=%d\n",x,abs(x));
        x=+5;
        printf("|%d|=%d\n",x,abs(x));
       
        getchar();
        return 0;
      }
     
相关函数:fabs

 

acos

原 型:extern float acos(float x);

用法:#include <math.h>

功 能:求x(弧度表示)的反余弦值

说明:x的定义域为[-1.0,1.0],值域为[0,π]。

举例:

      // acos.c
     
      #include <syslib.h>
      #include <math.h>

      main()
      {
        float x;
       
        clrscr();        // clear screen
        textmode(0x00); // 6 lines per LCD screen
       
        x=0.32;
        printf("acos(%.2f)=%.4f",x,acos(x));
       
        getchar();
        return 0;
      }
     
相关函数:asin,atan,atan2,sin,cos,tan

 


asin

原 型:extern float asin(float x);

用法:#include <math.h>

功 能:求x(弧度表示)的反正弦值

说明:x的定义域为[-1.0,1.0],值域为[-π/2,+π/2]。

举例:

      // asin.c
     
      #include <syslib.h>
      #include <math.h>

      main()
      {
        float x;
       
        clrscr();        // clear screen
        textmode(0x00); // 6 lines per LCD screen
       
        x=0.32;
        printf("asin(%.2f)=%.4f",x,asin(x));
       
        getchar();
        return 0;
      }
     
相关函数:acos,atan,atan2,sin,cos,tan

 

 

atan

原 型:extern float atan(float x);

用法:#include <math.h>

功 能:求x(弧度表示)的反正切值

说明:值域为(-π/2,+π/2)。

举例:

      // atan.c
     
      #include <syslib.h>
      #include <math.h>

      main()
      {
        float x;
       
        clrscr();        // clear screen
        textmode(0x00); // 6 lines per LCD screen
       
        x=0.32;
        printf("atan(%.2f)=%.4f",x,atan(x));
       
        getchar();
        return 0;
      }
     
相关函数:asin,acos,atan2,sin,cos,tan

 

atan2

原 型:extern float atan2(float y, float x);

用法:#include <math.h>

功能:求y/x(弧度表示)的反正切值

说明:值域为(-π/2,+π/2)。

举 例:

      // atan2.c
     
      #include <syslib.h>
      #include <math.h>

      main()
      {
        float x,y;
       
        clrscr();        // clear screen
        textmode(0x00); // 6 lines per LCD screen
       
        x=0.064;
        y=0.2;
        printf("atan2(%.3f,%.2f)=%.4f",y,x,atan2(y,x));
       
        getchar();
        return 0;
      }
     
相关函 数:asin,acos,atan,sin,cos,tan

 


ceil

原型:extern float ceil(float x);

用法:#include <math.h>

功能:求不小于x的最 小整数

说明:返回x的上限,如74.12的上限为75,-74.12的上限为-74。返回值为float类型。

举例:

      // ceil.c
     
      #include <syslib.h>
      #include <math.h>

      main()
      {
        float x;
       
        clrscr();        // clear screen
        textmode(0x00); // 6 lines per LCD screen
       
        x=74.12;
        printf("ceil(%.2f)=%.0f\n",x,ceil(x));
        x=-74.12;
        printf("ceil(%.2f)=%.0f\n",x,ceil(x));
       
        getchar();
        return 0;
      }
     
相关函数:floor

 


cos

原 型:extern float cos(float x);

用法:#include <math.h>

功 能:求x(弧度表示)的余弦值

说明:返回值在[-1.0,1.0]之间。

举例:

      // cos.c
     
      #include <syslib.h>
      #include <math.h>

      main()
      {
        float x;
       
        clrscr();        // clear screen
        textmode(0x00); // 6 lines per LCD screen
       
        x=PI/4.;
        printf("cos(%.4f)=%.4f\n",x,cos(x));
       
        getchar();
        return 0;
      }
     
相关函数:asin,acos,atan,atan2,sin,tan

 


cosh

原 型:extern float cosh(float x);

用法:#include <math.h>

功 能:求x的双曲余弦值

说明:cosh(x)=(e^x+e^(-x))/2

举例:

      // cosh.c
     
      #include <syslib.h>
      #include <math.h>

      main()
      {
        float x;
       
        clrscr();        // clear screen
        textmode(0x00); // 6 lines per LCD screen
       
        x=PI/4.;
        printf("cosh(%.4f)=%.4f\n",x,cosh(x));
       
        getchar();
        return 0;
      }
     
相关函数:sinh,tanh

 

exp

原 型:extern float exp(float x);

用法:#include <math.h>

功 能:求e的x次幂

说明:e=2.718281828...

举例:

      // exp.c
     
      #include <syslib.h>
      #include <math.h>

      main()
      {
        clrscr();        // clear screen
        textmode(0x00); // 6 lines per LCD screen
       
        printf("e=%f\n",exp(1.0));
       
        getchar();
        return 0;
      }
     
相关函数:无

 


fabs

原 型:extern float fabs(float x);

用法:#include <math.h>

功 能:求浮点数x的绝对值

说明:计算|x|, 当x不为负时返回x,否则返回-x

举例:

      // fabs.c
     
      #include <syslib.h>
      #include <math.h>

      main()
      {
        float x;
       
        clrscr();        // clear screen
        textmode(0x00); // 6 lines per LCD screen
       
        x=-74.12;
        printf("|%f|=%f\n",x,fabs(x));
        x=0;
        printf("|%f|=%f\n",x,fabs(x));
        x=74.12;
        printf("|%f|=%f\n",x,fabs(x));
       
        getchar();
        return 0;
      }
     
相关函数:abs

 


floor

原 型:extern float floor(float x);

用法:#include <math.h>

功 能:求不大于x的最达整数

说明:返回x的下限,如74.12的下限为74,-74.12的下限为-75。返回值为float类型。

举 例:

      // floor.c
     
      #include <syslib.h>
      #include <math.h>

      main()
      {
        float x;
       
        clrscr();        // clear screen
        textmode(0x00); // 6 lines per LCD screen
       
        x=74.12;
        printf("floor(%.2f)=%.0f\n",x,floor(x));
        x=-74.12;
        printf("floor(%.2f)=%.0f\n",x,floor(x));
       
        getchar();
        return 0;
      }
     
相关函数:ceil

 

fmod

原 型:extern float fmod(float x, float y);

用法:#include <math.h>

功 能:计算x/y的余数

说明:返回x-n*y,符号同y。n=[x/y](向离开零的方向取整)

举例:

      // fmod.c
     
      #include <syslib.h>
      #include <math.h>

      main()
      {
        float x,y;
       
        clrscr();        // clear screen
        textmode(0x00); // 6 lines per LCD screen

        x=74.12;
        y=6.4;
        printf("74.12/6.4: %f\n",fmod(x,y));       
        x=74.12;
        y=-6.4;
        printf("74.12/(-6.4): %f\n",fmod(x,y));       
       
        getchar();
        return 0;
      }
     
相关函 数:无

 


frexp

原型:extern float frexp(float x, int *exp);

用法:#include <math.h>

功能:把浮点数x分解成尾数和指数。

说 明:x=m*2^exp,m为规格化小数。返回尾数m,并将指数存入exp中。

举例:

      // frexp.c
     
      #include <syslib.h>
      #include <math.h>

      main()
      {
        float x;
        int exp;
       
        clrscr();        // clear screen
        textmode(0x00); // 6 lines per LCD screen
     
        x=frexp(64.0,&exp);
        printf("64=%.2f*2^%d",x,exp);
       
        getchar();
        return 0;
      }
     
相关函数:ldexp,modf

 


hypot

原 型:extern float hypot(float x, float y);

用法:#include <math.h>

功能:对于给定的直角三角形的两个直角边,求其斜边的长度。

说明:返回斜边值。

举 例:

      // hypot.c
     
      #include <syslib.h>
      #include <math.h>

      main()
      {
        clrscr();        // clear screen
        textmode(0x00); // 6 lines per LCD screen
     
        printf("3^2+4^2=%.0f^2\n",hypot(3.,4.));
        printf("3.2^2+4.3^2=%.2f^2",hypot(x,y));
       
        getchar();
        return 0;
      }
     
相关函数:frexp,ldexp

 


ldexp

原 型:extern float ldexp(float x, int exp);

用法:#include <math.h>

功能:装载浮点数。

说明:返回x*2^exp的值。

举例:

      // ldexp.c
     
      #include <syslib.h>
      #include <math.h>

      main()
      {
        float x;
       
        clrscr();        // clear screen
        textmode(0x00); // 6 lines per LCD screen
     
        x=ldexp(1.0,6); // 1.0*2^6
        printf("2^6=%.2f",x);
       
        getchar();
        return 0;
      }
     
相关函数:frexp,modf

 


log

原 型:extern float log(float x);

用法:#include <math.h>

功 能:计算x的自然对数。

说明:x的值应大于零。

举例:

      // log.c
     
      #include <syslib.h>
      #include <math.h>

      main()
      {
        float x;
        clrscr();        // clear screen
        textmode(0x00); // 6 lines per LCD screen
     
        printf("ln(e)=%f\n", log(M_E)); // M_E is 2.71828..., defined in math.h
       
        getchar();
        return 0;
      }
     
相关函数:log10

 


log10

原型:extern float log10(float x);

用法:#include <math.h>

功能:计算x的常用 对数。

说明:x的值应大于零。

举例:

      // log10.c
     
      #include <syslib.h>
      #include <math.h>

      main()
      {
        float x;
        clrscr();        // clear screen
        textmode(0x00); // 6 lines per LCD screen
     
        printf("lg(5)=%f\n", log10(5.0));
       
        getchar();
        return 0;
      }
     
相关函数:log

 

 

modf

原 型:extern float modf(float num, float *i);

用法:#include <math.h>

功能:将浮点数num分解成整数部分和小数部分。

说明:返回小数部分,将整数部分存入*i 所指内存中。

举例:

      // modf.c
     
      #include <syslib.h>
      #include <math.h>

      main()
      {
        float x, i;
       
        clrscr();        // clear screen
        textmode(0x00); // 6 lines per LCD screen
     
        x=modf(-74.12,&i);
        printf("-74.12=%.0f+(%.2f)",i,x);
       
        getchar();
        return 0;
      }
     
相关函数:frexp,ldexp

 

pow10

原 型:extern float pow10(float x);

用法:#include <math.h>

功 能:计算10的x次幂。

说明:相当于pow(10.0,x)。

举例:

      // pow10.c
     
      #include <syslib.h>
      #include <math.h>

      main()
      {
        clrscr();        // clear screen
        textmode(0x00); // 6 lines per LCD screen
     
        printf("10^3.2=%f\n",pow10(3.2));
        printf("10^3.2=%f",pow(10,3.2));
       
        getchar();
        return 0;
      }
     
相关函数:pow

 


pow

原 型:extern float pow(float x, float y);

用法:#include <math.h>

功 能:计算x的y次幂。

说明:x应大于零,返回幂指数的结果。

举例:

      // pow.c
     
      #include <syslib.h>
      #include <math.h>

      main()
      {
        clrscr();        // clear screen
        textmode(0x00); // 6 lines per LCD screen
     
        printf("4^5=%f",pow(4.,5.));
       
        getchar();
        return 0;
      }
     
相关函数:pow10

 


sin

原 型:extern float sin(float x);

用法:#include <math.h>

功 能:计算x(弧度表示)的正弦值。

说明:x的值域为[-1.0,1.0]。

举例:

      // sin.c
     
      #include <syslib.h>
      #include <math.h>

      main()
      {
        float x;
        clrscr();        // clear screen
        textmode(0x00); // 6 lines per LCD screen
     
        x=M_PI/2;        // M_PI=PI=3.14159265..., defined in math.h
        printf("sin(PI/2)=%f",sin(x));
       
        getchar();
        return 0;
      }
     
相关函数:asin,acos,atan,atan2,cos,tan

 

 

sinh

原 型:extern float sinh(float x);

用法:#include <math.h>

功 能:计算x(弧度表示)的双曲正弦值。

说明:sinh(x)=(e^x-e^(-x))/2。

举例:


      // sinh.c
     
      #include <syslib.h>
      #include <math.h>

      main()
      {
        float x;
       
        clrscr();        // clear screen
        textmode(0x00); // 6 lines per LCD screen
       
        x=PI/4.;
        printf("sinh(%.4f)=%.4f\n",x,sinh(x));
       
        getchar();
        return 0;
      }
           
相关函数:cosh,tanh

 

sqrt

原 型:extern float sqrt(float x);

用法:#include <math.h>

功 能:计算x的平方根。

说明:x应大于等于零。

举例:


      // sqrt.c
     
      #include <syslib.h>
      #include <math.h>

      main()
      {
        clrscr();        // clear screen
        textmode(0x00); // 6 lines per LCD screen
       
        printf("sqrt(2000)=%f",sqrt(2000.0));
       
        getchar();
        return 0;
      }
           
相关函数:无

 


tan

原 型:extern float tan(float x);

用法:#include <math.h>

功 能:计算x(弧度表示)的正切值。

说明:返回x的正切值。

举例:

      // tan.c
     
      #include <syslib.h>
      #include <math.h>

      main()
      {
        float x;
        clrscr();        // clear screen
        textmode(0x00); // 6 lines per LCD screen
     
        x=M_PI/4;        // M_PI=PI=3.14159265..., defined in math.h
        printf("tan(PI/4)=%f",tan(x));
       
        getchar();
        return 0;
      }
     
相关函 数:asin,acos,atan,atan2,sin,cos

 


tanh

原型:extern float tanh(float x);

用法:#include <math.h>

功能:求x的双曲正切 值

说明:tanh(x)=(e^x-e^(-x))/(e^2+e^(-x))

举例:

      // tanh.c
     
      #include <syslib.h>
      #include <math.h>

      main()
      {
        float x;
       
        clrscr();        // clear screen
        textmode(0x00); // 6 lines per LCD screen
       
        x=PI/4.;
        printf("tanh(%.4f)=%.4f\n",x,tanh(x));
       
        getchar();
        return 0;
      }

转载于:https://www.cnblogs.com/rogee/archive/2010/11/05/1869955.html

<think>好的,我现在要处理用户关于Android 12中RKNN开发配置和使用教程的问题。首先,我需要回顾用户提供的引用内容,看看是否有相关的信息。 用户提供了三个引用。引用1提到了RKNN Model Zoo和RKNN Toolkit2的环境配置,包括安装Miniconda3。引用2是关于系统镜像下载,特别是Ubuntu 22.04版本和导出ONNX模型。引用3讨论了RV1808/1126使用RKNN作为MediaPipe推理后端的步骤,包括配置参数如use_rga、use_mmap和data_type的设置,以及反量化的处理。 用户的问题集中在Android 12上,所以需要将引用中的信息与Android系统关联起来。引用3提到了MediaPipe和RKNN的集成,这可能适用于Android开发,但需要确认是否支持Android 12。此外,RKNN Toolkit2的环境配置可能在不同平台上有所不同,用户可能需要针对Android进行调整。 接下来,我需要考虑Android开发中RKNN的一般步骤,比如环境准备、模型转换、集成到Android应用、配置参数和性能优化。结合引用中的信息,模型转换部分可以参考引用2提到的导出ONNX模型,然后使用RKNN Toolkit转换为RKNN格式。引用3中的配置参数(如use_rga、use_mmap、data_type)可能在Android中同样适用,但需要查阅相关文档确认。 可能还需要指导用户如何将RKNN库集成到Android项目中,比如添加依赖项或NDK配置。此外,性能优化方面,引用3提到的反量化处理可能需要特别注意,尤其是在Android设备上的实现。 最后,确保回答结构清晰,分步骤说明,并引用提供的资料。需要验证引用中的信息是否适用于Android 12,例如系统镜像是否支持,或者是否有特定版本的要求。如果某些步骤在引用中没有明确说明,可能需要提示用户参考官方文档或社区资源。</think>### Android 12 RKNN 开发配置与使用教程 #### 1. **环境准备** - **系统镜像与工具链**: 根据引用[2],建议使用Ubuntu 22.04作为开发环境,并下载Orange Pi 5 Pro等支持RKNN的硬件镜像。对于Android 12开发,需确保RKNN Toolkit2适配Android NDK版本(推荐NDK 21+)[^2]。 - **安装RKNN Toolkit2**: 参考引用[1],通过Miniconda3配置Python环境(建议Python 3.8),并安装RKNN Toolkit2以支持模型转换与推理。命令示例: ```bash pip install rknn_toolkit2 ``` #### 2. **模型转换(ONNX转RKNN)** - **导出ONNX模型**: 使用PyTorch或TensorFlow等框架导出模型为ONNX格式(引用[2])。 - **转换为RKNN格式**: 编写RKNN配置文件(`config.yml`),指定输入尺寸、量化方式等参数。示例代码: ```python from rknn.api import RKNN rknn = RKNN() rknn.config(target_platform='rk3568', mean_values=[[127.5]], std_values=[[127.5]]) rknn.load_onnx(model="model.onnx") rknn.build(do_quantization=True, dataset="dataset.txt") rknn.export_rknn("model.rknn") ``` #### 3. **Android项目集成** - **添加RKNN库依赖**: 将编译好的RKNN动态库(`librknnrt.so`)和模型文件(`model.rknn`)放入Android项目的`jniLibs`目录。 - **JNI接口开发**: 通过JNI调用RKNN的C/C++ API实现推理。关键配置参考引用[3]: ```cpp #include <rknn/rknn_runtime.h> rknn_context ctx; int ret = rknn_init(&ctx, model_path, 0, 0, nullptr); ret = rknn_inputs_set(ctx, input_num, input_attrs); ret = rknn_run(ctx, nullptr); ret = rknn_outputs_get(ctx, output_num, output_attrs, nullptr); ``` #### 4. **关键配置参数** - **use_rga与use_mmap**: 根据引用[3],若Android设备不支持硬件加速(如RGA),需将`use_rga`和`use_mmap`设为`false`。 - **数据量化类型**: `data_type`可选`uint8`或`fp32`。若选`uint8`,需手动实现反量化;若选`fp32`,RKNN会自动处理[^3]。 #### 5. **性能优化** - **多线程推理**: 利用Android的`ThreadPoolExecutor`并行处理输入数据。 - **内存优化**: 启用`use_mmap`(若支持)减少内存拷贝开销,或通过零拷贝技术直接操作物理内存。 #### 6. **调试与验证** - **日志输出**: 启用RKNN的调试模式(`RKNN_LOG_LEVEL=DEBUG`)查看详细运行信息。 - **性能分析**: 使用`rknn.query(RKNN_QUERY_PERF_DETAIL)`获取各层耗时,针对性优化模型结构。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值