Getting Clock period Mismatch Error while comparing two clocks even though values of two clocks are same

Hi All,

I was trying to write clock checker, initially I calculated actual_period between two posedge of clocks and expected_period calculated from freequency,
Im trying to calculate expected period based on freequency division, Im putting output log also Im getting check failed even values are same, if I convert to integer and compare then it is matching, anyone help with this, I want to compare clocks without converting into integers, and im using timescale 1ns/1fs
Thanks In Advance.

this is my code:

int freq = 156250;//KHz
bit[3:0] f_div;
realtime exp_period;

realtime actual_period;

realtime c;
realtime p;

initial begin
  
   @(posedge input_clk)
    p=$realtime;
    $display(" period P is:%0t",p);
  forever
   begin
   @(posedge input_clk)
     c=$realtime;
    $display(" period C is:%0t",c);
     actual_period=c-p;
         $display("actual period is:%0t",actual_period);
     p=c;
    if(f_div==1)
      begin
         exp_period=1000000000000000fs/(freq*1000);
       
     end
    else if(f_div==2)
      begin
               exp_period=2000000000000fs/(freq*1000);
            end
    else if(f_div==4)
      begin
               exp_period=4000000000000000fs/(freq*1000);
            end
    else if(f_div==8)
      begin
               exp_period=8000000000000000fs/(freq*1000);
            end
   
     if((exp_period!= actual_period))
     begin
     $display("clock check FAILED for clkout_dig0 actual_period is :%0f expected period is:%0f f_div is %0d ",actual_period,exp_period,f_div);
    //`uvm_error($psprintf("actual_period is :%0t expected period is:%0t and f_div is %0d",actual_period,exp_period,f_div,$time),$psprintf("clock check failed for clkout_dig0!"));  
     end
     else begin
     $display("clock check PASSED for clkout_dig0 actual_period is :%0t expected period is:%0t f_div is %0d ",actual_period,exp_period,f_div);
     end
    end
  end

LOG
**clock check FAILED for clkout_dig0 actual_period is :6.400000 expected period is:6.400000 f_div is 1 period C is:281985100000

clock check FAILED for clkout_dig0 actual_period is :6.400000 expected period is:6.400000 f_div is 1**

In reply to chakrik:

Floating point numbers can not be compared directly because of rounding errors and representation. For example, 1/3 can not be stored exactly in a floating point number. For example, ((1.0 / 3.0) * 3.0) != 1.0

Take a step back. Do you really care if the two periods are only different by 1fs? 100fs? Build this into your comparison.

if((exp_period - actual_period) > 100fs)
  $error("clock check clkout_dig0 actual period %0f expected period %0f",
         actual_period, exp_period);

In reply to chrisspear:

Hi chrisspear,

Thank you for your reply, I would like to understand is there any workaround to compare float numbers, so that with precision I can compare the two clocks.

In reply to chakrik:

What problem are you trying to solve? If one clock period is 6.400000 and another is 6.400001, is this a failure? Is your hardware really accurate to 1 in 10 million?

Read up on floating point numbers, accuracy, and comparison. The next SystemVerilog standard will have real number comparison functions that have a delta amount.