当JS遇到uint64:JS整数溢出问题

最近遇到一个问题,就是传了一个超级大的uint64,结果前端发现数字对不上,精度丢失了!原因就在于,JS的Number是 “64-bit floating point IEEE 754 number”。最大能表示的数是 Number.MAX_SAFE_INTEGER,一般来说,就是:

  • 2 ** 53 - 1, 或者
  • +/- 9,007,199,254,740,991, 或者
  • nine quadrillion seven trillion one hundred ninety-nine billion two hundred fifty-four million seven hundred forty thousand nine hundred ninety-one

总之,就是如果表示整数,最多能表示到 2 ** 53 - 1。解决方案,就是用string来表示,对于GIN来说,就是在定义的struct 中,uint64的tag里,加一个 string,比如如果原来是 json:"id",现在就是 json:"id,string"

package main

import (
	"log"

	"github.com/gin-gonic/gin"
)

type Demo struct {
	ID uint64 `json:"id,string"`
}

func bigNumHandler(c *gin.Context) {
	d := Demo{}
	if err := c.BindJSON(&d); err != nil {
		log.Printf("error: %s", err.Error())
		c.JSON(400, d)
		return
	}

	d.ID -= 2
	c.JSON(200, d)
}

func main() {
	route := gin.Default()
	route.POST("/big", bigNumHandler)
	route.Run(":8085")
}
$ http :8085/big id=18446744073709551615
HTTP/1.1 200 OK
Content-Length: 29
Content-Type: application/json; charset=utf-8
Date: Tue, 22 Jun 2021 02:51:10 GMT

{
    "id": "18446744073709551613"
}


$ http :8085/big id='18446744073709551615'
HTTP/1.1 200 OK
Content-Length: 29
Content-Type: application/json; charset=utf-8
Date: Tue, 22 Jun 2021 02:51:13 GMT

{
    "id": "18446744073709551613"
}

这个坑,比较难遇上,你得同时遇到:

  • 刚好你用了 uint64/int64
  • 刚好你的值大于 2 ** 53

以上就是这个坑的总结。



更多文章
  • GFS 论文阅读
  • MapReduce 论文阅读
  • 一起来做贼:Goroutine原理和Work stealing
  • Go语言的defer, panic和recover
  • 再读 Python Language Reference
  • 再读vim help:vim小技巧
  • 设计模式(2)- 深入浅出设计模式 阅读笔记
  • 设计模式(1)- 深入浅出设计模式 阅读笔记
  • Cython! Python和C两个世界的交叉点
  • socketserver 源码阅读与分析
  • functools 源码阅读与分析
  • contextlib代码阅读
  • Collections 源码阅读与分析
  • Redis通信协议阅读
  • 2016年就要结束了,2017年就要开始啦!