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

**URL:** <https://verificationacademy.com/forums/t/getting-clock-period-mismatch-error-while-comparing-two-clocks-even-though-values-of-two-clocks-are-same/41668>\
**Category:** UVM\
**Tags:** UVM, Systemverilog-UVM-RAL, SV, clockcheck, SV-UVM-clockcheck\
**Created:** [May 16, 2023, 10:02am UTC](https://verificationacademy.com/forums/t/getting-clock-period-mismatch-error-while-comparing-two-clocks-even-though-values-of-two-clocks-are-same/41668 "2023-05-16T10:02:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![chakrik](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/c/d2c977/32.png) [@chakrik](https://verificationacademy.com/forums/u/chakrik)\
**Post date:** [May 16, 2023, 10:02am UTC](https://verificationacademy.com/forums/t/getting-clock-period-mismatch-error-while-comparing-two-clocks-even-though-values-of-two-clocks-are-same/41668/1 "2023-05-16T10:02:58Z")

</div>

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:**

```verilog
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\*\*

---

<div class="post-metadata">

**Author:** ![chrisspear](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/chrisspear/32/2702_2.png) [@chrisspear](https://verificationacademy.com/forums/u/chrisspear)\
**Post date:** [May 16, 2023, 12:10pm UTC](https://verificationacademy.com/forums/t/getting-clock-period-mismatch-error-while-comparing-two-clocks-even-though-values-of-two-clocks-are-same/41668/2 "2023-05-16T12:10:01Z")

</div>

_In reply to [chakrik](https://verificationacademy.com/forums/t/getting-clock-period-mismatch-error-while-comparing-two-clocks-even-though-values-of-two-clocks-are-same/41668):_

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.

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

```

---

<div class="post-metadata">

**Author:** ![chakrik](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/c/d2c977/32.png) [@chakrik](https://verificationacademy.com/forums/u/chakrik)\
**Post date:** [May 17, 2023, 5:23am UTC](https://verificationacademy.com/forums/t/getting-clock-period-mismatch-error-while-comparing-two-clocks-even-though-values-of-two-clocks-are-same/41668/3 "2023-05-17T05:23:24Z")

</div>

_In reply to [chrisspear](https://verificationacademy.com/forums/t/getting-clock-period-mismatch-error-while-comparing-two-clocks-even-though-values-of-two-clocks-are-same/41668/2):_

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.

---

<div class="post-metadata">

**Author:** ![chrisspear](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/chrisspear/32/2702_2.png) [@chrisspear](https://verificationacademy.com/forums/u/chrisspear)\
**Post date:** [May 17, 2023, 1:17pm UTC](https://verificationacademy.com/forums/t/getting-clock-period-mismatch-error-while-comparing-two-clocks-even-though-values-of-two-clocks-are-same/41668/4 "2023-05-17T13:17:37Z")

</div>

_In reply to [chakrik](https://verificationacademy.com/forums/t/getting-clock-period-mismatch-error-while-comparing-two-clocks-even-though-values-of-two-clocks-are-same/41668/3):_

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.
