_cmptest: mathbigdemo
This commit is contained in:
33
_cmptest/_goconstdemo/goconst.go
Normal file
33
_cmptest/_goconstdemo/goconst.go
Normal file
@@ -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)
|
||||
}
|
||||
25
_cmptest/mathbigdemo/big.go
Normal file
25
_cmptest/mathbigdemo/big.go
Normal file
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user