码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • Go语言学习笔记-A Tour of Go 练习笔记-Loops and Functions


    Exercise: Loops and Functions

    题目:

    As a way to play with functions and loops, let's implement a square root function: given a number x, we want to find the number z for which z² is most nearly x.

    Computers typically compute the square root of x using a loop. Starting with some guess z, we can adjust z based on how close z² is to x, producing a better guess:

    z -= (z*z - x) / (2*z)

    Repeating this adjustment makes the guess better and better until we reach an answer that is as close to the actual square root as can be.

    Implement this in the func Sqrt provided. A decent starting guess for z is 1, no matter what the input. To begin with, repeat the calculation 10 times and print each z along the way. See how close you get to the answer for various values of x (1, 2, 3, ...) and how quickly the guess improves.

    Hint: To declare and initialize a floating point value, give it floating point syntax or use a conversion:

    z := 1.0
    z := float64(1)

    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. Try other initial guesses for z, like x, or x/2. How close are your function's results to the math.Sqrt in the standard library?

    (Note: If you are interested in the details of the algorithm, the z² − x above is how far away z² is from where it needs to be (x), and the division by 2z is the derivative of z², to scale how much we adjust z by how quickly z² is changing. This general approach is called Newton's method. It works well for many functions but especially well for square root.)

    练习程序:

    1. package main
    2. import (
    3. "fmt"
    4. "math"
    5. )
    6. func Sqrt(x float64) float64 {
    7. z := 1.0
    8. for math.Abs(z*z - x) > 0.00000001 {
    9. z -= (z*z - x) / (2 * z)
    10. }
    11. fmt.Println(z)
    12. return z*z
    13. }
    14. func main() {
    15. fmt.Println(Sqrt(2))
    16. fmt.Println("The sqrt is", math.Sqrt(2))
    17. }

    运行结果:

    1. 1.4142135623746899
    2. 2.0000000000045106
    3. The sqrt is 1.4142135623730951

    学习笔记:

    该程序使用著名的牛顿迭代发求解一个函数值的平方根,通过程序实现了当平方根的解在一定精度范围内,即可输出。

  • 相关阅读:
    SSM甜品店系统计算机毕业论文java毕业设计选题源代码
    对于koa中间件的理解
    搭建自己的SSR
    基于51单片机的人体红外探测防盗报警
    net-java-php-python-网上书店管理系统设计计算机毕业设计程序
    【100天精通Python】Day50:Python Web编程_Django框架从安装到使用
    verilog实现DDS波形发生器模块,可实现频率、相位可调,三种波形
    ElasticSearch深度分页并可以小幅度跳页的实现
    基于Delft3D模型水体流动、污染物对流扩散、质点运移、溢油漂移及地表水环境报告编制教程
    EA&UML日拱一卒 总目录
  • 原文地址:https://blog.csdn.net/sxmatch/article/details/127808790
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号