Avoid understanding float, double, or decimal in C #.

Source: Internet
Author: User
It's always strange why a decimal is added to the pre-defined data type of C #. Isn't float and double enough? I am going to dig it out today. Floating Point Type

Name

CTS Type

Description

Significant Figures

Range (approximate)

Float

System. Single

32-bit single-precision floating point

7

± 1. 5 × 10 −45 to ± 3. 4 × 1038

Double

System. Double

64-bit double-precision floating point

15/16

± 5. 0 × 10 −324 to ± 1. 7 × 10308

If we write 12.3 in the Code, the compiler will automatically consider this number as a double type. So if we want to specify 12.3 As the float type, you must add F/f after the number:
Float f = 12.3F;

Decimal type

In addition, the decimal type is used to represent high-precision floating point numbers.

Name

CTS Type

Description

Significant Figures

Range (approximate)

Decimal

System. Decimal

128-bit high precision decimal notation

28

± 1. 0 × 10 − 28 to ± 7. 9 × 1028

From the table above, we can see that the decimal type has a large number of valid digits, reaching 28 BITs, but the data range is smaller than the float and double types. The decimal type is not the basic type in C #. Therefore, it may affect the computing performance.

We can define a floating point number of the decimal type as follows:
Decimal d = 12.30 M;

Knowledge of decimal, float, and double errors

Reference from: Ivony in http://topic.csdn.net/t/20050514/20/4007155.html

It is very dangerous to use floating point numbers in exact calculations, although C # takes many measures in floating point operations to make the results of floating point operations look very normal. However, if you do not know the characteristics of floating point numbers and use them rashly, it will cause serious risks.

Consider the following statement:

Double dd = 0000000000000000000000d;
Dd + = 1; // an error occurs even if it is 1000000, but 10000000 is correct.
Console. WriteLine ("{0: G50}", dd );

What is output? Who knows?
Output: 10000000000000000000000

This is the problem of floating point Precision loss. The most important thing is that when the precision loss occurs, no errors will be reported or any exceptions will occur.

The loss of floating point precision may occur in many places. For example, d * g/g is not necessarily equal to d, and d/g * g is not necessarily equal to d.

There are two very dangerous mistakes !!

1. decimal is not a floating point type or decimal type, and there is no loss of precision.
The following is a program for you to see what the result is. Remember! All floating-point variables have the problem of loss of precision, while decimal is a full floating-point type. No matter how high the precision is, the loss of precision still exists!

Decimal dd = 10000000000000000000000000000 m;
Dd + = 0.1 m;
Console. WriteLine ("{0: G50}", dd );

2. the number that decimal can store is larger than that of double. The conversion from double to decimal does not cause any problems.
Microsoft has to reflect on the help of decimal. In fact, only the transformation from an integer to decimal is an extended conversion. The precision of decimal is greater than that of double, but the maximum number that can be stored is smaller than that of double.

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.