【发布时间】:2017-08-09 16:44:03
【问题描述】:
我正在使用 David bishop 的定点库 在 vhdl 中进行一些数学运算。我需要将最终值解码为整数。我遵循的方法如下,我确实得到了小数部分的精细值,但十进制值不正确。我还找不到问题。在小数部分第 1 两位数是错误的。 xx8374.839923 xx 数字总是错误的。
当我执行此操作时,我得到 274334.738295 for 174334.738295
在建筑内部, 在进程内部我确实声明了这些变量,
variable cp : sfixed(20 downto -19);
variable mod10 : sfixed(4 downto 0);
variable div10 : sfixed(4 downto 0);
variable L0 : sfixed(4 downto 0);
variable L1 : sfixed(4 downto -4);
variable L2 : sfixed(4 downto -8);
variable L3 : sfixed(4 downto -12);
variable L4 : sfixed(4 downto -16);
variable L5 : sfixed(4 downto -20);
variable L6 : sfixed(4 downto -24);
variable temp_L : sfixed(19 downto 0);
variable temp_L1 : sfixed(20 downto -4);
variable temp_L2 : sfixed(21 downto -8);
variable temp_L3 : sfixed(22 downto -12);
variable temp_L4 : sfixed(23 downto -16);
variable temp_L5 : sfixed(24 downto -20);
variable temp_L6 : sfixed(25 downto -24);
开始后,我需要在 sfixed 中解码 174334.738295,因为有时我会得到负数。这个数字被另一个计算代替,这里没有提供,如果这个数字是174334.738295(sfixed)那么我需要解码这个数字,
cp := to_sfixed(174334.738295,20,-19);
temp_L := cp(19 downto 0); -- remove sign bit
mod10 :=to_sfixed(10,4,0) ;
div10 :=to_sfixed(10,4,0) ;
L0 := temp_L mod mod10;
temp_L1 := temp_L/div10;
L1 := temp_L1 mod mod10;
temp_L2 := temp_L1/div10;
L2 := temp_L2 mod mod10;
temp_L3 := temp_L2/div10;
L3 := temp_L3 mod mod10;
temp_L4 := temp_L3/div10;
L4 := temp_L4 mod mod10;
temp_L5 := temp_L4/div10;
L5 := temp_L5 mod mod10;
temp_L6 := temp_L5/div10;
L6 := temp_L6 mod mod10;
L6 为第 1 位,L5 为第 2 位。
【问题讨论】:
-
我不知道负索引是可能的。不过,我不会使用它们。
-
@Oron Port 请参考 David Bishop 的定点库手册!
-
定点和浮点库是开源的,存储在 GitHub:github.com/fphdl/fphdl。您可以在那里打开一个问题并寻求更详细的帮助。您还应该将当前副本与最新版本进行比较。顺便说一下,这是将与 VHDL-2017 一起发布的版本。
-
@Paebbels 所以你没有看到代码有任何问题?
-
您的代码有问题。这不是MCVE,您没有展示“xx8374.839923”是如何产生的,并且需要进行一些编码来测试它(实体、体系结构和进程或进程和子程序)。使用 to_integer[sfixed return integer] 需要进行舍入(并且您没有说明 fixed_pkg 来自哪里,也不是舍入模式)。 mod10 和 div10 也没有声明为变量,您的代码需要修改才能分析。不使用四舍五入和 sfixed to_integer 为整数部分给出 174334。提供一个复制您的结果的 MCVE。
标签: vhdl fpga fixed-point intel-fpga