Skip to content

math/big: Float.Sqrt does not set accuracy #37915

@mohanson

Description

@mohanson

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

$ go version go1.13.8 linux/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
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/root/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/root/app/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/root/app/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build510748991=/tmp/go-build -gno-record-gcc-switches"

What did you do?

I was researching ieee 754, and when using go, I found that the big.Float.Sqrt function always output exact results. I calculate sqrt(pi) with go and cpp on one enviroment:

Go

package main

import (
	"log"
	"math/big"
)

func main() {
	pi := big.NewFloat(3.14159265)
	r := big.NewFloat(0).Sqrt(pi)
	log.Println(r, r.Acc())
}

Cpp

#include <iostream>
#include <cfenv>
#include <cmath>

#pragma STDC FENV_ACCESS ON

double pi = 3.14159265;

int main()
{
    std::feclearexcept(FE_ALL_EXCEPT);
    std::cout << "sqrt(pi) = " << sqrt(pi) << '\n';
    if(std::fetestexcept(FE_INEXACT)) {
        std::cout << "inexact result reported\n";
    } else {
        std::cout << "inexact result not reported\n";
    }
}

What did you expect to see?

golang: 1.7724538498928541 "InExact"
cpp     : sqrt(pi) = 1.77245, inexact result reported

What did you see instead?

golang: 1.7724538498928541 "Exact"
cpp     :  sqrt(pi) = 1.77245, inexact result reported

I don't think this is an accurate value, for sqrt(pi) = 1.7724538498928541 in go

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions