Details about the mathematics class C #, Math, floating point number (top)

Source: Internet
Author: User

C # language supports the 11 numeric types shown in the Table B-1, respectively integer, floating point and decimal.

In a c # program, an integer (no decimal point) is considered as an int type (unless its value is greater than the maximum int value). According to the data value, the data is regarded as uint, long, And ulong in turn. The number of decimal places is considered as a double value. This means (1.0). GetType () = typeof (double ).

You can use a prefix to indicate your intention.

U represents the meaning of unsigned. Because it is unsigned, u indicates that the data is positive or 0, and cannot be negative.

 

A: Check integer overflow.

Consider the following code:

Short s = 32767; s + = 1;

Ushort us = 0; us-= 1;

In the first case, a signed number plus 1 goes through its maximum value. Because the integer is stored in the memory, the result is-32768.

In the second case, an unsigned number is reduced to less than 0, and the result is 65535.

In these two examples, overflow and underflow occur respectively. To avoid this problem, you can use the checked keyword,

Or use the following compiler switch:

/Checked +

The default compiler switch corresponds to:/checked-

If you use the checked Keyword:

Short s = 32767;

Checked

{

S + = 1; // an OverFlowException will be generated.

}

 

So far, I have been introducing the runtime integer overflow check up or down. By default, the compiler will mark the overflow check up and down as an error, it has nothing to do with your compiler switch.

 

For example, the statement short s = 32767 + 1;

A compilation error is generated because the addition is evaluated during the compilation.

For example:

Const int i1 = 65536;

Const int i2 = 65536;

Int i3 = i1 * i2;

Because i1 and i2 are both const values, the compiler will try to evaluate i3 = i1 * i2 during compilation and encounter an upward overflow. Therefore, compilation errors may occur.

The compiler switch does not overwrite this line, but the unchecked keyword can overwrite this behavior.

Int i3 = unchecked (i1 * i2). It can be compiled normally.

 

B: decimal type

The decimal keyword indicates the 128-bit data type. Compared with the floating point type, the decimal type has higher precision and a smaller range, which makes it suitable for financial and currency computing. The approximate range and precision of the decimal type are shown in the following table.

It uses 16 bytes (128 bits) to store each value. The 128 bits are divided into 96 full bits, one symbol bit, and one can be in the range of 0 ~ Ratio between 28. In mathematics, this proportional factor is a 10 negative exponential power, indicating the number of the decimal place in the value.

For example, if a decimal number is defined as 12.34, the storage method of this number is Integer 0x4D2 (or 1234), and a proportional factor 2,

 

As long as a decimal has (or less) 28 valid digits and (or less than) 28 decimal places, the decimal data type can store it accurately. This is not true for floating point numbers !, If you define a float value equal to 12.34, it will be stored as 0xC570A4 (or 12939428) divided by 0x100000 (or 1048576), and this value is equal to 12.340000152587890625 or approximately 12.34. Even if you define a double value as equal to 12.34, it is stored as: 0x18AE147AE147AE (or 6 946 802 425 218 990) divided by 0x2000000 (or 562 949 953 421 ), it is about 12.34.

 

This is why you should use decimal to perform computation where you don't want the cents to appear and disappear mysteriously, because the floating point number is not accurate.

Floating-point data types are suitable for scientific and engineering applications, but are not expected by financial applications.

 

Decimal Constructor (excerpt)

Decimal (int iLow, int iMiddle, int iHigh, bool bNegative, byte byScale)

Example expression: new Decimal (123456789,0, 0, false, 5)

Creates a decimal type decimal: 1234.56789 .,

5 indicates that the decimal point starts from the left to the right. Move 5 digits.

If the last parameter is 1, it is 12345678.9.

If the last parameter is 2, it is 1234567.89.

If the last parameter is 3, it is 123456.789.

...

The maximum number of Decimal values is: new Decimal (-1,-1,-1, false, 0) = Decimal. MaxValue.

The minimum Decimal value close to 0 is: new Decimal (0.0000000000000000000000000001, 0, false, 28) = 1*10-28.

If this number is divided by 2 in the C # program, the result is 0.

I have already pointed out why floating point notation is always an approximate value. When you start performing arithmetic operations on floating-point numbers, the approximation may be more serious.

For example, almost everyone who uses floating-point numbers knows that a 4.55 number is often stored as 4.549999 or 4.550001.

Decimal representation is much better. For example, suppose m1 is defined

Decimal m1 = 1, 12.34

In the internal m1, there is an integer part 1234 and a proportional factor 2, assuming that m2 is defined

Decimal m2 = 56.789;

The integer part is 56789 and the proportional factor is 3. Now we add the two numbers:

Decimal m3 = m1 + m2;

Internally, the integer part of m1 is multiplied by 10 (12340), and the proportional factor is set to 3. now the integer is directly added: 12340 + 56789 equals 69129, the proportional factor is 3, and the actual value is 69. 129. each number is accurate.

Now multiply the two numbers:

Decimal m4 = m1 * m2;

Internally, the two integers are multiplied (1234*56789 equals 70 077 626), and the proportional factor is added (2 + 3 equals 5 ). The actual result is 70.077626. This time, the computation is also accurate.

When division .... oh, My god, division is messy, no matter how you do it, but in most cases, you can use Decimal to better control the accuracy and accuracy of the results.

 

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.