当前位置:网站首页>Floating point and complex type of go data type (4)
Floating point and complex type of go data type (4)
2022-06-28 03:32:00 【weixin_ fifty-nine million two hundred and eighty-four thousand】
floating-point
Floating point type is also called floating point number , Used to represent data containing decimal points , such as 3.14、1.00 All floating point data .
The representation of floating-point numbers
Go Floating point numbers in the language use IEEE-754 Standard expression , Two types are defined :float32 and float64, among float32 Is a single precision floating point number , It can be accurate to the decimal point 7 position ( similar PHP、Java Languages like float type ),float64 It's a double floating point number , It can be accurate to the decimal point 15 position ( similar PHP、Java Languages like double type ).
stay Go In language , The code to define a floating-point variable is as follows :
var floatValue1 float32
floatValue1 = 10
floatValue2 := 10.0 // If you don't add a decimal point ,floatValue2 Will be derived as an integer instead of a floating point
floatValue3 := 1.1E-10 For variables whose floating-point type needs to be derived automatically , Its type will be automatically set to float64, Whether or not the number assigned to it is in 32 Bit length . therefore , For the above example , The following assignment will cause compilation errors :
floatValue1 = floatValue2 // floatValue2 yes float64 type
The compilation error message is as follows :
cannot use floatValue2 (type float64) as type float32 in assignmentYou must use such a cast to :
floatValue1 = float32(floatValue2) In actual development , Use... As much as possible float64 type , because math All functions related to mathematical operations in the package will require to receive this type .
The precision of floating point numbers
Floating point numbers are not an exact expression , Because binary cannot accurately represent all decimal decimals , such as 0.1、0.7 such , Let's give you a visual demonstration through an example :
floatValue4 := 0.1
floatValue5 := 0.7
floatValue6 := floatValue4 + floatValue5notes : Floating point numbers operate like integers , Also make sure that the types of operands are consistent ,
float32andfloat64Type data cannot be mixed , Forced conversion is required manually , This is different from dynamic languages .
Do you think the above calculation results floatValue6 What's the value of ?0.8? No , Its result is 0.7999999999999999, This is because the bottom of the computer will be decimal 0.1 and 0.7 When converting to binary representation , Will lose precision , So never believe that floating-point results are accurate to the last place , Never compare two floating-point numbers for equality .
Floating point comparison
Floating point numbers are supported through Arithmetic operator Do four calculations , Also support passing Comparison operator Compare ( The premise is that the operand types on both sides of the operator are the same ), Except for comparisons involving equality , Because we mentioned above , Two decimal floating point numbers that look equal , Precision will be lost when converting to binary at the bottom , Therefore, we should not be blinded by appearances .
If we have to judge equality , Here is an alternative solution :
p := 0.00001
// Judge floatValue1 And floatValue2 Whether it is equal or not
if math.Dim(float64(floatValue1), floatValue2) < p {
fmt.Println("floatValue1 and floatValue2 equal ")
} You can see , Our solution is an approximate judgment , Through an acceptable minimum error value p, It is agreed that if the difference between two floating-point numbers is within the error range of this precision , Then determine that the two floating-point numbers are equal . This solution is also a common solution used by other languages to judge the equality of floating-point numbers .
Plural type
In addition to integer and floating-point types ,Go The language also supports plural types , Opposite the plural , We can call integer and floating-point numbers, which are common in daily life, real numbers , The complex number is an extension of the real number , You can use two real numbers ( In a computer, expressed as a floating-point number ) constitute , A representation of the real part (real), An empty part (imag), Common expressions are as follows :
z = a + biamong a、b All are real numbers. ,i It's called an imaginary unit , When b = 0 when ,z Real numbers are common , When a = 0 and b ≠ 0 when , take z Call it a pure imaginary number , If you understand the concept of plural numbers in mathematical concepts , All of these are well understood , Now let's look at the plural in Go Representation and use in language .
stay Go In language , The complex number supports two types :complex64(32 Real part and imaginary part ) and complex128(64 Real part and imaginary part ), The corresponding representation examples are as follows , It is consistent with the plural representation in mathematical concepts :
var complexValue1 complex64
complexValue1 = 1.10 + 10i // By two float32 A plural type of real numbers
complexValue2 := 1.10 + 10i // Same as floating point type , The default auto derived real number type is float64, therefore complexValue2 yes complex128 type
complexValue3 := complex(1.10, 10) // And complexValue2 Equivalent For a complex number z = complex(x, y), You can go through Go Language built-in functions real(z) Obtain the real part of the complex number , That is to say x, adopt imag(z) Obtain the imaginary part of the complex number , That is to say y.
Complex numbers support the same arithmetic operators as other numeric types . When you use == perhaps != When comparing complex numbers , Because the real part of the complex number is also a floating point type , Attention should be paid to the accuracy .
边栏推荐
猜你喜欢

17 `bs对象.节点名h3.parent` parents 获取父节点 祖先节点

2022安全员-C证考试题库模拟考试平台操作

项目实战!手把手教你 Jmeter 性能测试

Importer un fichier Excel, résoudre le problème de sauter les cellules vides et de ne pas lire, et avancer l'indice, et retourner Blank As NULL Red

剑指 Offer 47. 礼物的最大价值(DP)

Tencent games released more than 40 products and projects, including 12 new games

Tardigrade:Trino 解决 ETL 场景的方案

GAMES104 作业2-ColorGrading

Tardigrade: Trino's solution to ETL scenarios

调试利器 go-spew
随机推荐
Hot! Yolov6's fast and accurate target detection framework is open source (with source code download)
暴雨去哪儿?天气预报不准谁的锅?
Apache——阿帕奇简介
开口式霍尔电流传感器如何助力直流配电改造?
被校园暴力,性格内向的马斯克凄惨而励志的童年
Redis搭建集群【简单】
TypeScript 联合类型
数据库
剑指 Offer 49. 丑数(三指针法)
如何给Eclips自动添加作者,时间等…
Object class, and__ new__,__ init__,__ setattr__,__ dict__
2022电工(初级)复训题库及在线模拟考试
What is the best and safest software to download when buying stocks?
SSH框架的搭建(上)
一位博士在华为的22年(干货满满)
service实现类里面为何一直报红
kubernetes资源对象介绍及常用命令
導入Excel文件,解决跳過空白單元格不讀取,並且下標前移的問題,以及RETURN_BLANK_AS_NULL報紅
Summary of the use of composition API in the project
2022安全员-C证考试题库模拟考试平台操作