Division operators in VB. NET and division operators in C,

Source: Internet
Author: User
Tags integer division

Division operators in VB. NET and division operators in C,

The Division operators in VB. NET include:/(floating point Division) and \ (integer division)

There is only one division operator in C #:/(Division)

Division operators andDivision operator in C #There are many differences, so pay attention to the difference during use.

 

AboutVB. NETIntroduction to Division operators in (From MSDN):

/(Floating point Division): divide two numbers and return the result expressed as a floating point number.

The data type of the result depends on the type of the operand. The following table shows how to determine the Data Type of the result.

Operand Data Type

Result Data Type

Both expressions are Integer Data Types (SByte, Byte, Short, UShort, Integer, UInteger, Long, And ULong) Double
One expression is of the Single data type, and the other expression is not of the Double type.

Single

One expression is of the Decimal data type, and the other expression is not of the Single or Double type.

Decimal

Any expression is of the Double data type. Double
Before division, any integer numeric expression is extended to Double. If you assign the result to the integer data type, Visual Basic will try to convert the result from Double to this type. If the result is not suitable for this type, an exception is thrown. If DivisorOr DivisorIf the calculation result is null, it is regarded as zero. \ (Integer Division): divide two numbers and return results in integer form.The following table describes how to determine the Data Type of the result. Note that this table is symmetric. For a combination of Data Types of a given operand, The result data type is the same regardless of the sequence of the operands.
  Boolean SByte Byte Short UShort Integer UInteger Long ULong
Boolean Boolean SByte Short Short Integer Integer Long Long Long
SByte SByte SByte Short Short Integer Integer Long Long Long
Byte Short Short Byte Short UShort Integer UInteger Long ULong
Short Short Short Short Short Integer Integer Long Long Long
UShort Integer Integer UShort Integer UShort Integer UInteger Long ULong
Integer Integer Integer Integer Integer Integer Integer Long Long Long
UInteger Long Long UInteger Long UInteger Long UInteger Long ULong
Long Long Long Long Long Long Long Long Long Long
ULong Long Long ULong Long ULong Long ULong Long ULong

If either of the two operands of the \ operator is Decimal, Single, or Double, Visual Basic tries to convert it to Long before the operation and the result data type of the operation is Long. If Option Strict is On, a compiler error occurs. If Option Strict is Off, OverflowException may occur if the value is beyond the range of Long data type (Visual Basic. The conversion to Long is also subject to "four homes, six homes, five into two ". IfDivisorOrDivisorIf the calculation result is null, it is regarded as zero.

 

AboutC #Introduction to Division operators in (From MSDN):

/(Division): separates two data types and returns the data type with high precision among the divisor and divisor.

Before division is executed, the compiler will unify the Data Types of the divisor and divisor into excellent data types of both types. The type of the returned computation result is also a data type with high precision in both types. For example, the result of division of two integers is always an integer. If an integer and a Double type are mutually exclusive, the return result is Double type. However, you must note that Double and Decimal cannot perform arithmetic operations directly. You must explicitly unify the two types before performing operations, double and Decimal cannot be operated directly because implicit type conversion cannot be performed between the two.


The Division operator "/" in C language can be used exactly the same as the Division operator "/" in mathematics.

Not the same,
The difference lies in our division in mathematics. the numbers involved in the calculation do not take the type into account.

Division considerations in the program
For example
1/2 is an integer division, and the result is not 0.5, but 0, that is, the integer part.
1.0/2. This is 0.5.

If you want to get the result of a floating point number, you must consider making at least one of the two operands be a floating point number. Forcing a 1.0 operation is also an I method.

How to calculate division in C Language

What does the problem mean?
When calculating the result of a C-language arithmetic expression, especially division, you must note the data type conversion of the computing object during the calculation process.
Perform operations on data and variables of the same data type to maintain the original data type.
When data and variables of different data types are computed, the result is of a data type with high precision.
For example, the 1/2 result is 0.
The 1.0/2 result is 0.5.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.