From 9f26d12a3ebc77652f2cc089d50a336cb9b6bf13 Mon Sep 17 00:00:00 2001 From: xushiwei Date: Mon, 28 Apr 2025 01:03:13 +0800 Subject: [PATCH] _cmptest: mathbigdemo --- _cmptest/_goconstdemo/goconst.go | 33 ++++++++++++++++++++++++ _cmptest/mathbigdemo/big.go | 25 ++++++++++++++++++ runtime/internal/lib/math/big/prime.go | 26 +++++++++++++++++++ runtime/internal/lib/runtime/compiler.go | 2 +- 4 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 _cmptest/_goconstdemo/goconst.go create mode 100644 _cmptest/mathbigdemo/big.go create mode 100644 runtime/internal/lib/math/big/prime.go diff --git a/_cmptest/_goconstdemo/goconst.go b/_cmptest/_goconstdemo/goconst.go new file mode 100644 index 00000000..692c393e --- /dev/null +++ b/_cmptest/_goconstdemo/goconst.go @@ -0,0 +1,33 @@ +package main + +import ( + "fmt" + "go/constant" + "go/token" +) + +func main() { + // Create the complex number 2.3 + 5i. + ar := constant.MakeFloat64(2.3) + ai := constant.MakeImag(constant.MakeInt64(5)) + a := constant.BinaryOp(ar, token.ADD, ai) + + // Compute (2.3 + 5i) * 11. + b := constant.MakeUint64(11) + c := constant.BinaryOp(a, token.MUL, b) + + // Convert c into a complex128. + Ar, exact := constant.Float64Val(constant.Real(c)) + if !exact { + fmt.Printf("Could not represent real part %s exactly as float64\n", constant.Real(c)) + } + Ai, exact := constant.Float64Val(constant.Imag(c)) + if !exact { + fmt.Printf("Could not represent imaginary part %s as exactly as float64\n", constant.Imag(c)) + } + C := complex(Ar, Ai) + + fmt.Println("literal", 25.3+55i) + fmt.Println("go/constant", c) + fmt.Println("complex128", C) +} diff --git a/_cmptest/mathbigdemo/big.go b/_cmptest/mathbigdemo/big.go new file mode 100644 index 00000000..9876c2e0 --- /dev/null +++ b/_cmptest/mathbigdemo/big.go @@ -0,0 +1,25 @@ +package main + +import ( + "fmt" + "math/big" +) + +func main() { + // Initialize two big ints with the first two numbers in the sequence. + a := big.NewInt(0) + b := big.NewInt(1) + + // Initialize limit as 10^99, the smallest integer with 100 digits. + var limit big.Int + limit.Exp(big.NewInt(10), big.NewInt(99), nil) + + // Loop while a is smaller than 1e100. + for a.Cmp(&limit) < 0 { + // Compute the next Fibonacci number, storing it in a. + a.Add(a, b) + // Swap a and b so that b is the next number in the sequence. + a, b = b, a + } + fmt.Println(a) // 100-digit Fibonacci number +} diff --git a/runtime/internal/lib/math/big/prime.go b/runtime/internal/lib/math/big/prime.go new file mode 100644 index 00000000..a794073f --- /dev/null +++ b/runtime/internal/lib/math/big/prime.go @@ -0,0 +1,26 @@ +// Copyright 2016 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package big + +// ProbablyPrime reports whether x is probably prime, +// applying the Miller-Rabin test with n pseudorandomly chosen bases +// as well as a Baillie-PSW test. +// +// If x is prime, ProbablyPrime returns true. +// If x is chosen randomly and not prime, ProbablyPrime probably returns false. +// The probability of returning true for a randomly chosen non-prime is at most ¼ⁿ. +// +// ProbablyPrime is 100% accurate for inputs less than 2⁶⁴. +// See Menezes et al., Handbook of Applied Cryptography, 1997, pp. 145-149, +// and FIPS 186-4 Appendix F for further discussion of the error probabilities. +// +// ProbablyPrime is not suitable for judging primes that an adversary may +// have crafted to fool the test. +// +// As of Go 1.8, ProbablyPrime(0) is allowed and applies only a Baillie-PSW test. +// Before Go 1.8, ProbablyPrime applied only the Miller-Rabin tests, and ProbablyPrime(0) panicked. +func (x *Int) ProbablyPrime(n int) bool { + panic("ProbablyPrime: todo") +} diff --git a/runtime/internal/lib/runtime/compiler.go b/runtime/internal/lib/runtime/compiler.go index e79e34b2..fd77c421 100644 --- a/runtime/internal/lib/runtime/compiler.go +++ b/runtime/internal/lib/runtime/compiler.go @@ -9,5 +9,5 @@ package runtime // // - gc Also known as cmd/compile. // - gccgo The gccgo front end, part of the GCC compiler suite. -// - llgo Our proect +// - llgo Our project const Compiler = "llgo"