Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
13 years ago

integer arithmetic

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));

3 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    --- 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.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

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