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

MATLAB-Befehl

Sie haben auf einen Link geklickt, der diesem MATLAB-Befehl entspricht:

 

Führen Sie den Befehl durch Eingabe in das MATLAB-Befehlsfenster aus. Webbrowser unterstützen keine MATLAB-Befehle.

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

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本 (日本語)
  • 한국 (한국어)

Contact your local office

Integer division with rounding option (2024)

FAQs

How do you round up integer division? ›

Rounding Modes For Integer Division
  1. Floor: A / B.
  2. Round: (A+B/2) / B.
  3. Ceiling: (A+B-1) / B alternately: (A-1) / B+1 (thanks @clift_m!)
Jul 21, 2022

What does round your answer to an integer mean? ›

Rounding a number involves replacing the number with an approximation of the number that results in a shorter, simpler, or more explicit representation of said number based on specific rounding definitions. For example, if rounding the number 2.7 to the nearest integer, 2.7 would be rounded to 3.

How do you round off an integer? ›

Rounding to the Nearest Integer

If the digit in the tenths place is less than 5, then round down, which means the units digit remains the same; if the digit in the tenths place is 5 or greater, then round up, which means you should increase the unit digit by one.

Does integer division round up or down in C? ›

If you merely assign an integer type to the value of a double, it will truncate the value to the integer part only. This always “rounds down” no matter what the mantissa of the floating point value is. So this works if rounding down is what you always want to do.

What is 329.54 to the nearest integer? ›

329.54 would be rounded off to 330. It is because after the decimal there is 5 and before, there is the number 9 which is odd.

What is an example of an integer answer? ›

What is an integer and what are examples of integers? An integer (pronounced IN-tuh-jer) is a whole number (not a fractional number) that can be positive, negative, or zero. Examples of integers are: -5, 1, 5, 8, 97, and 3,043. Examples of numbers that are not integers are: -1.43, 1 3/4, 3.14, .09, and 5,643.1.

What are integer answers? ›

An integer, also called a "round number" or “whole number,” is any positive or negative number that does not include decimal parts or fractions. For example, 3, -10, and 1,025 are all integers, but 2.76 (decimal), 1.5 (decimal), and 3 ½ (fraction) are not.

What is the symbol for rounding to integers? ›

⌊x⌉,[x], ⌊ x ⌉ , [ x ] , and ||x|| have all been used for the integer rounding function.

Do integers round down? ›

The Int and Trunc functions round a number to an integer (whole number without a decimal): Int rounds down to the nearest integer. Trunc truncates the number to just the integer portion by removing any decimal portion.

How do you round up to the whole integer? ›

The rounding up of numbers means if a number is given in the decimal form and followed by the numbers 5, 6, 7, 8, and 9 after the decimal point, then round the numbers up to the nearest whole number greater than the given one. For example, 14.682 = 15 (rounded up).

Do you round up in integer division? ›

Integer division rounds towards zero.

This means, for example, that the result of the integer division 7 / 5 would have the real number 1.4 as an intermediate result and rounded in the direction of 0 would result in 1 .

What happens in integer division? ›

In integer division and modulus, the dividend is divided by the divisor into an integer quotient and a remainder. The integer quotient operation is referred to as integer division, and the integer remainder operation is the modulus.

Why is division closed for integers? ›

The set of integers is not closed under the operation of division. because when one intger is divided by another integer,the result is not always an integer. For example, 4 and 9 both are integers, but 4 ÷ 9 = 4/9 is not an integer. Q.

How does Java round integer division? ›

So Does Java division round down? The answer is Yes. Java does a round down in case of division of two integer numbers.

How does Python round integer division? ›

In Python, we can perform floor division (also sometimes known as integer division) using the // operator. This operator will divide the first argument by the second and round the result down to the nearest whole number, making it equivalent to the math.

References

Top Articles
Latest Posts
Recommended Articles
Article information

Author: Twana Towne Ret

Last Updated:

Views: 6564

Rating: 4.3 / 5 (44 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Twana Towne Ret

Birthday: 1994-03-19

Address: Apt. 990 97439 Corwin Motorway, Port Eliseoburgh, NM 99144-2618

Phone: +5958753152963

Job: National Specialist

Hobby: Kayaking, Photography, Skydiving, Embroidery, Leather crafting, Orienteering, Cooking

Introduction: My name is Twana Towne Ret, I am a famous, talented, joyous, perfect, powerful, inquisitive, lovely person who loves writing and wants to share my knowledge and understanding with you.