【发布时间】:2016-02-28 11:18:35
【问题描述】:
我编写了一个对浮点数组的内容求和的过程。该数组的长度为 6,并且有 6 次相同的值,即 0.5。所以数组的和是3.0。
流程是这样的:
signal array_sum : float32 := to_float(0.0);
begin
....
sum_proc:process(clk)
begin
if(rising_edge(clk)) then
for i in 0 to size_array - 1 loop
array_sum <= array_sum + array_float(i);
end loop;
result <= array_sum;
end if;
end process;
结果是 1.5..
但是,如果我使用辅助值,它会起作用,结果是 3.0:
sum_proc:process(clk)
variable sum_aux : float32;
begin
sum_aux := to_float(0.0);
if(rising_edge(clk)) then
for i in 0 to size_array - 1 loop
sum_aux := sum_aux + array_float(i);
end loop;
result <= sum_aux;
end if;
end process;
我似乎无法理解为什么会发生这种情况。谁能解释一下?
另外,sum_aux 初始化为 0.0 需要在 begin 内部完成,否则结果不正确。是不是因为只有begin里面的代码被执行了多次?
【问题讨论】: