In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of "how to solve the accuracy problem encountered by Golang in dealing with floating point numbers". The editor shows you the operation process through an actual case. The method of operation is simple and fast, and it is practical. I hope that this article "how to solve the accuracy problem encountered by Golang in dealing with floating point numbers" can help you solve the problem.
What is a floating point number?
A floating point number is a digital representation of a number that belongs to a specific subset of rational numbers and is used in a computer to approximately represent any real number. Floating-point numbers are mainly used to represent decimals in computers. Floating-point numbers are numbers where the decimal point can be changed.
Because in the machine language of the computer, there is only binary, the machine language can only recognize 0 and 1. Therefore, it is impossible for a computer to store decimals, so another flexible storage scheme is needed.
This kind of plan is:
1. Index scheme
Exponential form: the numerical part is a decimal, the number before the decimal point is zero, and the first digit after the decimal point is not zero. A real number can have multiple exponential representations, but only one belongs to the standardized exponential form.
12.31.23 * 10 ^-11.231.23 * 10 ^ 01.230.123 * 10 ^ 1
In the above table, we can clearly understand the index scheme. Similarly, we can find that there is a serious problem with expressing decimals in this way, that is, there are too many exponential representations, and if we can't agree on the only form, there will be problems in communication between different codes.
two。 Normalized exponential form
In a variety of representations of the exponential form, the representation in which the number before the decimal point is 0 and the first digit after the decimal point is not zero is called the standardized exponential form.
The normalized exponential form of 1.23 is 0.123 * 10 ^ 1
A real number has only a standardized exponential form, and when a program outputs a real number in exponential form, it must be output in a standardized exponential form.
0.123e001
1. Why start with 0?
1.23456 needs to store the whole number and the decimal part respectively for binary storage, while 0.123456 only needs to store the decimal part, so that the latter method can accommodate floating-point numbers with greater precision while occupying the same bytes.
two。 Why do you add 0 after e? is e001 the same as E1?
Followed by 0 is the output format of% e, which is not required for a standardized exponential form.
E001 and E1 are the same.
3.IEEE 754 standard
Because different machines have different representations of floating-point numbers, this is not conducive to the migration of software between different computers. For this reason, IEEE in the United States has proposed a representation method that supports floating-point numbers from a systematic point of view, which is called IEEE754 Standard (IEEE,1985), which is adopted by almost all popular computers today.
IEEE 754 specifies four ways to represent floating-point values: single accuracy (32 bits), double accuracy (64 bits), extended single accuracy (more than 43 bits, rarely used), and extended double accuracy (more than 79 bits, usually implemented in 80 bits). Only 32-bit mode is mandatory, and the rest are optional.
Second, the case of accuracy problems 1. Floating point addition and subtraction operation
Enter data:
A = 2.3329 b = 3.1234
The code is as follows (example):
Package mainimport "fmt" func main () {/ a = 2.3329 b = 3.1234 a, b: = 2.3329, 3.1234 c: = a + b fmt.Println (c) / / 5.456300000000001}}
There is a problem with the accuracy of the result.
2.3329 + 3.1234 = 5.456300000000001
Has made a mistake.
Conversion between 2.float64 and float32
Enter data:
A = 9.99999
The code is as follows (example):
Package mainimport "fmt" func main () {var a float32 a = 9.99999 b: = float64 (a) fmt.Println (b) / / 9.999990463256836}}
There is a problem with the accuracy of the result.
9.99999 = 9.999990463256836
Has made a mistake.
3.int64 and float64,int32 and float32 conversion
1.int32 and float32 conversion
Enter data:
A = 9.99999
The code is as follows (example):
Package mainimport "fmt" func main () {var an int32 a = 999990455 b: = float32 (a) fmt.Printf ("% f\ n", b) / / 999990464.000000}}
There is a problem with the accuracy of the result.
999990455 = 999990464.000000
Has made a mistake.
2.int64 and float64 conversion
Enter data:
A = 999999942424527242
The code is as follows (example):
Package mainimport "fmt" func main () {var an int64 a = 999999942424527242 b: = float64 (a) fmt.Printf ("% f\ n", b) / / 9999999424527232.000000}}
There is a problem with the accuracy of the result.
999999942424527242 = 99999942424527232.000000
Has made a mistake.
The 4.float64 bit is directly multiplied by 100.
Enter data:
A = 999999942424527242
The code is as follows (example):
Package mainimport "fmt" func main () {var a float64 a = 1128.61 b: = a * 100fmt.Println (b) / / 112860.99999999999}}
There is a problem with the accuracy of the result.
1128.61 * 100 = 112860.99999999999
Has made a mistake.
Third, decimal solves the problem of accuracy.
Using Decimal package to solve the problem of accuracy
Go get github.com/shopspring/decimal1. Floating point addition and subtraction operation
Enter data:
A = 2.3329 b = 3.1234
The code is as follows (example):
Package mainimport ("fmt"github.com/shopspring/decimal") func main () {/ / a = 2.3329 b = 3.1234 a, b: = 2.3329, 3.1234 c: = decimal.NewFromFloat (a) d: = decimal.NewFromFloat (b) fmt.Println (a, b) fmt.Println (c) D) fmt.Println ("ab is the same as cd at this time") fmt.Println (a + b) / / 5.456300000000001} fmt.Println (c.Add (d)) / / 5.4563}}
The accuracy of the result is no longer a problem.
Conversion between 2.float64 and float32
Enter data:
A = 9.99999
The code is as follows (example):
Package mainimport ("fmt"github.com/shopspring/decimal") func main () {var a float32 a = 9.99999 c: = decimal.NewFromFloat32 (a) b: = float64 (a) c.Float64 () fmt.Println (b) / 9.999990463256836} fmt.Println (c.Float64 ()) / / 9.99999}}
The accuracy of the result is no longer a problem.
The 3.float64 bit is directly multiplied by 100.
Enter data:
A = 999999942424527242
The code is as follows (example):
Package mainimport ("fmt"github.com/shopspring/decimal") func main () {var a float64 a = 1128.61 c: = decimal.NewFromFloat (a) b: = a * 100fmt.Println (b) / / 112860.99999999999} fmt.Println (c.Mul (decimal.NewFromInt (1128.61) / / 112861}}
The accuracy of the result is no longer a problem.
This is the end of the content about "how to solve the accuracy problem encountered by Golang in dealing with floating point numbers". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.