草庐IT

floating-point-conversion

全部标签

json - 不同类型反射的 Golang JSON 数组 : float64 vs int64

考虑这个简单的例子:packagemainimport("encoding/json""fmt""log""reflect")varargs=`[1,2.5,"aaa",true,false]`funcmain(){varx[]interface{}err:=json.Unmarshal([]byte(args),&x)iferr!=nil{log.Fatalf("%s",err.Error())panic(fmt.Sprintf("%s",err.Error()))}for_,arg:=rangex{t:=reflect.TypeOf(arg).Kind().String()v:=r

go - 如何以正确的方式将 float64 数字更改为 uint64?

packagemainfuncmain(){varnfloat64=6161047830682206209println(uint64(n))}输出将是:6161047830682206208看起来当float64更改为uint64时,小数部分被丢弃。 最佳答案 这里的问题是常量和float的表示。常量以任意精度表示。float使用IEEE754表示标准。Spec:Constants:Numericconstantsrepresentvaluesofarbitraryprecisionanddonotoverflow.Spec:Nu

xml - 解码可选的 float64 字段在 Go 上返回错误

我的应用程序正在使用的外部API有时不会为其中一个float64字段返回任何值。发生这种情况时,我无法解码文档的其余部分。这是Goplayground中的示例代码:http://play.golang.org/p/Twv8b6KCtwpackagemainimport("encoding/xml""fmt")funcmain(){typeResultstruct{XMLNamexml.Name`xml:"Person"`Gradefloat64`xml:"grade"`Namestring`xml:"name"`}data:=`Jack`varvResulterr:=xml.Unmar

go - 如何将 float 转换为复数?

使用非常简单的代码:packagemainimport("fmt""math""math/cmplx")funcsqrt(xfloat64)string{ifx我收到以下错误消息:main.go:11:cannotconvertx(typefloat64)totypecomplex128我尝试了不同的方法,但找不到如何将float64转换为complex128(只是为了能够使用cmplx.Sqrt()负数函数)。处理这个问题的正确方法是什么? 最佳答案 您并不是真的想将float64转换为complex128,而是想构造一个comp

go - 接受所有数字类型(int、float、..)并将它们相加的函数

我想编写一个函数,它接受两个类型为int、int8、int16、...、的参数float和float64。参数类型不必匹配。此函数应采用两个参数并将它们相加。执行此操作的好方法是什么?基本上我想要这样的东西:funcf(ainterface{},binterface{})interface{}{returna+b}f(int8(1),int16(2))//3f(float64(2.2),int(1))//3.2有没有办法在Go中做到这一点?我愿意使用反射,但如果可能的话,我也想看看没有反射的方法。 最佳答案 要扩展@YandryPo

go - panic : interface conversion:Obj is not ObjInterface: missing method X

此刻,我陷入了这段代码:https://play.golang.org/p/r_HEVmpOuDpackagemainimport"fmt"type(Collectionstruct{Idstring}CollectionInterfaceinterface{Process(...string)})func(this*Collection)Process(params...string){this.Id="ok"}functestfunc(inputinterface{})CollectionInterface{inputCol:=input.(CollectionInterface)

go - "%b"在 fmt.Printf 中对 float64 有什么作用?二进制格式的 float64 中的 Min subnormal positive double 是什么?

GodocforPackagefmtFloating-pointandcomplexconstituents说:Floating-pointandcomplexconstituents:%bdecimallessscientificnotationwithexponentapoweroftwo,inthemannerofstrconv.FormatFloatwiththe'b'format,e.g.-123456p-78代码:fmt.Printf("0b%b\n",255)//0b11111111fmt.Printf("%b\n",1.0)//4503599627370496p-52什

go - 将 []float32 转换为 C *float

我想将指向slice[]float32的第一个元素的指针传递给C变量,但不知何故我不知道该怎么做。C代码:typedefstruct{constfloat*data_in;float*data_out;}SRC_DATA;开始:mySlice:=make([]float32,20)foo:=C.SRC_DATA{}foo.data_in=*C.float(&mySlice[0])//heretheprogrambreaks据我所知,应该可以直接将指向slice中第一个元素的指针从Go传递到C,而无需使用unsafe.Pointer()。然后C函数将遍历slice(长度已知)。感谢任何提

go - 将字符串转换为 float 会丢失精度吗?

问题将string转换为float64时,float64的小数部分会丢失大量数字。代码origVal:="0.00000628"convVal,err:=strconv.ParseFloat(origVal,64)iferr==nil{fmt.Printf("Originalvalue:%s\nConvertedvalue:%f\n",origVal,convVal)}输出:Originalvalue:0.00000628Convertedvalue:0.000006代码可在GoPlayground上获得:https://play.golang.org/p/a8fH_JGug7l上下文

arrays - 将 float64 数组转换为 float32 数组

我有一个float64数组,想将每个值转换为float32。我试过:#WhatIhavefeatures64[120]float64#WhatI'vetriedfeatures32=[120]float32(features64)但这会导致编译错误:cannotconvertfeatures(type[120]float64)totype[120]float32 最佳答案 您不能将一种slice/数组类型转换为另一种。您需要创建一个新数组并遍历转换每个元素的原始数组:fori,f:=rangefeatures64{features3