Skip to content

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

@conero

Description

@conero

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)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions