Verilog的浮点二进制转换 [英] Verilog floating point to binary conversion

查看:1291
本文介绍了Verilog的浮点二进制转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用Verilog转换签署浮点数到24签位的二进制值。例如:

I am trying convert a signed floating number in Verilog to a signed 24 bit binary value. For example

-0.0065 would become: 24'b100000000110101001111110
0.0901 would become: 24'b000001011100010000110010

这是正确的?
谢谢

Is this correct? Thank you

推荐答案

以分数小数 0.0901 ,并转换为定点值与2位整数和22分数位(24位总数):

Taking the fractional decimal 0.0901 and converting to a fixed-point value with 2 integer bits and 22 fractional bits (24 bit total):

用于数学(可以剪切并粘贴到IRB(交互式红宝石)的命令行工具)的Ruby语法

Ruby syntax used for the maths (you can cut and paste into irb (interactive ruby) a command line tool):

i = (0.0901 * 2**20) # 377906.7904
# truncat to integer value
i = i.to_i # 377906

# Convert to string using binary (base 2)
i.to_s(2) # "1011100010000110010"

要添加前导零(24位长),右对齐和垫0

To add the leading zeros (24 bit length), right justify and pad with 0's

i.to_s(2).rjust(24, '0') # "000001011100010000110010"

# Convert to Hex (base 16)
i.to_s(16) # "5c432"

符号数都有点问题更多,最简单的方法是计算正值然后执行二进制补码:

Signed numbers are a bit more problematic, easiest way is to calculate positive value then perform twos complement :

(0.0065 * 2**22).to_i.to_s(2).rjust(24, '0')
=> "000000000110101001111110"

二进制补码

"000000000110101001111110"
"111111111001010110000001" # Ones complement (bit invert)
"111111111001010110000001" + 1 
"111111111001010110000010" #Twos complement

您有 24'b100000000110101001111110 这仅仅是个正数的最高位设置为 1 这是不如何有符号数正常工作。你已经使用的格式是注册幅度,但你不能只给了成倍数(按你的previous问题)。

You had 24'b100000000110101001111110 which is just the positive number with the MSB set to 1 which is not how signed numbers normally work. The format you have used is Sign Magnitude, but you can not just feed that into a multiplier (as per your previous question).

注:我也跳过了转换到定点的量化效果。当你的分数比特的比例系数为377906.7904。但我们只取整数部分给你一个错误 0.7904 这将可能影响你的过滤性能。

NB: I have also skipped over the quantisation effect of converting to fixed point. Your coefficient when scaled by your fractional bit was 377906.7904. but we just take the integer part giving you an error of 0.7904 which will may effect your filter performance.

这篇关于Verilog的浮点二进制转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆