# Assignment to Int Variable Seems Improper

**URL:** <https://verificationacademy.com/forums/t/assignment-to-int-variable-seems-improper/32882>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog, systemverilog-datatypes, Real-datatypes\
**Created:** [February 20, 2017, 4:59pm UTC](https://verificationacademy.com/forums/t/assignment-to-int-variable-seems-improper/32882 "2017-02-20T16:59:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Karan](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/k/58f4c7/32.png) [@Karan](https://verificationacademy.com/forums/u/Karan)\
**Post date:** [February 20, 2017, 4:59pm UTC](https://verificationacademy.com/forums/t/assignment-to-int-variable-seems-improper/32882/1 "2017-02-20T16:59:49Z")

</div>

Here is the code.

```verilog
module test();
  int a = 256;
  int b;

  initial
  begin
    b = real'(a)/6;
    $display("b = %0d", b);
    b = a/6;
    $display("b = %0d", b);
  end
endmodule

// Output
b = 43
b = 42

```

Now as per 1800-2012 LRM, Topic 6.12.2,

> Real numbers shall be converted to integers by rounding the real number to the nearest integer, rather than  
> by truncating it. Implicit conversion shall take place when a real number is assigned to an integer. If the  
> fractional part of the real number is exactly 0.5, it shall be rounded away from zero.

Then why in the 2nd case, b is not getting value of the nearest integer (which is 43, because 256/6 = 42.67)?

---

<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 20, 2017, 5:16pm UTC](https://verificationacademy.com/forums/t/assignment-to-int-variable-seems-improper/32882/2 "2017-02-20T17:16:44Z")

</div>

_In reply to [Karan](https://verificationacademy.com/forums/t/assignment-to-int-variable-seems-improper/32882):_

Because a / 6 is integer division, and, from section 11.4.3:

> The integer division shall truncate any fractional part toward zero.

---

<div class="post-metadata">

**Author:** ![Karan](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/k/58f4c7/32.png) [@Karan](https://verificationacademy.com/forums/u/Karan)\
**Post date:** [February 21, 2017, 4:01am UTC](https://verificationacademy.com/forums/t/assignment-to-int-variable-seems-improper/32882/3 "2017-02-21T04:01:32Z")

</div>

_In reply to [sbellock](https://verificationacademy.com/forums/t/assignment-to-int-variable-seems-improper/32882/2):_

What does it mean by integer division? Does it mean that both numerator and denominator are integer?

---

<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 21, 2017, 4:09pm UTC](https://verificationacademy.com/forums/t/assignment-to-int-variable-seems-improper/32882/4 "2017-02-21T16:09:34Z")

</div>

_In reply to [Karan](https://verificationacademy.com/forums/t/assignment-to-int-variable-seems-improper/32882/3):_

Yes. C behaves the same way, although the C standard defines it a bit more explicitly than the SystemVerilog standard.

From C11:

> When integers are divided, the result of the / operator is the algebraic quotient with any  
> fractional part discarded. …This is often called “truncation toward zero”.

---

<div class="post-metadata">

**Author:** ![Karan](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/k/58f4c7/32.png) [@Karan](https://verificationacademy.com/forums/u/Karan)\
**Post date:** [February 21, 2017, 5:09pm UTC](https://verificationacademy.com/forums/t/assignment-to-int-variable-seems-improper/32882/5 "2017-02-21T17:09:07Z")

</div>

_In reply to [sbellock](https://verificationacademy.com/forums/t/assignment-to-int-variable-seems-improper/32882/4):_

Ok So Should the following both code give same results?

```verilog
module test();
  int b, tp;
  int d = 41;

  initial
  begin
    tp = ((d + 2) / 3);
    $display("tp = %0d", tp);
    tp = ((d + 2) / 3) - 0.5;
    $display("tp = %0d", tp);
  end
endmodule

```

1st one should truncate the results, because both numerator & denominator are integer. 2nd one will take the nearest integer, because subtracting 0.5 will make the result a real value.

Please comment on my understanding
