Golang教程练习:平方根

huangapple go评论110阅读模式
英文:

Golang Tour Excercise Square Root

问题

我正在尝试弄清楚为什么在这段代码中使用了1e-10。我正在完成练习,但在“接下来,将循环条件更改为在值停止改变(或仅通过非常小的增量改变)时停止。看看这样是更多还是更少的迭代次数。”这一部分卡住了。在找不到答案后,我搜索并找到了别人的解决方案。

在很多解决方案中,我看到1e-10作为for循环内的if语句的一部分。在Golang中,这是否意味着1e被提升到了-10次方?这在Golang中是自动的吗?所以基本上通常我会认为它应该写成1e^(-10)

谢谢任何提供帮助的人!

英文:

I'm trying to figure out why 1e-10 is used in this code. I was running through the exercises and got stuck on 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. After not figuring it out, I searched and found this solution by someone else.

In a lot of solutions, I saw 1e-10 as part of an if statement within the for loop. In Golang, does it mean that 1e is risen to the (-10) power? Is it like, automatic for Golang? So basically normally I'd assume it would be written as 1e^(-10)?

package main

import (
    "fmt"
    "math"
)

func Sqrt(x float64) float64 {
    z := float64(2.)
    s := float64(0)
    for i := 0; i < 10; i++ {
        z = z - (z*z - x) / (2 * z)
        if math.Abs(z-s) < 1e-10 {
            break
        }
        s = z
    }
    return z
}

func main() {
    fmt.Println(Sqrt(2))
    fmt.Println(math.Sqrt(2))
}

Thanks for any/all help one may provide!

答案1

得分: 4

1e-10 是一个浮点数字面量,其值为 1 乘以 10 的负 10 次方。

Go 编程语言规范

浮点数字面量

浮点数字面量是浮点常量的十进制表示。它包括整数部分、小数点、小数部分和指数部分。整数部分和小数部分由十进制数字组成;指数部分由 e 或 E 后跟一个可选的带符号十进制指数组成。整数部分或小数部分中的一个可以省略;小数点或指数中的一个可以省略。

float_lit = decimals "." [ decimals ] [ exponent ] |
            decimals exponent |
            "." decimals [ exponent ] .
decimals  = decimal_digit { decimal_digit } .
exponent  = ( "e" | "E" ) [ "+" | "-" ] decimals .

0.
72.40
072.40  // == 72.40
2.71828
1.e+0
6.67428e-11
1E6
.25
.12345E+5
英文:

1e-10 is a floating-point literal with the value 1 to the power minus 10.

> The Go Programming Language Specification
>
> Floating-point literals
>
> A floating-point literal is a decimal representation of a
> floating-point constant. It has an integer part, a decimal point, a
> fractional part, and an exponent part. The integer and fractional part
> comprise decimal digits; the exponent part is an e or E followed by an
> optionally signed decimal exponent. One of the integer part or the
> fractional part may be elided; one of the decimal point or the
> exponent may be elided.
>
> float_lit = decimals "." [ decimals ] [ exponent ] |
> decimals exponent |
> "." decimals [ exponent ] .
> decimals = decimal_digit { decimal_digit } .
> exponent = ( "e" | "E" ) [ "+" | "-" ] decimals .
>
> 0.
> 72.40
> 072.40 // == 72.40
> 2.71828
> 1.e+0
> 6.67428e-11
> 1E6
> .25
> .12345E+5

答案2

得分: 0

同样,我再次进行练习,试图回忆起之前解决这个任务的方法,以及以下这些话:

> 引用
将循环条件更改为在值停止改变后停止循环

这暗示着更改循环条件比添加额外的“if”条件更好……

很有可能只有了解牛顿法的工作原理才能改变循环本身的条件。

i<int(x)

提示:

https://gist.github.com/smirnoffs/6afc2dc31988c4935b139a6d0fea81a9

附言:

我认为这将使代码看起来更加优雅。

英文:

The same, I was on the exercise again, trying to recollect, how I solved the task in a previous time, and these words:

> Blockquote
change the loop condition to stop once the value has stopped changing

hinted that it is better to change the loop conditions than to add additional "if" condition...

it is very likely that knowing how Newton's method actually works will only allow changing the conditions of the cycle itself.

i<int(x)

the hint:

https://gist.github.com/smirnoffs/6afc2dc31988c4935b139a6d0fea81a9

p.s

IMO, it will allow the code look more elegant.

huangapple
  • 本文由 发表于 2014年9月14日 00:47:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/25825421.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定