change integer division rounding default

6 次查看(过去 30 天)
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.

回答(1 个)

Ayush Laddha
Ayush Laddha 2020-6-18
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!

Translated by