The common functions of computer and some simple processing of precision __ function

Source: Internet
Author: User
Tags acos asin cos sin
Double Hypot (double x,double y)/return the length of a right-angled triangle hypotenuse (z)
double fabs (double x)//returns the absolute value double
log (double x)//of a double-precision parameter X Returns the value of Logex  
double log10 (double x)/return the value of log10x double
pow (double x,double y)/return x^y value
double pow10 (int p)// Returns a value of 10p
double sqrt (double x)//returns the radical double ACOs (double x) of X to
return the inverse cosine of x, X to radians
double asin (double x)/ Returns the inverse chord value of x, X is radians
double atan (double x)//returns the tangent value of x, X is radians
double atan2 (double y,double x)/return y/x's tangent value, Y's X is radians   
Double cos (double x) returns the cosine cos (x) value of x, and X is radians
double sin (double x) returns the sine sin (x) value of x, and X is radians   
double tan (double   x)//Returns the tangent tan (x) value of x, and X is radians

Above are some of the functions in the math header file


The precision processing part is reproduced part

1. Why do floating point numbers have precision problems:

Floating point numbers (C + +), generally used more is float, double.

Number of bytes

Range of values

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 precision requirements are not very low, generally choose double. 14-bit precision (the number of digits in a valid digit, not a decimal point) is usually sufficient. Note that the problem is that the number of data precision digits reached 14 digits, but some floating-point operations result in precision and can not achieve this high, may be accurate results only 10~12 bit around. What about the lower few. Nature is the unpredictable number. This brings us to the problem that even the theoretically identical values, because they are obtained through different computational processes, may (in general) be different in the lower few. This phenomenon does not seem to have much effect, but it will have a fatal effect of the operation: = =. Well, that's the same judgment. Note that the = = = = = of the floating-point number in C + + requires exactly the same to return true. Look at the following example:

#include <stdio.h>

#include <math.h>

int main ()

{

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

Double b = acos (-1.0);

printf ("A =%.20lf\n", a);

printf ("B =%.20lf\n", b);

printf ("A-b =%.20lf\n", a-b);

printf ("a = = =%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 EPS to assist in judging the equality of floating-point numbers.

2. eps

The EPS abbreviation is epsilon, representing a small amount, but this small quantity is also guaranteed to be much larger than the uncertainty of the floating-point operation result. The most common value for EPS is about 1e-8. After the introduction of EPS, we judge the two floating-point number A, B is equal to the following way:

The three export functions are defined as follows: int SGN (double a) {return a <-eps? -1:a < EPS? 0:1;}

The calculation of the size of the various judgments should be amended as follows:

Traditional meaning

Fix the wording 1

Fix the wording 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 floating-point numbers that are very close to be equal, and the number of those that are really different (the difference is larger than the EPS) is not equal.

PS: nurturance good habit, try not to do the floating point to do = = Judge. For example, there is no = = in my correction 2.

3. EPS brings function out of bounds

If sqrt (a), ASIN (a), ACOs (a) are calculated and sent in by yourself, be careful.

If a is supposed to be 0, the floating-point error may actually be a small negative number (such as 1e-12), so that sqrt (a) should be 0, directly because a does not define the field.

Similarly, if a is supposed to be ±1, then ASIN (a), ACOs (a) may also have an error.

Therefore, for this function, a must be corrected beforehand.

4. Output Trap I

This section, like the next section, is caused by problems that require the output of floating-point numbers. And it's all about rounding.

When it comes to rounding, let's get back to the relevant content, as far as I know there are three common methods:

1. printf ("%.3lf", a); Keep a three decimal place, rounded by fourth digit

2. (int) A; Put a in 0 to get the whole

3. Ceil (a);   Floor (a); As the name suggests, take up the evidence, down to the whole. Note that both functions return a double, not an int

The first of these is very common in the output (nonsense ...).

Now consider a situation where the title requires the output to remain 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 encounter will be the same as the words in parentheses.

The solution is if a is positive, the output is a+eps, otherwise the output a-eps

Typical case: POJ2826

5. Output Trap II

ICPC problem output has an unwritten rule (sometimes written), do not output:-0.000

So first we have to figure out what happens when you press printf ("%.3lf\n", a) output.

Give the result directly: A∈ ( -0.000499999 ...,-0.000 ...). 1)

So, if you find that a falls in this range, please output 0.000 directly. A more insurance approach is to use sprintf to directly determine if the output is not-0.000 to be processed again.

Typical case: UVA746

6. Range of boundaries

This is strictly not part of the precision category, but dine is still possible. Note that although a double can represent a wide range of numbers, it is not as large as it is, it is said that the maximum is 1e308. So sometimes you have to be careful, such as when making a multiplication, when necessary to replace the logarithm of the and.

Typical case: HDU3558

7. About Set<t>

Sometimes we might have this kind of requirement to insert a floating-point number, to query whether or not to insert an operation. A handwritten hash table is a method (as the hash function is designed to be careful), but set is not more convenient. But set seems to be based on the weight of = =. Looks like it won't work. After observation, set is not by = = To judge the equality, is carried out by <, specifically, as long as a<b and B<a are not established, think A and B equal, you can find,

If the is less than defined as: BOOL operator < (const dat DAT) Const{return val < dat.val-eps;} will be able to solve the problem. (the base type cannot overload the operator, so the following is encapsulated)

8. Input values fluctuate too much

This is not a common situation, but it can help you become more familiar with EPS. If a question input says, give a floating-point number A, 1e-20 < a < 1e20. How dare you make EPS with 1e-8? It is reasonable to scale the EPS to the appropriate size according to the input scale.

Typical case: Hustoj 1361

9. A number of recommendations

The functions that tend to produce large floating-point errors are ASIN and ACOS. Try to use atan2.

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.