# Wrong result of div

**URL:** <https://verificationacademy.com/forums/t/wrong-result-of-div/32831>\
**Category:** SystemVerilog\
**Tags:** real-numbers, division, SystemVerilog\
**Created:** [February 9, 2017, 12:22pm UTC](https://verificationacademy.com/forums/t/wrong-result-of-div/32831 "2017-02-09T12:22:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![saritr](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/s/df705f/32.png) [@saritr](https://verificationacademy.com/forums/u/saritr)\
**Post date:** [February 9, 2017, 12:22pm UTC](https://verificationacademy.com/forums/t/wrong-result-of-div/32831/1 "2017-02-09T12:22:14Z")

</div>

I have the following member of in\_trx class:

```verilog
       bit signed [15:0] rand_data_xi;
       logic [3:0] fact_log_2;

```

And the followinf var:

```verilog
       bit signed [15:0] xi_prev = 0;
       bit signed [15:0] yi_prev = 0;

```

I do the following operation:

```verilog
      real ci_n = (in_trx.rand_data_xi - xi_prev)/ (2 ** in_trx.fact_log_2);
      predicted.xi = yi_prev + int'(ci_n);

```

For the following input:

```
in_trx.rand_data_xi = 16'hFFEE
xi_prev = 0
in_trx.fact_log_2 = 4

```

I get:

```
 ci_n = 4094

```

Instead of:

```
 ci_n =-1.125

```

I asume that in\_trx.rand\_data\_xi give for some reason unsigned instead of signed.

Why?

---

<div class="post-metadata">

**Author:** ![cgales](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/c/ccd318/32.png) [@cgales](https://verificationacademy.com/forums/u/cgales)\
**Post date:** [February 9, 2017, 1:45pm UTC](https://verificationacademy.com/forums/t/wrong-result-of-div/32831/2 "2017-02-09T13:45:51Z")

</div>

_In reply to [saritr](https://verificationacademy.com/forums/t/wrong-result-of-div/32831):_

Please review chapter 11 of the SystemVerilog LRM.

Per the LRM, if all of the operands are integral, then the result is integral. Even though ci\_n is declared as real, all of the operands in the statement (rand\_data\_xi, xi\_prev and fact\_log\_2) are integrals, resulting in the (correct) integral result of -1 instead of the real result -1.125.

---

<div class="post-metadata">

**Author:** ![saritr](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/s/df705f/32.png) [@saritr](https://verificationacademy.com/forums/u/saritr)\
**Post date:** [February 9, 2017, 1:59pm UTC](https://verificationacademy.com/forums/t/wrong-result-of-div/32831/3 "2017-02-09T13:59:17Z")

</div>

_In reply to [cgales](https://verificationacademy.com/forums/t/wrong-result-of-div/32831/2):_

But it returns 4096, not -1.

---

<div class="post-metadata">

**Author:** ![cgales](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/c/ccd318/32.png) [@cgales](https://verificationacademy.com/forums/u/cgales)\
**Post date:** [February 9, 2017, 3:25pm UTC](https://verificationacademy.com/forums/t/wrong-result-of-div/32831/4 "2017-02-09T15:25:33Z")

</div>

_In reply to [saritr](https://verificationacademy.com/forums/t/wrong-result-of-div/32831/3):_

test.sv:

```verilog

module test;

  bit signed [15:0] rand_data_xi;
  logic [3:0] fact_log_2;

  bit signed [15:0] xi_prev = 0;
  bit signed [15:0] yi_prev = 0;

  real ci_n;

  initial begin
    rand_data_xi = 16'hFFEE;
    xi_prev = 0;
    fact_log_2 = 4;
    ci_n = (rand_data_xi - xi_prev) / (2 ** fact_log_2);
    $display("rand_data_xi = ", rand_data_xi);
    $display("2 **fact_log_2 = ", 2** fact_log_2);
    $display("ci_n = ", ci_n);
  end

endmodule

```

qverilog test.sv:

# rand\_data\_xi = -18

# 2 \*\* fact\_log\_2 = 16

# ci\_n = -1

---

<div class="post-metadata">

**Author:** ![sbellock](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/s/f4b2a3/32.png) [@sbellock](https://verificationacademy.com/forums/u/sbellock)\
**Post date:** [February 9, 2017, 4:02pm UTC](https://verificationacademy.com/forums/t/wrong-result-of-div/32831/5 "2017-02-09T16:02:59Z")

</div>

_In reply to [saritr](https://verificationacademy.com/forums/t/wrong-result-of-div/32831/4):_

You must cast the integral portions to real (or shortreal) to get the desired outcome.

```verilog
ci_n = real'(rand_data_xi - xi_prev) / real'(2 ** fact_log_2);

```
