I want to generate numbers with a range of 0.625 to 1 with increment of 0.001

class test;

  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.

See What Every Computer Scientist Should Know About Floating-Point Arithmetic

In reply to Somu:

is this want you want…

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

Dave’s Suggestion is …


  //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

In reply to swapnilsrf9:

When running your code, this is what display function shows:

int max 1

real min: 1
real inc : 0.001

the min value shows as 1 in my simulator

Thanks Dave its working now.

Thanks Suresh, its working now.

In reply to dave_59:

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 ?

*In reply to ssureshg_:*Scaling to integers eliminates the accumulation of rounding errors.