Intel® Quartus® Prime Software
Intel® Quartus® Prime Design Software, Design Entry, Synthesis, Simulation, Verification, Timing Analysis, System Design (Platform Designer, formerly Qsys)

integer arithmetic

Altera_Forum
Honored Contributor II
1,247 Views

I'm getting weird results when I do integer division.  

 

Frequency is being read in as 60 but the result being stored in var_1b /= 1e9/60 (~1.6E7). Instead the result reported by temp2 is 20522. The division for var_1a is performed correctly. 

 

The offending code: 

 

shared variable frequency: integer range 0 to 25000 :=0; 

shared variable var_1a, var_1b, var_1c, var_1d: integer; 

... 

frequency := (to_integer(unsigned(Fin))); 

var_1a := (1E9/pi_2); 

var_1b := (1E9/(frequency)); 

temp <= std_logic_vector(to_unsigned(frequency, 16)); 

temp2 <= std_logic_vector(to_unsigned(var_1b, 16));
0 Kudos
3 Replies
Altera_Forum
Honored Contributor II
501 Views

 

--- Quote Start ---  

Instead the result reported by temp2 is 20522 

--- Quote End ---  

Yes, that's expectable when you cast the divison result to 16 Bit unsigned. :( 

 

At least 24 Bits are needed to hold the result.
0 Kudos
Altera_Forum
Honored Contributor II
501 Views

That was the issue. Rookie mistake.

0 Kudos
Altera_Forum
Honored Contributor II
501 Views

another apparent rookie mistake - the use of shared variables - why?

0 Kudos
Reply