Accuracy in computational ry

Source: Internet
Author: User
Tags acos asin
Xh176233756: xh176233756

The biggest headache of geometric computing is the large amount of code and the high accuracy. The problem of code size is generally not a problem as long as you pay attention to the accumulation of templates at ordinary times. Precision Problems are hard to say. Sometimes a precision problem may become a bottleneck of a question ". Over the years, the questions have basically evolved towards a more and more non-card precision, but there are also some % ^ & % questions # $ % $ ^. In addition, some common sense doesn't matter whether the questions are stuck or not, all of them should be known. Today, I will review the accuracy problems that I have seen and have an impression on. Because I have limited experience and memory, I hope you will not be able to add them after your aim. In addition, in order to make up for my lack of ideas, I may mess up some irrelevant or well-known things. So, start now.

 

The accuracy of computational ry is actually a floating point number, But I think "computational ry" is more eye-catching than "floating point number", so I chose this title.

 

1. Why are there Precision Problems with floating point numbers:

Floating Point Number (in C/C ++). Generally, float and double are used.

 

Number of characters

Value Range

Decimal precision digits

Float

4

-3.4e-38 ~ 3.4e38

6 ~ 7

Double

8

-1.7e-308 ~ 1.7e308

14 ~ 15

If the memory is not very tight or the accuracy requirement is not very low, double is generally used. The precision of 14 digits (which is a valid digit, not the digits after the decimal point) is usually enough. Note: The problem is that the number of data precision digits reaches 14 bits, but the precision of some floating point operations is not as high as that, and the accuracy may be only 10 ~ About 12 digits. How many lower? Naturally, this is an unpredictable number. This brings us the problem that even if the same value is obtained through different calculation processes, it is possible (generally) That they have a few lower digits. This phenomenon does not seem to have much influence, but it has a fatal impact on an operation: =. Well, it means that the judgment is equal. Note: The = of a floating point in C/C ++ must be exactly the same before true is returned. Let's look at the example below:

# Include <stdio. h>

# Include <math. h>

Int main ()

{

Double A = Asin (SQRT (2.0)/2) * 4.0;

Double B = ACOs (-1, 1.0 );

Printf ("A = %. 20lf/N", );

Printf ("B = %. 20lf/N", B );

Printf ("a-B = %. 20lf/N", A-B );

Printf ("A = B = % d/N", A = B );

Return 0;

}

Output:

A = 3.14159265358979360000

B = 3.14159265358979310000

A-B = 0.00000000000000044409

A = B = 0

The solution is to introduce the EPS to help determine the equality of floating point numbers.

 

2. EPS

The abbreviation of EPS is Epsilon, indicating a small amount, but this small amount must be much larger than the uncertain amount of floating point operation results. The most common value of EPS is around 1-8. After the introduction of EPS, we can determine that two floating point numbers A and B are equal in the following way:

Define the Three-exit function: int SGN (double A) {return a <-EPS? -1: A <EPS? 0: 1 ;}

The following corrections should be made to all types of operations to determine the size:

Traditional meaning

Revision 1

Revision 2

A = B

SGN (a-B) = 0

FABS (a-B) <EPS

A! = B

SGN (a-B )! = 0

FABS (a-B)> EPS

A <B

SGN (a-B) <0

A-B <-EPS

A <= B

SGN (a-B) <= 0

A-B <EPS

A> B

SGN (a-B)> 0

A-B> EPS

A> = B

SGN (a-B)> = 0

A-B>-EPS

In this way, we can judge the number of floating-point numbers with very close differences as equal, and the numbers with a large difference (the difference value is greater than the EPS) as unequal.

PS: it is a good habit to avoid making = judgments on floating point numbers. For example, = is not displayed in my revision 2.

 

3. The function caused by EPS is out of bounds.

If you calculate and pass a in SQRT (A), asin (A), and ACOs (a) by yourself, be careful.

If a is supposed to be 0, it may be a negative number with a small absolute value (such as 1e-12) due to the floating point error, so SQRT (a) should be 0, an error occurs because a is not in the defined domain.

Similarly, if A is supposed to be ± 1, asin (A) and ACOs (a) may also encounter errors.

Therefore, for such a function, a must be corrected in advance.

 

4. Output trap I

This section is the same as the next section because the question requires the output of floating point numbers. It is also related to rounding.

Speaking of rounding, let's talk about the related content. As far as I know, there are three common methods:

1. printf ("%. 3lf", a); // keep the three decimal places of A, rounded to the fourth place.

2. (INT) A; // round a to 0

3. Ceil (a); floor (a); // as the name suggests, collect evidence up and round down. Note that both functions return double instead of Int.

Among them, the first is very common in output (nonsense ...).

Now we can consider a situation where the question requires that the output retain two decimal places. The exact answer to a case is 0.005, which should be 0.01, but your result may be 0.005000000001 (congratulations) or 0.004999999999 (tragedy ), if you follow printf ("%. 2lf ", a) Output, then your experience will be the same as the words in brackets.

Solution: If a is positive, A + EPS is output; otherwise, a-EPS is output.

Typical Case: poj2826

 

5. Output trap II

ICPC question output has an unwritten rule (sometimes written). Do not output:-0.000

First, we need to figure out when the result will be output by printf ("%. 3lf/N",.

The result is displayed directly: A, (-0.000499999 ......, -0.000 ...... 1)

Therefore, if you find that a falls within this range, you can directly output 0.000. It is safer to use sprintf to determine whether the output result is-0.000 and then process it.

Typical Case: uva746

 

6. Out-of-Range

This strictly does not belong to the category of precision, but it is acceptable to count. Please note that although the value range of double can be very large, it is not too big. As mentioned above, the maximum value is 1e308. So sometimes you have to be careful. For example, when making a concatenation, You need to replace it with the logarithm sum when necessary.

Typical Case: hdu3558

 

7. About set <t>

Sometimes we may need to insert floating point numbers and check whether they have been inserted. Writing a hash table is a method (the hash function should be designed with caution), but isn't set more convenient. But set seems to be weighted by =? It does not seem to work. After observation, set does not use = to judge whether the values are equal, but uses <. Specifically, as long as both a <B and B <A are not true, we can find that A and B are equal,

If the value of the value smaller than is defined as: bool operator <(const dat) const {return Val <dat. Val-EPS;}, the problem can be solved. (The basic types cannot be overloaded, so they are encapsulated)

 

8. The input value fluctuates too much.

This is not common, but it helps you become more familiar with EPS. If a question is entered, give a floating point number A, 1e-20 <A <1e20. Do you dare to use 1e-8 for EPS? It is reasonable to scale the EPS to a proper size according to the input size.

Typical Case: hustoj 1361

 

9. Some suggestions

Functions that are prone to large floating point errors include asin and ACOs. Try to use atan2.

In addition, if the data clearly indicates that it is an integer and the range is not large, using Int or long to replace double is an excellent choice, because there is no floating point error (although I almost always use double --!)

 

If the content is incorrect or other content is missing, please don't hesitate to give me some advice ~

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.