Floating point number 0 is not 0, I doubt life

table of Contents

1. The teacher told me that the denominator cannot be 0.

2. Why does switch have to add break?

3. Java 8 stream and limit


Record some recent pits and share them with you who often walk by the river.

1. The teacher told me that the denominator cannot be 0.

Scenario: A function of the project is a guessing function. The gold coins required for the guessing are based on the formula:

M+ N*0.5/ diffDays. M is a value planned and configured according to the player's level, N is the sum of income from a certain day to the current time, and diffDays is the number of days of income.

During the test, the students in the test found that the players still couldn't bet with 1.4 billion gold coins, which was strange. After debugging, it is found that diffDays is 0. Why is there no error when dividing by 0? Did the teacher tell me wrong?

Uncomfortable.

 

Reason: Double type data is directly divided by 0, 0 will be converted to double, and become 0.0. 0.0 is not 0 in memory, but an inaccurate number, which may be 0.00000000001 or other, so the result of dividing a positive number by 0.0 is Very big. So the maximum value of Long is returned.

2. Why does switch have to add break?

Scenario: When writing a gm command, all the gm of a function is defined in a protocol, and the case is used for distinguishing operations. One bit {} is the end of the statement, and multiple cases are executed at the same time. code show as below

 

Explore why? Want to know why everything starts from the source: bytecode

You can see that the switch case is compiled into Lookupswitch, the two cases are compiled into L2, L3, and the default L4 is automatically added.

Next, let’s see what happens when we join break?

 

After adding break, add L5 after L2, GOTO L4, L4 directly execute 18 lines, which is the end of the program, no longer execute the following case.

In addition to Lookupswitch and tableswitch, do you know the specific difference? I'll talk about it next time.

3. Java 8 stream and limit

Scenario: Recently, there is a function to record the information of the top three players in the arena, but only keep the last 5 seasons, and delete the redundant ones. I wrote the following code:

The result of running is:

Majesty: It's not the 5 seasons I envisioned at all, it hurts. Analyze the reason: Java 8 has written a lot, but some functions are still taken for granted. Because there are multiple seasons in this collection, 5 of them are selected in the final limit, but only 8 and 9 are finally obtained. How to solve it? To solve duplicate data, of course use distinct.

 

Summary: Moving bricks is not easy, but you still need to pay attention to details. What pits you have encountered, you can leave a message to share, remember to like and share.

1. Floating point number 0 is not 0, 2. switch can not forget the break, 3. limit is to filter all data.

Guess you like

Origin blog.csdn.net/perfect2011/article/details/108686065