2014-09-13 16:47

# Golang游览运动平方根

• floating-point
• math

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条回答

• dongzhitao4839 7年前

`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
``````
点赞 评论 复制链接分享