当前位置:网站首页>The boss said: whoever wants to use double to define the amount of goods, just pack up and go
The boss said: whoever wants to use double to define the amount of goods, just pack up and go
2022-07-04 01:49:00 【Tile grabbing engineering lion】
Let's look at the phenomenon first
Involving such as float perhaps double The processing of these two floating-point data , There will always be some Strange phenomenon , I wonder if you have noticed , Give me a few common chestnuts :
Typical phenomena ( One ): Conditional judgment exceeds expectations
System.out.println( 1f == 0.9999999f ); // Print :false
System.out.println( 1f == 0.99999999f ); // Print :true what ?
- 1.
- 2.
Typical phenomena ( Two ): Data conversion exceeded expectations
float f = 1.1f;
double d = (double) f;
System.out.println(f); // Print :1.1
System.out.println(d); // Print :1.100000023841858 what ?
- 1.
- 2.
- 3.
- 4.
Typical phenomena ( 3、 ... and ): The basic operation is better than expected
System.out.println( 0.2 + 0.7 );
// Print :0.8999999999999999 what ?
- 1.
- 2.
- 3.
Typical phenomena ( Four ): The data increased more than expected
float f1 = 8455263f;
for (int i = 0; i < 10; i++) {
System.out.println(f1);
f1++;
}
// Print :8455263.0
// Print :8455264.0
// Print :8455265.0
// Print :8455266.0
// Print :8455267.0
// Print :8455268.0
// Print :8455269.0
// Print :8455270.0
// Print :8455271.0
// Print :8455272.0
float f2 = 84552631f;
for (int i = 0; i < 10; i++) {
System.out.println(f2);
f2++;
}
// Print :8.4552632E7 what ? No +1 Did you? ?
// Print :8.4552632E7 what ? No +1 Did you? ?
// Print :8.4552632E7 what ? No +1 Did you? ?
// Print :8.4552632E7 what ? No +1 Did you? ?
// Print :8.4552632E7 what ? No +1 Did you? ?
// Print :8.4552632E7 what ? No +1 Did you? ?
// Print :8.4552632E7 what ? No +1 Did you? ?
// Print :8.4552632E7 what ? No +1 Did you? ?
// Print :8.4552632E7 what ? No +1 Did you? ?
// Print :8.4552632E7 what ? No +1 Did you? ?
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
You see , These simple scenarios are difficult to meet our needs , So use floating point numbers ( Include double and float) There are many problems to deal with Obscure pit Waiting for us !
No wonder the technical director made harsh remarks : Who dares to deal with things like The amount of goods 、 Order transaction 、 as well as Monetary calculation Time use floating point data (double/float), Just let us go !
What's the reason ?
Let's take the first typical phenomenon as an example to analyze :
System.out.println( 1f == 0.99999999f );
- 1.
Compare directly with code 1 and 0.99999999, Actually print out true!
What does that mean ? This shows that computers are not at all I can't tell These two numbers . Why is that ?
We might as well think briefly :
We know that these two floating-point numbers are only the specific values seen by human eyes , Is what we usually understand as a decimal number , But the bottom of the computer does not calculate according to the decimal system , Anyone who has learned the basic group counting principle knows , The bottom of the computer is ultimately based on things like 010100100100110011011 such 0、1 Binary to complete .
So in order to understand the actual situation , We should put these two decimal floating-point numbers Convert to binary space Take a look at .
Decimal floating point number to binary How to turn 、 How to calculate , I think this should belong to basic computer base conversion common sense , stay 《 The principle of computer organization 》 I must have learned in similar classes , I won't repeat it here , Give the result directly ( Convert it to IEEE 754 Single precision 32-bit, It's just float The precision corresponding to the type )
1.0( Decimal system )
↓
00111111 10000000 00000000 00000000( Binary system )
↓
0x3F800000( Hexadecimal )
- 1.
- 2.
- 3.
- 4.
- 5.
0.99999999( Decimal system )
↓
00111111 10000000 00000000 00000000( Binary system )
↓
0x3F800000( Hexadecimal )
- 1.
- 2.
- 3.
- 4.
- 5.
Sure enough , The underlying binary representation of these two decimal floating-point numbers is the same , It's no wonder that == The judgment result of returns true!
however 1f == 0.9999999f The result returned is expected , Print false, We also convert them to binary mode to see what happens :
1.0( Decimal system )
↓
00111111 10000000 00000000 00000000( Binary system )
↓
0x3F800000( Hexadecimal )
- 1.
- 2.
- 3.
- 4.
- 5.
0.9999999( Decimal system )
↓
00111111 01111111 11111111 11111110( Binary system )
↓
0x3F7FFFFE( Hexadecimal )
- 1.
- 2.
- 3.
- 4.
- 5.
Oh , Obviously , The binary representation of the two is really different , This is a logical result .
So why 0.99999999 The underlying binary representation of is :00111111 10000000 00000000 00000000 Well ?
This is not a floating point number 1.0 Binary representation of ?
This is about the precision of floating point numbers .
The precision of floating point numbers !
Did you learn 《 The principle of computer organization 》 Everyone in this class should know , The storage of floating-point numbers in the computer follows IEEE 754 Floating point count standard , It can be expressed by scientific counting as :
Just give : Symbol (S)、 Step code part (E)、 Mantissa part (M) The information of these three dimensions , The representation of a floating point number is completely determined , therefore float and double Floating point numbers are stored in the following two memory structures :
、 The symbolic part (S)
0- just 1- negative
2、 Step code part (E)( Index part ):
- about float Type floating point number , Index part 8 position , Consider positive and negative , Therefore, the index range that can be expressed is -127 ~ 128
- about double Type floating point number , Index part 11 position , Consider positive and negative , Therefore, the index range that can be expressed is -1023 ~ 1024
3、 Mantissa part (M):
The precision of a floating-point number is determined by the number of bits of the mantissa :
- about float Type floating point number , Mantissa part 23 position , Conversion to decimal is 2^23=8388608, So the decimal precision is only 6 ~ 7 position ;
- about double Type floating point number , Mantissa part 52 position , Conversion to decimal is 2^52 = 4503599627370496, So the decimal precision is only 15 ~ 16 position
So for the above values 0.99999999f, It's obviously more than float Precision range of type floating-point data , Problems are inevitable .
How to solve the problem of accuracy
So if it involves The amount of goods 、 Transaction value 、 Monetary calculation What should we do in such a scene that requires high accuracy ?
Method 1 : Use string or array to solve multi bit number problem
All the partners who have brushed the algorithm problems should know , Using strings or arrays to represent large numbers is a typical problem-solving idea .
For example, classic interview questions : Write the addition of two arbitrary large numbers 、 Subtraction 、 Multiplication and so on .
At this time, we can use strings or arrays to represent such large numbers , Then the specific calculation process is simulated manually according to the rules of four operations , We also need to consider various factors such as : carry 、 Borrow position 、 Symbol And so on , It's really complicated , This article does not elaborate .
Method 2 :Java The large number of categories is a good thing
JDK We have already considered the calculation accuracy of floating-point numbers , Therefore, it provides a special for high-precision numerical calculation Class of large numbers To facilitate our use .
In the foreword 《 To be honest with you , I recently talked to Java The source code is on the bar 》 Said in ,Java A large number of classes are located in java.math It's a bag :
You can see , frequently-used BigInteger and BigDecimal It is a sharp tool for dealing with high-precision numerical calculation .
BigDecimal num3 = new BigDecimal( Double.toString( 1.0f ) );
BigDecimal num4 = new BigDecimal( Double.toString( 0.99999999f ) );
System.out.println( num3 == num4 ); // Print false
BigDecimal num1 = new BigDecimal( Double.toString( 0.2 ) );
BigDecimal num2 = new BigDecimal( Double.toString( 0.7 ) );
// Add
System.out.println( num1.add( num2 ) ); // Print :0.9
// reduce
System.out.println( num2.subtract( num1 ) ); // Print :0.5
// ride
System.out.println( num1.multiply( num2 ) ); // Print :0.14
// except
System.out.println( num2.divide( num1 ) ); // Print :3.5
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
Yes, of course , image BigInteger and BigDecimal The operation efficiency of this large number class is certainly not as efficient as that of the native type , The price is still relatively expensive , The selection needs to be evaluated according to the actual scene .
by the way , Let's talk about it here , I'm currently on the job Java Development , If you are learning now Java, understand Java, Eager to be a qualified Java Development Engineer , At the beginning of learning Java The process of the lack of basic introductory video tutorial , You can pay attention to me :01. obtain . I have the latest Java Basic complete video tutorial .
边栏推荐
- Example 073 square sum value judgment programming requires the input of a and B, if a ²+ b ² If the result of is greater than 100, a is output ²+ b ² Value, otherwise output the result of a + B.
- Since the "epidemic", we have adhered to the "no closing" of data middle office services
- Gee: create a new feature and set corresponding attributes
- Why is the operation unsuccessful (unresolved) uncaught syntaxerror: invalid or unexpected token (resolved)
- Is Shengang securities company as safe as other securities companies
- Description of setting items of Jerry [chapter]
- Day05 branch and loop (II)
- Introduction to Tianchi news recommendation: 4 Characteristic Engineering
- How to view the computing power of GPU?
- From the 18th line to the first line, the new story of the network security industry
猜你喜欢
Feign implements dynamic URL
Small program graduation project based on wechat examination small program graduation project opening report function reference
Small program graduation project based on wechat reservation small program graduation project opening report reference
Do you know the eight signs of a team becoming agile?
Small program graduation design is based on wechat order takeout small program graduation design opening report function reference
Huawei rip and BFD linkage
Remember a lazy query error
A fan summed up so many interview questions for you. There is always one you need!
Small program graduation project based on wechat video broadcast small program graduation project opening report function reference
String & memory function (detailed explanation)
随机推荐
mysql使用視圖報錯,EXPLAIN/SHOW can not be issued; lacking privileges for underlying table
Huawei BFD and NQA
C library function int fprintf (file *stream, const char *format,...) Send formatted output to stream
Pyrethroid pesticide intermediates - market status and future development trend
Force buckle day32
Cancer biopsy instruments and kits - market status and future development trends
When the watch system of Jerry's is abnormal, it is used to restore the system [chapter]
C import Xls data method summary IV (upload file de duplication and database data De duplication)
Override and virtual of classes in C #
String & memory function (detailed explanation)
Do you know the eight signs of a team becoming agile?
Hunan University | robust Multi-Agent Reinforcement Learning in noisy environment
ES6 deletes an attribute in all array objects through map, deconstruction and extension operators
Solution to the problem that jsp language cannot be recognized in idea
Jerry's modification setting status [chapter]
Introduction to graphics: graphic painting (I)
Future source code view -juc series
Huawei cloud micro certification Huawei cloud computing service practice has been stable
IPv6 experiment
Introduction to superresolution