Reputation: 31
I want to write a function that returns mod 100
.
What's wrong with code below?
subtype int is integer range 0 to 255;
function div(val : int) return int is
variable result : int;
variable number : int;
begin
result := 0;
number := val;
while number >= 100 loop
result := result + 1;
number := number - 100;
end loop;
return result;
end div;
Upvotes: 0
Views: 134
Reputation: 12600
Edit: extend the answer to be more understandable.
In your function you return the quotient that is counted in result
, not the remainder that is left in number
. But this is what I would expect from a function named div()
.
If you want to return the remainder, you can use the same code, but change the last line into
return number;
And I would rename this new function to mod()
.
Upvotes: 1