我爱占星网 我爱占星网
首页
编程
java
php
前端
首页 编程 java php 前端

Golang浮点运算[重复]

This question already has an answer here:

  • Is floating point math broken? 30 answers

How does the float arithmetic work in Go

Playground link

package main

import "fmt"

func main() {
    j := 1.021
    fmt.Println(j)
    k := j*1000
    fmt.Println(k)
    l := int(k)
    fmt.Println(l)
}

Output:
1.021
1020.9999999999999
1020

I was expecting 1021 to be printed, but I got 1020

</div>

Go, like everyone else, uses IEEE-754 binary floating-point arithmetic. Floating-point arithmetic is inexact for some decimal values.

References:

Floating point

IEEE floating point

近期文章

  • Go-尝试创建超过一定数量的goroutine时出现细分违规问题
  • 如何先声明struct然后再在switch语句中对其进行初始化?
  • 去解析字符串时间
  • 执行nodejs并获得错误输出
  • 去,没有得到字符串值
  • 在golang中晃来晃去的goroutine
  • 通过指针获取值
  • 如何使用Go newRequest方法
  • 运行时错误:“分配给nil映射中的条目”
  • 在GoLang和Rust中初始化字符串数组
  • godoc本身的源代码在哪里? [关闭]
  • 我应该使用os.Open使用log.Panic()还是log.Fatal()吗?
  • RSA Javascript加密和Golang解密
  • 如何优雅地使切片附加安全
  • 函数接收器的地址因方法而异
  • ctx超时时,如何完全终止正在运行的go func()?
  • 将shell输出转换为float64?
  • 比较接口为零
  • Golang PutItem DynamoDB:运行时错误无效的内存地址或nil指针取消引用
  • 如何模拟接口实现

Copyright ©2022 我爱占星 All Rights Reserved.

浙ICP备2022030071号-1

部分图文来自网络,如有侵犯您的版权,请告诉我们删除

友情链接:代码精华