5

I would like to print some real numbers to a log file. To make them easy to read I would like them to all have the same width. I know these numbers will range from 0 to 4095.75 so I tried this:

$display("expected= %4.2f, actual= %4.2f", expected, actual)

What I expected to see was this:

expected=   12.25, actual=   12.75 
expected= 4093.25, actual= 4094.75 

But what I got in was this:

expected= 12.25, actual= 12.75 
expected= 4093.25, actual= 4094.75 

How do I force the width of the value above the decimal point to be 4 characters? Section 21.2.1.3 Size of displayed data of the LRM is silent on %f.

toolic
  • 57,801
  • 17
  • 75
  • 117
nguthrie
  • 2,615
  • 6
  • 26
  • 43

2 Answers2

5

Using %7 works for your values with 2 simulators I tried:

module tb;

real expected, actual;

initial begin
    expected = 12.25; actual=   12.75;
    $display("expected= %7.2f, actual= %7.2f", expected, actual);
    expected= 4093.25; actual= 4094.75;
    $display("expected= %7.2f, actual= %7.2f", expected, actual);
    #5 $finish;
end

endmodule

Output:

expected=   12.25, actual=   12.75
expected= 4093.25, actual= 4094.75
toolic
  • 57,801
  • 17
  • 75
  • 117
  • 2
    Stupid me assuming that the first number referred to what happened before the decimal point instead of the total length of the field. – nguthrie Apr 28 '15 at 17:49
  • @nguthrie: That was just a wild guess on my part. The IEEE Std 1800-2012 does not explicity show the %X.Yf syntax. It just alludes to the C language. Behavior might be system-dependent, too. – toolic Apr 28 '15 at 17:51
-2

It is possible also to specify how many number we want to see for floating point vars, after comma:

    `uvm_info(get_type_name(), $sformatf("tslot_start=%.3f, tslot_start_tolerance=%.2f", 
tslot_start, tslot_start_tolerance), UVM_LOW)
haykp
  • 435
  • 12
  • 29