Hello Astronomy

Automatic translation; Simplified Chinese is the reference text.

The calculator calculated 32°, why is the target in the opposite direction?

Two opposite directions can have the same slope. By leaving only a ratio, you're throwing away the symbols you really need.

General Algorithms and Numerical Computing

You know the target is at the bottom left, but the program draws an arrow pointing up and to the right. The calculation may be correct: if you divide two negative numbers first, the positive ratio obtained cannot tell you which quadrant the target is in.

Open interactive lesson →

Guess first

Is it possible that the y/x ratios of the two lines going up to the right and down to the left are the same?

Show answer and explanation

possible — The two components change signs at the same time, the ratio remains unchanged, but the direction differs by 180°.

Then connect experience and science

First identify the quadrant where the target is located

The position of the blue point is determined by x and y. The single-parameter atan only reads the ratio, and the two negative signs will cancel out in the division.

The return range of atan(y/x) is only −90° to 90°

Give the two components to atan2 respectively

atan2(y,x) retains the positive and negative signs and can distinguish between lower left and upper right; its parameter order must be confirmed according to the language document.

This project uses Math.atan2(y,x)

The direction is correct and the angle unit must be used.

JavaScript trigonometric function input is radians. If you give a degree like 212 directly to sin, it will be mistaken for 212 radians.

Radians = degrees × π/180

Don’t rely on false numbers to get past special circumstances.

The on-axis direction can be defined, but the zero vector cannot. Rounding down and truncation of negative numbers are also different, and calendar codes especially cannot be mixed.

atan2(0,0) has no geometric direction; floor(−7.83)=−8

Expand the calculation

θ弧Spend = atan2(y,x) θSpend = θ弧Spend × 180/π θ圈内 = ((θSpend mod 360)+360) mod 360

atan2 passes y first, then x; it retains quadrant information. There is no geometric direction when x=y=0, and even if the program returns a value, it cannot be used as a direction.

give it a try

First look at the example at the bottom left of the original book, and then change both components to positive numbers. The slope ratios are the same, but the directions must differ by half a turn; finally try the zero vector.

Take a look at it →

No need to memorize symbols

atan(y/x)
Just looking at the ratio, it is impossible to distinguish directions that are 180° apart.
atan2(y,x)
Keep two components respectively and judge the quadrant
θ / θπ/180
Angle value (degree)/corresponding radian value
floor / trunc
Rounding towards negative infinity / truncation towards zero are not the same operation

Algorithm, accuracy and scope of application

Supplementary Chapter 1 Correct Quadrant and Language Unit Trap. Using the original book A=−0.5712, B=−0.9139, the interface y=A, x=B; atan(A/B) is about 32° and the correct direction is about 212°. Single-parameter atan does not perform division demonstration when x=0; atan2 can still determine the direction on the axis, but (0,0) has no geometric direction, and you cannot use JavaScript atan2(0,0)=0 to forge the answer. The picture on the right compares the results after converting the same angle into radians and mistakenly giving the degrees directly to Math.sin; it also shows the range of 0–180° when only cos is inferred. Negative floor/trunc rounding is consistent with Chapter 7. The old introductory course on angles already had degrees minutes seconds minus sign, angle hours, and circles; it will not be repeated here. The math portal also implements the original book's large-angle normalization test, Horner polynomials, and summation of numerical periodic tables, and checks are consistent with direct expansion; it does not infer old model speed tests to modern JavaScript performance guarantees. The implementation of safe branches and iteration upper bounds can also be found in the general root finding course.

my understanding check

Which function is used in this project to preserve the signs of two components?

atan2(y,x) — atan2 receives y and x respectively without discarding the quadrant information first.

When θ is expressed in degrees, can Math.sin(θ) be used directly?

No, change the radian first — JavaScript's sin input is in radians.

Both components are 0, how should the correct direction be reported?

no definition — The zero vector has no direction; the number returned by the function cannot replace the geometric definition.

Keep the direction components and signs first, and then convert the units; the number given by the program does not mean that the number has the correct geometric meaning.

Jean Meeus · Astronomical Algorithms, 2nd ed. · No. 1 chapter

General Algorithms and Numerical Computing · All interactive courses