String conversion to real

I need to convert a CSV parsed line to real values and use them with the full precision in my test bench.

My problem is that the presicion of the real value is cut off.
How can i retain the full presicion of the real number?
I have created a little example that shows that my expectation is not met.

module tb();
 
 	parameter string linex = "-0.51171875, 0.052734375, -0.0625, -0.638671875,";
  	var string s0,s1,s2,s3;
 	var real r0;
  
	initial begin
      $display("[info] the string is: ",linex);
      $sscanf(linex,"%s %s %s %s",s0,s1,s2,s3);
      $display("[info] %f",s0);
      r0 = s0.atoreal();
      $display("[info] %f",r0);
 	end

endmodule

The output of the above code is:

# [info] the string is: -0.51171875, 0.052734375, -0.0625, -0.638671875,
# [info] -0.51171875,
# [info] -0.511719

You should add a width specification to your format string:

$display("[info] %2.10f", r0);
1 Like