change integer division rounding default

6 views (last 30 days)
Andrea Carignano
Andrea Carignano on 18 Jun 2020
Answered: Ayush Laddha on 18 Jun 2020
In matlab and simulink int32(7)/int32(4) = 2.
I would like to have int32(7)/int32(4) = 1 like any other language (for example C).
Is there a way to change this default?
I know the function idivide, but this function is slower and I'd prefere to see the symbol "/" that is clearer.

Answers (1)

Ayush Laddha
Ayush Laddha on 18 Jun 2020
My understanding of your question is that you want to change the default answer received when we use division operator. You cannot alter the default operation but there are some other methods which you can use -
1.Use fixed-point numeric objects and settings.
F = fimath('RoundingMethod', 'floor');
A = fi([7], 1, 32, 0, F);
B = fi([4], 1, 32, 0);
ans = A/B;
Documentation links -
2. Use other datatypes like single/double.
floor(single(7)/single(4))
Or
floor(7/4) % By default, MATLAB stores all numeric variables as double-precision floating-point values
3. Use idivide function.
idivide(int32(7), int32(4))
Link to refer to the documentation of idivide function -

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!