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

Golang中的固定cos数学函数是否有解决方案

I am trying to use cos function from math API in golang, but for some reason the function is returning an incorrect value. Is there a known solution for this problem?

Code:

    fmt.Println("cos(pi/2):", math.Cos(math.Pi / 2))
    fmt.Println("sen(pi/2):", math.Sin(math.Pi / 2))

Output:

cos(pi/2): 6.123233995736757e-17

sen(pi/2): 1

The cosine of pi / 2 is zero.

What you're seeing is just a slight floating point inaccuracy. It's correctly returning "almost zero".

See: What Every Computer Scientist Should Know About Floating-Point Arithmetic.

近期文章

  • 一个HTTP代理
  • Golang检测飞行中的请求
  • 关于Go语法的困惑
  • 调用接口函数时出错?
  • 如何在MuxChain中使用HttpRouter?
  • 如何在防范“切片边界超出范围”错误的同时轻松地在Go中获取子字符串?
  • 此代码使用类型的地址?
  • 在类型切换中使用strconv.FormatFloat()遇到麻烦
  • 测量执行时间,如果golang时间过长,则停止等待
  • Golang多次返回超载是地图类型独有的吗?
  • 如何实现BNF语法树来解析GO中的输入?
  • 如何从处理程序内部按名称调用路由?
  • 如何使用Go获取离地理坐标最近的城市?
  • 优化Go文件读取程序
  • 可以使用panic / recover作为测试成功类型断言的方法吗?
  • 我如何处理goroutine中的恐慌?
  • python 求m组数据的和
  • 如果gc是可选的,该怎么办? [关闭]
  • Go是否支持继承?
  • Go XML Unmarshal示例无法编译

Copyright ©2022 我爱占星 All Rights Reserved.

浙ICP备2022030071号-1

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

友情链接:代码精华