当前位置: 首页 > news >正文

Verilog语法之数学函数

        Verilog-2005支持一些简单的数学函数,其参数的数据类型只能是integer和real型。

Integer型数学函数

        $clog2是一个以2为底的对数函数,其结果向上取整,返回值典型的格式:

integer result;

result = $clog2(n);

        最典型的应用就是通过参数化的方式来求某个变量的位宽,在另一篇文章已经对用法做了详细的介绍:Verilog设计中如何匹配变量的位宽?($clog2系统函数)

Real型数学函数

        其参数数据类型为real型,返回值同样为real型,这意味着下面这些数学函数都无法被综合:

FunctionDescription
$ln(x)N自然对数(以e为底的对数)
$log10(x)十进制对数(以10为底的对数)
exp(x)e^x ,e=2.718281828...
sqrt(x)开平方
$pow(x, y)x^y
$floor(x)向下取整
$ceil(x)向上取整
$hypot(x, y)sqrt(xx + yy)。对两个数平方和开平方
$sin(x)sin
$cos(x)cos
$tan(x)tan
$asin(x)arcsin
$acos(x)arccos
$atan(x)arccos
$atan2(x, y)x/y的反正切
$sinh(x)双曲正弦
$cosh(x)双曲余弦
$tanh(x)双曲正切
$asinh(x)反双曲正弦
$acosh(x)反双曲余弦
$atanh(x)反双曲正切

        写个简单的testbench到modelsim验证一下:

module tb_math_fuc;real x, y;		//这些函数的参数需要是real类型,返回也是real类型initial begin		//0.3f表示取小数点后3位,下同x = 10000;$display("$log10(%0.3f) = %0.3f", x, $log10(x));				//以10为底的对数	x = 1;$display("$ln(%0.3f) = %0.3f", x, $ln(x));						//以e为底的对数x = 2;$display("$exp(%0.3f) = %0.3f", x, $exp(x));						//e^xx = 25;$display("$sqrt(%0.3f) = %0.3f", x, $sqrt(x));					//开平方x = 5;y = 3;$display("$pow(%0.3f, %0.3f) = %0.3f", x, y, $pow(x, y));	//x^yx = 2.7813;$display("$floor(%0.3f) = %0.3f", x, $floor(x));				//向下取整x = 7.1111;$display("$ceil(%0.3f) = %0.3f", x, $ceil(x));				//向上取整x = 30 * (22.0/7.0) / 180;$display("$sin(%0.3f) = %0.3f", x, $sin(x));	//sin函数x = 90 * (22.0/7.0) / 180;$display("$cos(%0.3f) = %0.3f", x, $cos(x));	//cos函数x = 45 * (22.0/7.0) / 180;$display("$tan(%0.3f) = %0.3f", x, $tan(x));	//tan函数x = 0.5;$display("$asin(%0.3f) = %0.3f rad, %0.3f deg", x, $asin(x), $asin(x) * 7.0/22.0 * 180);//arcsin函数x = 0;$display("$acos(%0.3f) = %0.3f rad, %0.3f deg", x, $acos(x), $acos(x) * 7.0/22.0 * 180);	//arccos函数x = 1;$display("$atan(%0.3f) = %0.3f rad, %f deg", x, $atan(x), $atan(x) * 7.0/22.0 * 180);		//arctan函数endendmodule

        这是验证结果:

http://www.lryc.cn/news/3434.html

相关文章:

  • 【手撕面试题】JavaScript(高频知识点一)
  • 如何用PHP实现消息推送
  • 电子学会2020年6月青少年软件编程(图形化)等级考试试卷(四级)答案解析
  • DaVinci:调色版本
  • 【C++初阶】十二、STL---反向迭代器的实现
  • day 43|● 1049. 最后一块石头的重量 II ● 494. 目标和 ● 474.一和零
  • [SSD固态硬盘技术 0] SSD的结构和原理导论
  • Vue (3)
  • SQL语句,常用的DDL表操作语句
  • C 语言 宏定义 :字符串化 stringify 的应用
  • 代替swagger的api接口神器
  • 2月12日,30秒知全网,精选7个热点
  • HTML img和video object-fit 属性
  • Pascal版本的 - freopen
  • STM32单片机OLED显示
  • 备战金三银四,软件测试面试题(全)
  • 硬件篇-配置
  • 网页内容 中文乱码 解决办法
  • 【C++之容器篇】造轮子:模拟实现vector类
  • C++中的右值引用与移动构造函数
  • Swift如何使用依赖注入进行解藕
  • 合宙ESP32S3-CORE开发板|保姆级|Arduino IDE|windows11|esp32S3支持库|helloword例程:Arduino 环境搭建
  • CMake中target_precompile_headers的使用
  • SpringCloud和微服务介绍
  • Qt源码编译过程中配置文件中的选项说明
  • Mysql 增删改查(一) —— 查询(条件查询where、分页limits、排序order by、分组 group by)
  • VScode 结合clangd 构建linux源代码阅读环境
  • web应用 —— JavaScript
  • SSM整合SpringSecurity简单使用
  • Java零基础教程——数据类型