aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJosh Deprez <josh.deprez@gmail.com>2016-12-04 18:43:47 +1100
committerJaana Burcu Dogan <jbd@google.com>2017-04-15 03:29:35 +0000
commit53949f851297f0caf032ddf7ae5fb31ed6797eb6 (patch)
tree9449e8b61f74802e1bbde188898a617d30116fe1
parentc373014285671e75a2b50f45d204669442474b1b (diff)
downloadgolang-id-tour-53949f851297f0caf032ddf7ae5fb31ed6797eb6.tar.xz
tour: make Newton's method example more straightforward
Change-Id: Ie3473dfcfbd7272d40ff26110261f721f74d026a Reviewed-on: https://go-review.googlesource.com/33930 Reviewed-by: Jaana Burcu Dogan <jbd@google.com>
-rw-r--r--content/flowcontrol.article14
1 files changed, 9 insertions, 5 deletions
diff --git a/content/flowcontrol.article b/content/flowcontrol.article
index 4adfb65..9720b34 100644
--- a/content/flowcontrol.article
+++ b/content/flowcontrol.article
@@ -73,17 +73,21 @@ in `main` begins.)
* Exercise: Loops and Functions
-As a simple way to play with functions and loops, implement the square root function using Newton's method.
+As a way to play with functions and loops, implement the square root function using Newton's method.
-In this case, Newton's method is to approximate `Sqrt(x)` by picking a starting point _z_ and then repeating:
+Newton's method is to approximate `Sqrt(x)` by picking a starting point _z_ first, and repeating:
.image /content/img/newton.png
-To begin with, just repeat that calculation 10 times and see how close you get to the answer for various values (1, 2, 3, ...).
+Hint: Iterate and return the final value of _z_ as the answer:
-Next, change the loop condition to stop once the value has stopped changing (or only changes by a very small delta). See if that's more or fewer iterations. How close are you to the [[https://golang.org/pkg/math/#Sqrt][math.Sqrt]]?
+ z -= (z*z - x) / (2*z)
-Hint: to declare and initialize a floating point value, give it floating point syntax or use a conversion:
+To begin with, repeat the calculation 10 times and see how close you get to the answer for various values (1, 2, 3, ...).
+
+Next, change the loop condition to stop once the value has stopped changing (or only changes by a very small amount). See if that's more or fewer than 10 iterations. How close are you to the [[https://golang.org/pkg/math/#Sqrt][math.Sqrt]]?
+
+Hint: To declare and initialize a floating point value, give it floating point syntax or use a conversion:
z := float64(1)
z := 1.0