Reputation: 324
I want to perform an integer division in Octave (I'm using the latest version 5.1.0
). I tired doing idivide(5, 2, "round")
which should produce 3
. The Documentation says for using idivide
with round
:
Calculate a ./ b with the fractional part rounded towards the nearest integer.
However, the result I get is 2.5000
. What am I doing wrong?
Edit: sorry I used the wrong parameter when initially writing the question. Calling the function with fix
works perfectly fine (e.g. idivide(5, 2, "fix")
returns 2
), I get the problem when using round
.
Upvotes: 3
Views: 4654
Reputation: 246
Edit: The below is important for Matlab, in Octave the originally posed question (fix
) gave the expected answer.
At least one of the input arguments must be an integer, hence this should work
idivide(5, int32(2), 'fix')
But be sure that this is what you want. idivide
will return an integer class object which has various consequences if you aren't expecting it.
For example compare with an alternative solution which returns a double
idivide(5, int32(2), 'fix')*0.9
ans =
int32
2
fix(5/2)*0.9
ans =
1.8000
Upvotes: 2