Integer division with rounding option (2024)

Integer division with rounding option

collapse all in page

Syntax

C = idivide(A,B)

C = idivide(A,B,opt)

Description

example

C = idivide(A,B) divides each element of A by the corresponding element of B, rounded to the nearest integers toward zero. A and B must contain real numbers and at least one of them must belong to an integer class.

  • If A and B are arrays, then they must belong to the same integer class and have sizes that are compatible.

  • If A or B is a scalar double, then the other input must be an integer class, but not int64 or uint64. The idivide function then returns C as the same integer class.

example

C = idivide(A,B,opt) specifies an alternative rounding option: 'fix', 'floor', 'ceil', or 'round'. For example, idivide(A,B,'ceil') rounds the quotients to the nearest integers toward positive infinity. The default rounding option is 'fix'.

Examples

collapse all

Integer Division

Open Live Script

Create an integer array A and an integer scalar B. Divide each element of A by B, rounded to the nearest integers toward zero.

A = int16([-7 -4 7 11]);B = int16(10);C = idivide(A,B)
C = 1x4 int16 row vector 0 0 0 1

Divide Two Integer Arrays

Open Live Script

Create two integer arrays, A and B. Divide each element of A by the corresponding element of B, rounded to the nearest integers toward zero.

A = int64([-2 3]);B = int64([3 5]);C = idivide(A,B)
C = 1x2 int64 row vector 0 0

Rounding Options for Integer Division

Open Live Script

Create a scalar double A and an integer array B. Divide A by each element of B with the default rounding option 'fix'.

A = 2.0;B = int32([-3 3 4]);C = idivide(A,B)
C = 1x3 int32 row vector 0 0 0

Compare the results with other rounding options.

C = idivide(A,B,'floor')
C = 1x3 int32 row vector -1 0 0
C = idivide(A,B,'ceil')
C = 1x3 int32 row vector 0 1 1
C = idivide(A,B,'round')
C = 1x3 int32 row vector -1 1 1

Input Arguments

collapse all

ANumerator
scalar | vector | matrix | multidimensional array

Numerator, specified as a scalar, vector, matrix, or multidimensional array. Integer inputs A and B must be either the same size or have sizes that are compatible. For example, A is an M-by-N matrix, and B is a scalar or 1-by-N row vector. For more information, see Compatible Array Sizes for Basic Operations.

If A is a scalar double, then B must be an integer class, but not int64 or uint64.

Data Types: double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

BDenominator
scalar | vector | matrix | multidimensional array

Denominator, specified as a scalar, vector, matrix, or multidimensional array. Integer inputs A and B must be either the same size or have sizes that are compatible. For example, A is an M-by-N matrix, and B is a scalar or 1-by-N row vector. For more information, see Compatible Array Sizes for Basic Operations.

If B is a scalar double, then A must be an integer class, but not int64 or uint64.

Data Types: double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

optRounding option
'fix' (default) | 'floor' | 'ceil' | 'round'

Rounding option, specified as 'fix', 'floor', 'ceil', or 'round'.

  • 'fix' rounds to the nearest integers toward zero, which is equivalent to removing any digits after the decimal point.

  • 'floor' rounds to the nearest integers toward negative infinity.

  • 'ceil' rounds to the nearest integer toward positive infinity.

  • 'round' rounds to the nearest integers. If an element has a fractional part of exactly 0.5, then it rounds away from zero to the integer with larger magnitude.

Output Arguments

collapse all

C — Integer solution
scalar | vector | matrix | multidimensional array

Integer solution, returned as a scalar, vector, matrix, or multidimensional array. If either A or B is an integer data type, then C is the same integer data type.

Data Types: int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Tips

  • The function idivide(A,B,'round') is the same as A./B and B.\A for integer data types. The argument opt provides the rounding options for A./B and B.\A when dividing integers.

  • MATLAB® does not support complex integer division.

Extended Capabilities

This function fully supports tall arrays. Formore information, see Tall Arrays.

This function fully supports GPU arrays. For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).

Version History

Introduced in R2006a

See Also

ceil | floor | fix | round | rdivide | ldivide

Topics

  • Integers
  • Floating-Point Numbers

Commande MATLAB

Vous avez cliqué sur un lien qui correspond à cette commande MATLAB:

 

Pour exécuter la commande, saisissez-la dans la fenêtre de commande de MATLAB. Les navigateurs web ne supportent pas les commandes MATLAB.

Integer division with rounding option (1)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom (English)

Asia Pacific

Contact your local office

Integer division with rounding option (2024)
Top Articles
Latest Posts
Article information

Author: Zonia Mosciski DO

Last Updated:

Views: 6244

Rating: 4 / 5 (51 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Zonia Mosciski DO

Birthday: 1996-05-16

Address: Suite 228 919 Deana Ford, Lake Meridithberg, NE 60017-4257

Phone: +2613987384138

Job: Chief Retail Officer

Hobby: Tai chi, Dowsing, Poi, Letterboxing, Watching movies, Video gaming, Singing

Introduction: My name is Zonia Mosciski DO, I am a enchanting, joyous, lovely, successful, hilarious, tender, outstanding person who loves writing and wants to share my knowledge and understanding with you.