首页 > 其他分享 >Go - floating points

Go - floating points

时间:2024-02-14 22:04:40浏览次数:23  
标签:exponent float64 mantissa points Go floating bits math

Note that there’s an infinite number of real values between math.SmallestNonzeroFloat64 (the float64 minimum) and math.MaxFloat64 (the float64 maximum). Conversely, the float64 type has a finite number of bits: 64. Because making infinite values fit into a finite space isn’t possible, we have to work with approximations. Hence, we may lose precision. The same logic goes for the float32 type.

 

Floating points in Go follow the IEEE-754 standard, with some bits representing a mantissa and other bits representing an exponent. A mantissa is a base value, whereas an exponent is a multiplier applied to the mantissa. In single-precision floating-oint types (float32), 8 bits represent the exponent, and 23 bits represent the mantissa. In double-precision floating-point types (float64), the values are 11 and 52 bits, respectively, for the exponent and the mantissa. The remaining bit is for the sign. To convert a floating point into a decimal, we use the following calculation:

sign * 2^exponent * mantissa

 

Once we understand that float32 and float64 are approximations, what are the implications for us as developers? The first implication is related to comparisons. Using the == operator to compare two floating-point numbers can lead to inaccuracies. Instead, we should compare their difference to see if it is less than some small error value. For example, the testify testing library (https://github.com/stretchr/testify) has an InDelta function to assert that two values are within a given delta of each other. Also bear in mind that the result of floating-point calculations depends on the actual processor. Most processors have a floating-point unit (FPU) to deal with such calculations. There is no guarantee that the result executed on one machine will be the same on another machine with a different FPU. Comparing two values using a delta can be a solution for implementing valid tests across different machines.

 

 

 

func main() {
    fmt.Printf("math.SmallestNonzeroFloat64:\n%.1080f\n\n", math.SmallestNonzeroFloat64)
    fmt.Printf("math.MaxFloat64:\n%.100f\n", math.MaxFloat64)
}

 

 

标签:exponent,float64,mantissa,points,Go,floating,bits,math
From: https://www.cnblogs.com/zhangzhihui/p/18015636

相关文章

  • Go - int overflow
     funcmain(){fmt.Printf("math.MaxInt32:%d\n",math.MaxInt32)fmt.Printf("math.MinInt32:%d\n",math.MinInt32)varcounterint32=math.MaxInt32counter++fmt.Pri......
  • Go 之烧脑的接口
    基本定义Go官方对于接口的定义是一句话:An interfacetypeisdefinedasasetofmethodsignatures.翻译过来就是,一个接口定义了一组方法的集合。这和Java和PHP的接口类似,定义一组方法而不定义方法的具体实现。但是与Java和PHP迥然不同的地方在于Go不需要显式的声......
  • Go 100 mistakes - #16: Not using linters
    Alinterisanautomatictooltoanalyzecodeandcatcherrors. Tounderstandwhylintersareimportant,let’stakeoneconcreteexample.Inmistake#1,“Unintendedvariableshadowing,”wediscussedpotentialerrorsrelatedto variableshadowing.Using......
  • Go 100 mistakes - #15: Missing code documentation
    Documentationisanimportantaspectofcoding.ItsimplifieshowclientscanconsumeanAPIbutcanalsohelpinmaintainingaproject.InGo,weshouldfollowsome rulestomakeourcodeidiomatic.First,everyexportedelementmustbedocumented.Wheth......
  • Go语言精进之路读书笔记第26条——了解接口类型变量的内部表示
    接口是Go这门静态语言中唯一“动静兼备”的语言特性接口的静态特性接口类型变量具有静态类型,比如:vareerror中变量e的静态类型为error支持在编译阶段的类型检查:当一个接口类型变量被赋值时,编译器会检查右值的类型是否实现了该接口方法集合中的所有方法接口的动态特性接......
  • Go - Project structure
    TheGolanguagemaintainerhasnostrongconventionaboutstructuringaprojectin Go.However,onelayouthasemergedovertheyears:project-layout(https://github.com/golang-standards/project-layout).Ifourprojectissmallenough(onlyafewfiles),......
  • Go 100 mistakes - #11: Not using the functional options pattern
      Here,WithPortreturnsaclosure.Aclosureisananonymousfunctionthatreferences variablesfromoutsideitsbody;inthiscase,theportvariable.Theclosurerespectsthe Optiontypeandimplementstheport-validationlogic.Eachconfigfieldr......
  • 8小时golang速成(五)Golang高阶 1、goroutine
    1、goroutine 协程并发协程:coroutine。也叫轻量级线程。与传统的系统级线程和进程相比,协程最大的优势在于“轻量级”。可以轻松创建上万个而不会导致系统资源衰竭。而线程和进程通常很难超过1万个。这也是协程别称“轻量级线程”的原因。一个线程中可以有任意多个......
  • Go 100 mistakes - #10: Not being aware of the possible problems with type embedd
     Becausethemutexisembedded,wecandirectlyaccesstheLockandUnlockmethods fromtheireceiver.Wementionedthatsuchanexampleisawrongusageoftypeembedding.What’s thereasonforthis?Sincesync.Mutexisanembeddedtype,theLockand......
  • 8小时速成golang(四)反射reflect 和 结构体标签
    编程语言中反射的概念在计算机科学领域,反射是指一类应用,它们能够自描述和自控制。也就是说,这类应用通过采用某种机制来实现对自己行为的描述(self-representation)和监测(examination),并能根据自身行为的状态和结果,调整或修改应用所描述行为的状态和相关的语义。每种语言的反射模......