Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added Cubic root and methods

  • Loading branch information...
commit 2933eb13f1f6b982524a52576a5871341bc9848d 1 parent 34c0657
@bragi authored
Showing with 62 additions and 0 deletions.
  1. +28 −0 cubic_root.go
  2. +34 −0 methods.go
View
28 cubic_root.go
@@ -0,0 +1,28 @@
+package main
+
+import (
+ "fmt"
+ "math/cmplx"
+)
+
+func Cbrt(x complex128) complex128 {
+
+ Step := func(x, z complex128) complex128 {
+ return z - (z*z*z -x)/(3*z*z)
+ }
+
+ const LIMIT = 1e-5
+ var z complex128 = 1
+ var newZ complex128 = Step(x, z)
+
+ for cmplx.Abs(newZ - z) > LIMIT {
+ z = newZ
+ newZ = Step(x, z)
+ }
+ return newZ
+}
+
+func main() {
+ fmt.Println(Cbrt(2))
+ fmt.Println(cmplx.Pow(2, 0.5))
+}
View
34 methods.go
@@ -0,0 +1,34 @@
+package main
+
+import("fmt")
+
+
+type Game struct {
+ name string
+ finished bool
+}
+
+func (game *Game) state() (state string) {
+ if game.finished {
+ state = "finished"
+ } else {
+ state = "started"
+ }
+ return
+}
+
+func (game *Game) logStatus() {
+ fmt.Println(game.name)
+ fmt.Println(game.state())
+}
+
+func (game *Game) finish() {
+ game.finished = true
+}
+
+func main() {
+ game := &Game{name: "New game", finished: false}
+ game.logStatus()
+ game.finish()
+ game.logStatus()
+}
Please sign in to comment.
Something went wrong with that request. Please try again.