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

Integer division with rounding option? ›

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.

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.

Why does integer division round down? ›

Secondly, because the set of integers is not closed under division and there is no special infinite value for integers, integer division implicitly rounds the result. If both arguments are positive, the result is rounded down. For example, 1 / 2 evaluates to 0 and 5 / 3 evaluates to 1.

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.

How to round 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

Does integer round up or 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.

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 the symbol for rounding to integers? ›

⌊x⌉,[x], ⌊ x ⌉ , [ x ] , and ||x|| have all been used for the integer rounding function. Atlhough not per se discrete, I've found that using floor(x + 0.5) works quite well for integer rounding. If, however, you want to round, e.g., to the hundredths-place, then I might recommend floor((x * 100) + 0.5) / 100 instead.

How to round off integer value in C? ›

The round() functions round a floating-point number to the nearest integer value, regardless of the current rounding direction setting in the floating-point environment. If the argument is exactly halfway between two integers, round() rounds it away from 0. The return value is the rounded integer value.

What is the rule for rounding off numbers? ›

The general rule for rounding is as follows: If the number you're rounding has a 5, 6, 7, 8, or 9 after it, round it up. Example: 28 rounded to the nearest tens place equals 30. If the number you're rounding has a 0, 1, 2, 3, or 4 after it, round it down.

How do you estimate division by rounding off? ›

The roundoff rule can be used depending on the size of the number. The numbers can be rounded to the closest 10, 100, 1000, and so on. For example, if we want to divide 7898 by 9, then we can round off 7898 to 8000 and 9 to 10 to make calculations easy, and hence we can estimate the quotient.

Do you round when dividing? ›

Multiplication and Division

When multiplying or dividing numbers, find the number with the fewest significant figures, then round the result to that many significant figures. (The operand 32.0 is only known to three significant figures, so the result must be rounded to three significant figures.)

How does integer division work in C? ›

Division of integers in C rounds the result to an integer. The result is always rounded towards zero. ' % ' has the same operator precedence as ' / ' and ' * '. To do unrounded division, use floating point.

How does integer rounding work in C? ›

The round() functions round a floating-point number to the nearest integer value, regardless of the current rounding direction setting in the floating-point environment. If the argument is exactly halfway between two integers, round() rounds it away from 0. The return value is the rounded integer value.

What is the rule of division in C? ›

If you divide two integer type values, int or char, the result will be an integer that can be stored in int or char, depending on its size. If one or both of its operands is a floating point type, float or double, the result will be a floating point number that can be stored in float or double, depending on its size.

Does integer division truncate in C? ›

Integer division in C and C++ truncate towards zero. Sometimes rounding is more appropriate, but it's not trivial. The common solution is to convert to double, add 0.5 then perform floating point division and convert the result back to integer.

Top Articles
Latest Posts
Article information

Author: Ms. Lucile Johns

Last Updated:

Views: 6467

Rating: 4 / 5 (61 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Ms. Lucile Johns

Birthday: 1999-11-16

Address: Suite 237 56046 Walsh Coves, West Enid, VT 46557

Phone: +59115435987187

Job: Education Supervisor

Hobby: Genealogy, Stone skipping, Skydiving, Nordic skating, Couponing, Coloring, Gardening

Introduction: My name is Ms. Lucile Johns, I am a successful, friendly, friendly, homely, adventurous, handsome, delightful person who loves writing and wants to share my knowledge and understanding with you.