In the development of HarmonyOS Next, floating point types are a key part of processing numerical calculations. Their characteristics are closely related to the IEEE 754 standard and play an important role in the field of scientific computing.As a technical expert who has been deeply engaged in related technical fields for many years, I will analyze the key points of floating point types in depth to help developers better master and use this important data type.
Chapter 1: Accuracy comparison
In the Cangjie language of HarmonyOS Next, floating point types cover Float16, Float32 and Float64, which correspond to the half-precision format (binary16), single-precision format (binary32) and double-precision format (binary64) in IEEE 754 respectively.Floating point types with different precisions have significant differences when decimals are represented. The specific accuracy range is shown in the table below:
|Floating point type|precision|number of significant digits (approximate)|
|---|---|---|
|Float16|About 3 decimal places|5-6 digits|
|Float32|About 6 decimal places|7-8 digits|
|Float64|About 15 decimal places|15 - 17 digits|
Taking the pi calculation as an example, we can intuitively feel the errors caused by different accuracy:
// Use Float16 to calculate the pi correlation value
let piFloat16: Float16 = 3.141f16
let circumference16: Float16 = 2.0f16 * piFloat16 * 5.0f16
println("Float16 calculated perimeter: \(circumference16)")
// Use Float32 to calculate the pi correlation value
let piFloat32: Float32 = 3.14159f32
let circumference32: Float32 = 2.0f32 * piFloat32 * 5.0f32
println("Float32 calculated perimeter: \(circumference32)")
// Use Float64 to calculate the pi correlation value
let piFloat64: Float64 = 3.141592653589793f64
let circumference64: Float64 = 2.0f64 * piFloat64 * 5.0f64
println("Float64 calculated perimeter: \(circumference64)")
From the above code running results, it can be seen that with the improvement of accuracy, the calculation results are closer to the real value, while the low-precision Float16 has a significantly larger error during the calculation process.In scientific computing scenarios with extremely high accuracy requirements, choosing the right floating point type is crucial.
Chapter 2: Primitive representation
Hexadecimal floating-point literals have a unique representation in Cangjie language. Taking 0x1.1p0 as an example, binary analysis of them will help us deeply understand the storage and operation principles of floating-point data.
In 0x1.1p0, 0x represents hexadecimal and 1.1 is the decimal part of hexadecimal.Convert the 1.1 of hexadecimal to binary, the integer part 1 to binary is 1, and the decimal part 0.1 to binary. By calculating 0.1 * 2 = 0.2, take the integer part 0, 0.2 * 2 = 0.4 to integer part 0, 0.4 * 2 = 0.8 to integer part 0, 0.8 * 2 = 1.6 to integer part 1, 0.6 * 2 = 1.2 to integer part 1... Continuous looping to get 0.000110011..., so 1.1 to hexadecimal to binary is approximately 1.000110011 (actual storage will be truncated according to accuracy).p0 means that the exponential part with base 2 is 0, that is 2^0 = 1.Finally, the floating point number represented by 0x1.1p0 is the binary 1.000110011 multiplied by 2^0, and converted to decimal to 1.0625.Understanding this kind of binary conversion and representation is very critical for optimizing numerical calculations and processing underlying data storage scenarios.
Chapter 3: Financial calculation avoids pitfalls
In financial computing scenarios, due to the extremely high accuracy requirements, ordinary floating point types have the risk of cumulative errors, which may lead to serious financial problems.For example, simple currency calculation:
let amount1: Float64 = 0.1
let amount2: Float64 = 0.2
let sum: Float64 = amount1 + amount2
println(sum) // The output may not be accurate 0.3, and there is an accuracy error
To solve this problem, the Decimal type is usually used instead of the normal floating point type.The Decimal type can accurately represent decimals through internal decimal storage.Here is a simple cumulative error test code to compare the calculation differences between Float64 and Decimal:
import std.decimal.*
func calculateWithFloat() {
var total: Float64 = 0.0
for (i in 0..10000) {
total += 0.1
}
println("Float64 cumulative calculation result: \(total)")
}
func calculateWithDecimal() {
var total: Decimal = Decimal(0)
for (i in 0..10000) {
total += Decimal(0.1)
}
println("Decimal cumulative calculation result: \(total)")
}
calculateWithFloat()
calculateWithDecimal()
It can be clearly seen from the test results that Float64's error gradually accumulates after a large amount of calculations, while the Decimal type always remains accurate.In core businesses such as interest rate calculation and amount trading in the financial field, the Decimal type must be used to ensure the accuracy of data and the stability of the business.
A deep understanding of the accuracy differences, division representations and correct application in financial computing can help developers avoid numerical calculation errors in HarmonyOS Next development, especially in the fields of scientific computing and finance. Choosing the right type and mastering its characteristics is the key to developing high-quality applications.
Top comments (0)