Skip to content

math: when use math.Modf get frac then conver int/int64 is inaccurate #62232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
conero opened this issue Aug 23, 2023 · 1 comment
Closed

math: when use math.Modf get frac then conver int/int64 is inaccurate #62232

conero opened this issue Aug 23, 2023 · 1 comment

Comments

@conero
Copy link

conero commented Aug 23, 2023

What version of Go are you using (go version)?

$ go version
go version go1.20.4 windows/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
set GO111MODULE=on
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\ksj\AppData\Local\go-build
set GOENV=C:\Users\ksj\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=D:\programdata\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=D:\programdata\go
set GOPRIVATE=
set GOPROXY=https://goproxy.cn,direct
set GOROOT=D:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=D:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.20.4
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=D:\conero\repository\conero\uymas\go.mod
set GOWORK=
set CGO_CFLAGS=-O2 -g
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-O2 -g
set CGO_FFLAGS=-O2 -g
set CGO_LDFLAGS=-O2 -g
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=C:\Users\ksj\AppData\Local\Temp\go-build253827529=/tmp/go-build -gno-record-gcc-switches

What did you do?

https://go.dev/play/p/phY1f6R4OU6

package main

import (
	"fmt"
	"math"
)

func main() {
	var fl float64 = 1.000000
	fmt.Printf("fl -> %d\n", int(fl))

	//cover
	fl = 3.01
	val, frac := math.Modf(fl)
	fracExt := frac * 100
	fmt.Printf("fl: %f => (%f, %f); (%d, %d)\n",
		fl, val, fracExt, int(val), int(fracExt))
}

What did you expect to see?

The calculated result '1.000000' should be 1 instead of 0

fracExt := frac * 100
//int(fracExt) --> int: 1 not 0

What did you see instead?

fl -> 1
fl: 3.010000 => (3.000000, 1.000000); (3, 0)
@merykitty
Copy link

fracExt is 0.9999999999999787 which is rounded down to 0 when converting to int. Playground

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Aug 23, 2023
@golang golang locked and limited conversation to collaborators Aug 22, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants