Let’s talk about overcalculation! Is your calculation method overcalculated?

Let’s talk about overcalculation! Is your calculation method overcalculated?

Let’s talk about overcalculation! Is your calculation method overcalculated?

The game is about to be officially launched, and today I found a bug that made me laugh and cry. The data calculation overflowed; the gold recharged by the player became 0; this is a big deal, after all, no one can bear this responsibility;

Let me explain the reason. The development language is Java and the tool is NetBeans IDE 8.0.2

The player object has an attribute called gold which is of int type;

The calculation method when a player recharges is as follows.

  1.   int gold = 20000 ; //The player's original gold  
  2. int tempGold = 20000 ; //The current recharge amount of the player  
  3.  
  4. if (Integer.MAX_VALUE >= gold + tempGold) {
  5. gold = gold + tempGold;
  6. } else {
  7. gold = Integer.MAX_VALUE;
  8. }

It seems like there is nothing wrong with it, right? Of course, the above is a simulation;

If you are experienced, or can see some clues below, then it will be over-calculated;

Maybe you can't see any problem. I didn't find any problem at the beginning, so I wrote the code like this. Well, let's simulate the copy code

  1. int gold = Integer .MAX_VALUE - 1800; //The player's original gold
  2. int tempGold = 20000 ; //The current recharge amount of the player
  3. if (Integer.MAX_VALUE > = gold + tempGold) {
  4. gold gold = gold + tempGold;
  5. System.out.println("1");
  6. } else {
  7. gold = Integer.MAX_VALUE ;
  8. System.out.println("2");
  9. }

Well, guess what the output will be?

Maybe you will answer output 2. Yes, I thought it would output 2.

But why is the running result 1?

First, let's analyze why we thought it would output 2? Then it is obvious that we put

gold + tempGold

The calculation of these two values ​​is assumed to be long type and greater than Integer.MAX_VALUE

However, this is not the case. Let me take a look at the output. Copy code

  1. nt gold = Integer.MAX_VALUE - 1800 ; //The player's original  
  2. int tempGold = 20000 ; //The current recharge amount of the player  
  3. if (Integer.MAX_VALUE >= gold + tempGold) {
  4. gold = gold + tempGold;
  5. System.out.println( "1" );
  6. } else {
  7. gold = Integer.MAX_VALUE;
  8. System.out.println( "2" );
  9. }

  1. --- exec-maven-plugin: 1.2 . 1 :exec ( default -cli) @ game-gamesr ---
  2. 1  
  3. - 2147445449  
  4. --------------------------------------------------------------------------------
  5. BUILD SUCCESS

In Java, the addition of gold + tempGold does not become a long but a negative number.

Seeing this, maybe you will laugh at me. Well, I admit that I have not verified this problem. Fortunately, the game has not been launched yet. The problem was found in the test.

Anyway, now that I have discovered the problem and understand what the problem is, I will try to solve it.

That is, the problem of converting int to long

  1. int gold = Integer.MAX_VALUE - 1800 ; //The player's original  
  2. int tempGold = 20000 ; //The current recharge amount of the player  
  3. long tempLGold = tempGold;
  4. if (Integer.MAX_VALUE >= gold + tempLGold) {
  5. gold = gold + tempGold;
  6. System.out.println( "1" );
  7. } else {
  8. gold = Integer.MAX_VALUE;
  9. System.out.println( "2" );
  10. }
  11. System.out.println(gold + tempGold);

How about testing the current output?

  1. --- exec-maven-plugin: 1.2 . 1 :exec ( default -cli) @ game-gamesr ---
  2. 2  
  3. - 2147463649  
  4. --------------------------------------------------------------------------------
  5. BUILD SUCCESS

These are correct, ok. . The stupid things are over. But I found that there is an extra variable long tempLGold; the properties and operations are not very convenient. Is there any better operation?

  1. nt gold = Integer.MAX_VALUE - 1800 ; //The player's original  
  2. int tempGold = 20000 ; //The current recharge amount of the player  
  3. if (Integer.MAX_VALUE >= gold + tempGold + 0L) {
  4. gold = gold + tempGold;
  5. System.out.println( "1" );
  6. } else {
  7. gold = Integer.MAX_VALUE;
  8. System.out.println( "2" );
  9. }

Note the 0L at the end

Look at the output

  1. --- exec-maven-plugin:1.2.1:exec (default-cli) @ game-gamesr ---
  2. 1
  3. --------------------------------------------------------------------------------
  4. BUILD SUCCESS

The result is still output 1. Maybe you will laugh at me for being stupid. I am indeed stupid. After further investigation, I found out that it is a problem of operator priority.

Okay, then change it.

  1. int gold = Integer.MAX_VALUE - 1800 ; //The player's original  
  2. int tempGold = 20000 ; //The current recharge amount of the player  
  3. if (Integer.MAX_VALUE >= 0L + gold + tempGold) {
  4. gold = gold + tempGold;
  5. System.out.println( "1" );
  6. } else {
  7. gold = Integer.MAX_VALUE;
  8. System.out.println( "2" );

Output

  1. --- exec-maven-plugin: 1.2 . 1 :exec ( default -cli) @ game-gamesr ---
  2. 2  
  3. --------------------------------------------------------------------------------
  4. BUILD SUCCESS

Well enough

  1. int gold = Integer.MAX_VALUE - 1800 ; //The player's original  
  2. int tempGold = 20000 ; //The current recharge amount of the player  
  3. if (Integer.MAX_VALUE >= gold + tempGold * 1L) {
  4. gold = gold + tempGold;
  5. System.out.println( "1" );
  6. } else {
  7. gold = Integer.MAX_VALUE;
  8. System.out.println( "2" );
  1. nt gold = Integer.MAX_VALUE - 1800 ; //The player's original  
  2. int tempGold = 20000 ; //The current recharge amount of the player  
  3. if (Integer.MAX_VALUE >= gold + tempGold * 1L) {
  4. gold = gold + tempGold;
  5. System.out.println( "1" );
  6. } else {
  7. gold = Integer.MAX_VALUE;
  8. System.out.println( "2" );
  9. }

  1. --- exec-maven-plugin: 1.2 . 1 :exec ( default -cli) @ game-gamesr ---
  2. 2  
  3. --------------------------------------------------------------------------------
  4. BUILD SUCCESS

This is correct now.

These are the mistakes made by a programmer who has made mistakes...

<<:  Battery and memory have once again become Google's development focus?

>>:  Featured recommendation: Detailed explanation of the use of xUtils framework

Recommend

Get the operation guide of placing game ads on Toutiao today quickly!

Advertising is a process of selection Advertising...

11 common mistakes that new entrepreneurs make

[[155037]] The process of starting a business is ...

"Rice" thoughts! This is the first spring after Mr. Yuan left...

Qingming Festival is coming In the blink of an ey...

How much does it cost to rent a gigabit bandwidth server?

Gigabit bandwidth refers to resources with bandwi...

Can an “anti-inflammatory diet” really improve immunity?

This is the 4274th article of Da Yi Xiao Hu Recen...

If an owl lives in a cave and likes to poop...

What comes to mind when you see the picture below...

Short video live streaming skills

This time I will share with you some live broadca...

E-commerce operation plan: entry-level e-commerce operation plan from 0 to 1!

Many friends who do e-commerce often come to cons...

Three channels for App operation and promotion: online, offline and new media.

App operation is not easy, which depends on produ...

Operational Tips: How can a good product retain users?

Many bosses are very tired doing business. Why? A...