real max = 1;
//int queue[$];
real min = 0.625;
shortreal a ;
shortreal min_value;
real min_total;
shortreal array[*];
int i = 0;
int j = 0;
real period;
real clk_period;
task for_loop();
a = 0.001;
min_value = min + a;
for(i=0;i<375;i++)
begin
min_value = min_value + a;
array[i] = min_value;
$display("////////////////min_value = %p//////////////////",min_value);
$display("////////////////array[%p] = %p//////////////////",i,array[i]);
end
endtask
task clk_period_1(real period);
for(j=0;j<375;j++)
begin
if(array[j] == period)
begin
$display("////////////////matched//////////////////");
$display("////////////////clk_period_1 array[%p] = %p//////////////////",j,array[j]);
end
end
endtask
endclass
module name;
initial begin
test t;
t =new;
t.for_loop();
t.clk_period_1(1);
end
endmodule
//but after some simulation time the number gets appended with .000000000001 or .999999999991 whether i use real or shortreal datatype upto index [54] values coming properly after that numbers get appended with .000000000001 or .999999999991. How can i avoid it??
In reply to Somu:
This is a general problem with representing decimal numbers in a fractional binary format. You may want to scale your number by 1000 as you increment it, and then scale it back where you reference it.
class test;
int max = 1;
real min = 0.625;
real inc = 0.001;
shortreal arr[$];
function disp();
$display("int max",max);
$display("real min",min);
$display("real inc",inc);
$display("array",arr);
endfunction
task loop;
arr[0] = min;
for(int j=1; j<376; j++)
begin
min = min + inc;
arr.push_back(min);
end
endtask
endclass
module ex;
test t;
initial begin
t = new();
t.loop;
t.disp;
end
endmodule
//real min = 0.625;
int min = 625;
//real inc = 0.001;
int inc = 1;
shortreal arr[$];
.
.
.
task loop;
arr[0] = min;
for(int j=1; j<376; j++)
begin
min = min + inc;
arr.push_back(min/1000.0);
end
endtask
But Dave, the problem comes from inability to represent certain decimal numbers exactly in binary(IEEE 754). for eg, approximating 0.2 with 1/8+1/16…etc is going to have round-off errors.
whether we scale or not, don’t you think we may eventually have this issue ?