简体   繁体   English

在VHDL-2008中,如何用c语言设置类似于“%f”的“真实”格式,例如:sprintf(str,“%9.6f”,myreal)

[英]In VHDL-2008, how to format “real” similar to “%f” in c-language, example: sprintf(str, “%9.6f”, myreal)

I'm using VHDL-2008 and I want to nicely format real numbers are strings similar to this c-language function: 我正在使用VHDL-2008,并且我想很好地格式化实数为类似于此c语言函数的字符串:

    sprintf(str, "%9.6f", myreal);

Currently, I'm formatting my real numbers this way: 目前,我正在以这种方式格式化我的实数:

architecture sim of testbench is
    constant myreal :real := 3343.2342;
begin

process
begin
   report real'image(real_number);
   wait;
end process

end architecture;

Which doesn't allow enough control over the formatting of the real numbers in VHDL. 这不允许对VHDL中的实数格式进行足够的控制。 What I want, is control over the vhdl formating more like the c-language "%n.mf" formatter. 我想要的是对vhdl格式的控制,更像是c语言“%n.mf”格式器。

Basically, the VHDL defaul in GHDL simulator is always to print real numbers in scientific notation with one digit left of the decimal place, a fraction, and an exponent, which is annoying as heck. 基本上,GHDL仿真器中的VHDL默认设置始终以科学计数法打印实数,该数字以小数点后一位,小数和指数表示,这很令人讨厌。

VHDL 2008 provides to_string for real in 3 flavours: VHDL 2008提供了三种真实的to_string:

function TO_STRING (VALUE: REAL) return STRING;
function TO_STRING (VALUE: REAL; DIGITS: NATURAL) return STRING;
function TO_STRING (VALUE: REAL; FORMAT: STRING) return STRING;

The first case returns the real in a simple format, the 2nd returns it with DIGITS being the number of digits to display to the right of the decimal point, and the 3rd accepts sprintf style format strings from C: 第一种情况以简单格式返回实数,第二种情况以DIGITS返回其显示在小数点右边的位数,而第三种情况则接受来自C的sprintf样式格式字符串:

entity real_test is
end entity real_test;

architecture test of real_test is
begin

  process
    variable r : real;
  begin
    r := 3.25432;

    report to_string(r);
    report to_string(r, 3);
    report to_string(r, "%.2f");
    wait;
  end process;

end architecture;
EXECUTION:: NOTE   : 3.25432
EXECUTION:: Time: 0 ps,  Iteration: 0,  Instance: /real_test,  Process: line__7.
EXECUTION:: NOTE   : 3.254
EXECUTION:: Time: 0 ps,  Iteration: 0,  Instance: /real_test,  Process: line__7.
EXECUTION:: NOTE   : 3.25
EXECUTION:: Time: 0 ps,  Iteration: 0,  Instance: /real_test,  Process: line__7.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM