Menu
Coddy logo textTech

Integer And Real

Part of the Fundamentals section of Coddy's Verilog journey — lesson 8 of 90.

In addition to wire and reg, Verilog has other data types for numbers with different purposes.

Integer

integer is a general-purpose variable for counting and calculations.

  • Default size is 32 bits
  • Signed (can store negative numbers)
  • Used mainly in testbenches and for loops

Declaring an integer:

integer i;        // 32-bit signed variable
integer count;    // Can store negative numbers

Using integer:

integer i;

initial begin
  i = 0;           // Start at 0
  i = i + 1;       // Count up
  i = -5;          // Can store negative
  $display("i = %d", i);  // Prints: i = -5
end

Real

real is for floating-point numbers (numbers with decimals).

  • Stores fractional values
  • Used in testbenches for calculations
  • Cannot be synthesized to hardware

Declaring a real:

real pi;           // Floating-point variable
real voltage;

Using real:

real pi;

initial begin
  pi = 3.14159;               // Decimal value
  pi = 22.0 / 7.0;            // Division result
  $display("pi = %f", pi);    // Prints: pi = 3.142857
end

Where to Use Them

In testbenches: Integers and reals are commonly used for loop counters, calculations, and checking expected values. They make test code easier to write and understand.

In hardware: These types are usually not synthesizable—meaning they cannot be turned into actual circuits. For hardware that will go on an FPGA or chip, use reg and wire instead.

challenge icon

Challenge

Complete the code to make the testbench work.

What to do:

  1. Declare an integer called i (for loop counter)
  2. Declare a real called value (for calculation)

Cheat sheet

integer – 32-bit signed variable for counting/calculations (testbenches, loops):

integer i;
integer count;

initial begin
  i = 0;
  i = i + 1;
  i = -5;  // can store negatives
end

real – floating-point variable for fractional values (testbenches only, not synthesizable):

real pi;

initial begin
  pi = 3.14159;
  pi = 22.0 / 7.0;
  $display("pi = %f", pi);
end

Both integer and real are used in testbenches only. For synthesizable hardware, use reg and wire.

Try it yourself

module test;
  
  // Declare an integer called i

  
  // Declare a real called value

  
  initial begin
    i = 7;
    value = i * 0.5;
    $display("i = %0d, value = %f", i, value);
    
    $finish;
  end
  
endmodule
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals