2015-12-09 07:57:17 +00:00
<!DOCTYPE HTML>
< html lang = "zh-tw" >
< head >
< meta charset = "UTF-8" >
< meta http-equiv = "X-UA-Compatible" content = "IE=edge" / >
2015-12-31 08:20:27 +00:00
< title > 浮點數 | Go语言圣经< / title >
2015-12-09 07:57:17 +00:00
< meta content = "text/html; charset=utf-8" http-equiv = "Content-Type" >
< meta name = "description" content = "" >
2016-01-02 08:04:45 +00:00
< meta name = "generator" content = "GitBook 2.6.6" >
2015-12-09 07:57:17 +00:00
< meta name = "HandheldFriendly" content = "true" / >
< meta name = "viewport" content = "width=device-width, initial-scale=1, user-scalable=no" >
< meta name = "apple-mobile-web-app-capable" content = "yes" >
< meta name = "apple-mobile-web-app-status-bar-style" content = "black" >
< link rel = "apple-touch-icon-precomposed" sizes = "152x152" href = "../gitbook/images/apple-touch-icon-precomposed-152.png" >
< link rel = "shortcut icon" href = "../gitbook/images/favicon.ico" type = "image/x-icon" >
< link rel = "stylesheet" href = "../gitbook/style.css" >
2015-12-28 08:08:26 +00:00
< link rel = "stylesheet" href = "../gitbook/plugins/gitbook-plugin-katex/katex.min.css" >
2015-12-09 07:57:17 +00:00
< link rel = "stylesheet" href = "../gitbook/plugins/gitbook-plugin-highlight/website.css" >
< link rel = "stylesheet" href = "../gitbook/plugins/gitbook-plugin-fontsettings/website.css" >
< link rel = "next" href = "../ch3/ch3-03.html" / >
< link rel = "prev" href = "../ch3/ch3-01.html" / >
< / head >
< body >
2016-01-02 08:04:45 +00:00
< div class = "book"
data-level="3.2"
data-chapter-title="浮點數"
data-filepath="ch3/ch3-02.md"
data-basepath=".."
data-revision="Sat Jan 02 2016 16:00:23 GMT+0800 (中国标准时间)"
data-innerlanguage="">
2015-12-09 07:57:17 +00:00
< div class = "book-summary" >
< nav role = "navigation" >
< ul class = "summary" >
< li class = "chapter " data-level = "0" data-path = "index.html" >
< a href = "../index.html" >
< i class = "fa fa-check" > < / i >
前言
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "0.1" data-path = "ch0/ch0-01.html" >
< a href = "../ch0/ch0-01.html" >
< i class = "fa fa-check" > < / i >
< b > 0.1.< / b >
Go語言起源
< / a >
< / li >
< li class = "chapter " data-level = "0.2" data-path = "ch0/ch0-02.html" >
< a href = "../ch0/ch0-02.html" >
< i class = "fa fa-check" > < / i >
< b > 0.2.< / b >
Go語言項目
< / a >
< / li >
< li class = "chapter " data-level = "0.3" data-path = "ch0/ch0-03.html" >
< a href = "../ch0/ch0-03.html" >
< i class = "fa fa-check" > < / i >
< b > 0.3.< / b >
本書的組織
< / a >
< / li >
< li class = "chapter " data-level = "0.4" data-path = "ch0/ch0-04.html" >
< a href = "../ch0/ch0-04.html" >
< i class = "fa fa-check" > < / i >
< b > 0.4.< / b >
更多的信息
< / a >
< / li >
< li class = "chapter " data-level = "0.5" data-path = "ch0/ch0-05.html" >
< a href = "../ch0/ch0-05.html" >
< i class = "fa fa-check" > < / i >
< b > 0.5.< / b >
2015-12-21 04:55:18 +00:00
致謝
2015-12-09 07:57:17 +00:00
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "1" data-path = "ch1/ch1.html" >
< a href = "../ch1/ch1.html" >
< i class = "fa fa-check" > < / i >
< b > 1.< / b >
入門
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "1.1" data-path = "ch1/ch1-01.html" >
< a href = "../ch1/ch1-01.html" >
< i class = "fa fa-check" > < / i >
< b > 1.1.< / b >
Hello, World
< / a >
< / li >
< li class = "chapter " data-level = "1.2" data-path = "ch1/ch1-02.html" >
< a href = "../ch1/ch1-02.html" >
< i class = "fa fa-check" > < / i >
< b > 1.2.< / b >
命令行參數
< / a >
< / li >
< li class = "chapter " data-level = "1.3" data-path = "ch1/ch1-03.html" >
< a href = "../ch1/ch1-03.html" >
< i class = "fa fa-check" > < / i >
< b > 1.3.< / b >
2015-12-21 04:55:18 +00:00
査找重複的行
2015-12-09 07:57:17 +00:00
< / a >
< / li >
< li class = "chapter " data-level = "1.4" data-path = "ch1/ch1-04.html" >
< a href = "../ch1/ch1-04.html" >
< i class = "fa fa-check" > < / i >
< b > 1.4.< / b >
2015-12-21 04:55:18 +00:00
GIF動畵
2015-12-09 07:57:17 +00:00
< / a >
< / li >
< li class = "chapter " data-level = "1.5" data-path = "ch1/ch1-05.html" >
< a href = "../ch1/ch1-05.html" >
< i class = "fa fa-check" > < / i >
< b > 1.5.< / b >
2015-12-28 08:08:26 +00:00
獲取URL
2015-12-09 07:57:17 +00:00
< / a >
< / li >
< li class = "chapter " data-level = "1.6" data-path = "ch1/ch1-06.html" >
< a href = "../ch1/ch1-06.html" >
< i class = "fa fa-check" > < / i >
< b > 1.6.< / b >
2015-12-28 08:08:26 +00:00
併發獲取多個URL
2015-12-09 07:57:17 +00:00
< / a >
< / li >
< li class = "chapter " data-level = "1.7" data-path = "ch1/ch1-07.html" >
< a href = "../ch1/ch1-07.html" >
< i class = "fa fa-check" > < / i >
< b > 1.7.< / b >
Web服務
< / a >
< / li >
< li class = "chapter " data-level = "1.8" data-path = "ch1/ch1-08.html" >
< a href = "../ch1/ch1-08.html" >
< i class = "fa fa-check" > < / i >
< b > 1.8.< / b >
本章要點
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "2" data-path = "ch2/ch2.html" >
< a href = "../ch2/ch2.html" >
< i class = "fa fa-check" > < / i >
< b > 2.< / b >
程序結構
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "2.1" data-path = "ch2/ch2-01.html" >
< a href = "../ch2/ch2-01.html" >
< i class = "fa fa-check" > < / i >
< b > 2.1.< / b >
命名
< / a >
< / li >
< li class = "chapter " data-level = "2.2" data-path = "ch2/ch2-02.html" >
< a href = "../ch2/ch2-02.html" >
< i class = "fa fa-check" > < / i >
< b > 2.2.< / b >
聲明
< / a >
< / li >
< li class = "chapter " data-level = "2.3" data-path = "ch2/ch2-03.html" >
< a href = "../ch2/ch2-03.html" >
< i class = "fa fa-check" > < / i >
< b > 2.3.< / b >
變量
< / a >
< / li >
< li class = "chapter " data-level = "2.4" data-path = "ch2/ch2-04.html" >
< a href = "../ch2/ch2-04.html" >
< i class = "fa fa-check" > < / i >
< b > 2.4.< / b >
賦值
< / a >
< / li >
< li class = "chapter " data-level = "2.5" data-path = "ch2/ch2-05.html" >
< a href = "../ch2/ch2-05.html" >
< i class = "fa fa-check" > < / i >
< b > 2.5.< / b >
類型
< / a >
< / li >
< li class = "chapter " data-level = "2.6" data-path = "ch2/ch2-06.html" >
< a href = "../ch2/ch2-06.html" >
< i class = "fa fa-check" > < / i >
< b > 2.6.< / b >
包和文件
< / a >
< / li >
< li class = "chapter " data-level = "2.7" data-path = "ch2/ch2-07.html" >
< a href = "../ch2/ch2-07.html" >
< i class = "fa fa-check" > < / i >
< b > 2.7.< / b >
作用域
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "3" data-path = "ch3/ch3.html" >
< a href = "../ch3/ch3.html" >
< i class = "fa fa-check" > < / i >
< b > 3.< / b >
基礎數據類型
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "3.1" data-path = "ch3/ch3-01.html" >
< a href = "../ch3/ch3-01.html" >
< i class = "fa fa-check" > < / i >
< b > 3.1.< / b >
整型
< / a >
< / li >
< li class = "chapter active" data-level = "3.2" data-path = "ch3/ch3-02.html" >
< a href = "../ch3/ch3-02.html" >
< i class = "fa fa-check" > < / i >
< b > 3.2.< / b >
浮點數
< / a >
< / li >
< li class = "chapter " data-level = "3.3" data-path = "ch3/ch3-03.html" >
< a href = "../ch3/ch3-03.html" >
< i class = "fa fa-check" > < / i >
< b > 3.3.< / b >
2015-12-21 04:55:18 +00:00
複數
2015-12-09 07:57:17 +00:00
< / a >
< / li >
< li class = "chapter " data-level = "3.4" data-path = "ch3/ch3-04.html" >
< a href = "../ch3/ch3-04.html" >
< i class = "fa fa-check" > < / i >
< b > 3.4.< / b >
2015-12-21 04:55:18 +00:00
布爾型
2015-12-09 07:57:17 +00:00
< / a >
< / li >
< li class = "chapter " data-level = "3.5" data-path = "ch3/ch3-05.html" >
< a href = "../ch3/ch3-05.html" >
< i class = "fa fa-check" > < / i >
< b > 3.5.< / b >
字符串
< / a >
< / li >
< li class = "chapter " data-level = "3.6" data-path = "ch3/ch3-06.html" >
< a href = "../ch3/ch3-06.html" >
< i class = "fa fa-check" > < / i >
< b > 3.6.< / b >
常量
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "4" data-path = "ch4/ch4.html" >
< a href = "../ch4/ch4.html" >
< i class = "fa fa-check" > < / i >
< b > 4.< / b >
2015-12-21 04:55:18 +00:00
複合數據類型
2015-12-09 07:57:17 +00:00
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "4.1" data-path = "ch4/ch4-01.html" >
< a href = "../ch4/ch4-01.html" >
< i class = "fa fa-check" > < / i >
< b > 4.1.< / b >
數組
< / a >
< / li >
< li class = "chapter " data-level = "4.2" data-path = "ch4/ch4-02.html" >
< a href = "../ch4/ch4-02.html" >
< i class = "fa fa-check" > < / i >
< b > 4.2.< / b >
2015-12-31 08:20:27 +00:00
Slice
2015-12-09 07:57:17 +00:00
< / a >
< / li >
< li class = "chapter " data-level = "4.3" data-path = "ch4/ch4-03.html" >
< a href = "../ch4/ch4-03.html" >
< i class = "fa fa-check" > < / i >
< b > 4.3.< / b >
2015-12-31 08:20:27 +00:00
Map
2015-12-09 07:57:17 +00:00
< / a >
< / li >
< li class = "chapter " data-level = "4.4" data-path = "ch4/ch4-04.html" >
< a href = "../ch4/ch4-04.html" >
< i class = "fa fa-check" > < / i >
< b > 4.4.< / b >
結構體
< / a >
< / li >
< li class = "chapter " data-level = "4.5" data-path = "ch4/ch4-05.html" >
< a href = "../ch4/ch4-05.html" >
< i class = "fa fa-check" > < / i >
< b > 4.5.< / b >
JSON
< / a >
< / li >
< li class = "chapter " data-level = "4.6" data-path = "ch4/ch4-06.html" >
< a href = "../ch4/ch4-06.html" >
< i class = "fa fa-check" > < / i >
< b > 4.6.< / b >
文本和HTML模闆
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "5" data-path = "ch5/ch5.html" >
< a href = "../ch5/ch5.html" >
< i class = "fa fa-check" > < / i >
< b > 5.< / b >
函數
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "5.1" data-path = "ch5/ch5-01.html" >
< a href = "../ch5/ch5-01.html" >
< i class = "fa fa-check" > < / i >
< b > 5.1.< / b >
函數聲明
< / a >
< / li >
< li class = "chapter " data-level = "5.2" data-path = "ch5/ch5-02.html" >
< a href = "../ch5/ch5-02.html" >
< i class = "fa fa-check" > < / i >
< b > 5.2.< / b >
遞歸
< / a >
< / li >
< li class = "chapter " data-level = "5.3" data-path = "ch5/ch5-03.html" >
< a href = "../ch5/ch5-03.html" >
< i class = "fa fa-check" > < / i >
< b > 5.3.< / b >
多返迴值
< / a >
< / li >
< li class = "chapter " data-level = "5.4" data-path = "ch5/ch5-04.html" >
< a href = "../ch5/ch5-04.html" >
< i class = "fa fa-check" > < / i >
< b > 5.4.< / b >
錯誤
< / a >
< / li >
< li class = "chapter " data-level = "5.5" data-path = "ch5/ch5-05.html" >
< a href = "../ch5/ch5-05.html" >
< i class = "fa fa-check" > < / i >
< b > 5.5.< / b >
函數值
< / a >
< / li >
< li class = "chapter " data-level = "5.6" data-path = "ch5/ch5-06.html" >
< a href = "../ch5/ch5-06.html" >
< i class = "fa fa-check" > < / i >
< b > 5.6.< / b >
匿名函數
< / a >
< / li >
< li class = "chapter " data-level = "5.7" data-path = "ch5/ch5-07.html" >
< a href = "../ch5/ch5-07.html" >
< i class = "fa fa-check" > < / i >
< b > 5.7.< / b >
可變參數
< / a >
< / li >
< li class = "chapter " data-level = "5.8" data-path = "ch5/ch5-08.html" >
< a href = "../ch5/ch5-08.html" >
< i class = "fa fa-check" > < / i >
< b > 5.8.< / b >
Deferred函數
< / a >
< / li >
< li class = "chapter " data-level = "5.9" data-path = "ch5/ch5-09.html" >
< a href = "../ch5/ch5-09.html" >
< i class = "fa fa-check" > < / i >
< b > 5.9.< / b >
Panic異常
< / a >
< / li >
< li class = "chapter " data-level = "5.10" data-path = "ch5/ch5-10.html" >
< a href = "../ch5/ch5-10.html" >
< i class = "fa fa-check" > < / i >
< b > 5.10.< / b >
2015-12-28 08:08:26 +00:00
Recover捕獲異常
2015-12-09 07:57:17 +00:00
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "6" data-path = "ch6/ch6.html" >
< a href = "../ch6/ch6.html" >
< i class = "fa fa-check" > < / i >
< b > 6.< / b >
方法
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "6.1" data-path = "ch6/ch6-01.html" >
< a href = "../ch6/ch6-01.html" >
< i class = "fa fa-check" > < / i >
< b > 6.1.< / b >
方法聲明
< / a >
< / li >
< li class = "chapter " data-level = "6.2" data-path = "ch6/ch6-02.html" >
< a href = "../ch6/ch6-02.html" >
< i class = "fa fa-check" > < / i >
< b > 6.2.< / b >
2015-12-21 04:55:18 +00:00
基於指針對象的方法
2015-12-09 07:57:17 +00:00
< / a >
< / li >
< li class = "chapter " data-level = "6.3" data-path = "ch6/ch6-03.html" >
< a href = "../ch6/ch6-03.html" >
< i class = "fa fa-check" > < / i >
< b > 6.3.< / b >
通過嵌入結構體來擴展類型
< / a >
< / li >
< li class = "chapter " data-level = "6.4" data-path = "ch6/ch6-04.html" >
< a href = "../ch6/ch6-04.html" >
< i class = "fa fa-check" > < / i >
< b > 6.4.< / b >
2015-12-21 04:55:18 +00:00
方法值和方法表達式
2015-12-09 07:57:17 +00:00
< / a >
< / li >
< li class = "chapter " data-level = "6.5" data-path = "ch6/ch6-05.html" >
< a href = "../ch6/ch6-05.html" >
< i class = "fa fa-check" > < / i >
< b > 6.5.< / b >
示例: Bit數組
< / a >
< / li >
< li class = "chapter " data-level = "6.6" data-path = "ch6/ch6-06.html" >
< a href = "../ch6/ch6-06.html" >
< i class = "fa fa-check" > < / i >
< b > 6.6.< / b >
封裝
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "7" data-path = "ch7/ch7.html" >
< a href = "../ch7/ch7.html" >
< i class = "fa fa-check" > < / i >
< b > 7.< / b >
接口
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "7.1" data-path = "ch7/ch7-01.html" >
< a href = "../ch7/ch7-01.html" >
< i class = "fa fa-check" > < / i >
< b > 7.1.< / b >
2015-12-21 04:55:18 +00:00
接口是合約
2015-12-09 07:57:17 +00:00
< / a >
< / li >
< li class = "chapter " data-level = "7.2" data-path = "ch7/ch7-02.html" >
< a href = "../ch7/ch7-02.html" >
< i class = "fa fa-check" > < / i >
< b > 7.2.< / b >
接口類型
< / a >
< / li >
< li class = "chapter " data-level = "7.3" data-path = "ch7/ch7-03.html" >
< a href = "../ch7/ch7-03.html" >
< i class = "fa fa-check" > < / i >
< b > 7.3.< / b >
實現接口的條件
< / a >
< / li >
< li class = "chapter " data-level = "7.4" data-path = "ch7/ch7-04.html" >
< a href = "../ch7/ch7-04.html" >
< i class = "fa fa-check" > < / i >
< b > 7.4.< / b >
flag.Value接口
< / a >
< / li >
< li class = "chapter " data-level = "7.5" data-path = "ch7/ch7-05.html" >
< a href = "../ch7/ch7-05.html" >
< i class = "fa fa-check" > < / i >
< b > 7.5.< / b >
接口值
< / a >
< / li >
< li class = "chapter " data-level = "7.6" data-path = "ch7/ch7-06.html" >
< a href = "../ch7/ch7-06.html" >
< i class = "fa fa-check" > < / i >
< b > 7.6.< / b >
sort.Interface接口
< / a >
< / li >
< li class = "chapter " data-level = "7.7" data-path = "ch7/ch7-07.html" >
< a href = "../ch7/ch7-07.html" >
< i class = "fa fa-check" > < / i >
< b > 7.7.< / b >
http.Handler接口
< / a >
< / li >
< li class = "chapter " data-level = "7.8" data-path = "ch7/ch7-08.html" >
< a href = "../ch7/ch7-08.html" >
< i class = "fa fa-check" > < / i >
< b > 7.8.< / b >
error接口
< / a >
< / li >
< li class = "chapter " data-level = "7.9" data-path = "ch7/ch7-09.html" >
< a href = "../ch7/ch7-09.html" >
< i class = "fa fa-check" > < / i >
< b > 7.9.< / b >
2015-12-21 04:55:18 +00:00
示例: 表達式求值
2015-12-09 07:57:17 +00:00
< / a >
< / li >
< li class = "chapter " data-level = "7.10" data-path = "ch7/ch7-10.html" >
< a href = "../ch7/ch7-10.html" >
< i class = "fa fa-check" > < / i >
< b > 7.10.< / b >
類型斷言
< / a >
< / li >
< li class = "chapter " data-level = "7.11" data-path = "ch7/ch7-11.html" >
< a href = "../ch7/ch7-11.html" >
< i class = "fa fa-check" > < / i >
< b > 7.11.< / b >
2015-12-21 04:55:18 +00:00
基於類型斷言識别錯誤類型
2015-12-09 07:57:17 +00:00
< / a >
< / li >
< li class = "chapter " data-level = "7.12" data-path = "ch7/ch7-12.html" >
< a href = "../ch7/ch7-12.html" >
< i class = "fa fa-check" > < / i >
< b > 7.12.< / b >
通過類型斷言査詢接口
< / a >
< / li >
< li class = "chapter " data-level = "7.13" data-path = "ch7/ch7-13.html" >
< a href = "../ch7/ch7-13.html" >
< i class = "fa fa-check" > < / i >
< b > 7.13.< / b >
類型分支
< / a >
< / li >
< li class = "chapter " data-level = "7.14" data-path = "ch7/ch7-14.html" >
< a href = "../ch7/ch7-14.html" >
< i class = "fa fa-check" > < / i >
< b > 7.14.< / b >
示例: 基於標記的XML解碼
< / a >
< / li >
< li class = "chapter " data-level = "7.15" data-path = "ch7/ch7-15.html" >
< a href = "../ch7/ch7-15.html" >
< i class = "fa fa-check" > < / i >
< b > 7.15.< / b >
補充幾點
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "8" data-path = "ch8/ch8.html" >
< a href = "../ch8/ch8.html" >
< i class = "fa fa-check" > < / i >
< b > 8.< / b >
Goroutines和Channels
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "8.1" data-path = "ch8/ch8-01.html" >
< a href = "../ch8/ch8-01.html" >
< i class = "fa fa-check" > < / i >
< b > 8.1.< / b >
Goroutines
< / a >
< / li >
< li class = "chapter " data-level = "8.2" data-path = "ch8/ch8-02.html" >
< a href = "../ch8/ch8-02.html" >
< i class = "fa fa-check" > < / i >
< b > 8.2.< / b >
2015-12-21 04:55:18 +00:00
示例: 併發的Clock服務
2015-12-09 07:57:17 +00:00
< / a >
< / li >
< li class = "chapter " data-level = "8.3" data-path = "ch8/ch8-03.html" >
< a href = "../ch8/ch8-03.html" >
< i class = "fa fa-check" > < / i >
< b > 8.3.< / b >
2015-12-21 04:55:18 +00:00
示例: 併發的Echo服務
2015-12-09 07:57:17 +00:00
< / a >
< / li >
< li class = "chapter " data-level = "8.4" data-path = "ch8/ch8-04.html" >
< a href = "../ch8/ch8-04.html" >
< i class = "fa fa-check" > < / i >
< b > 8.4.< / b >
Channels
< / a >
< / li >
< li class = "chapter " data-level = "8.5" data-path = "ch8/ch8-05.html" >
< a href = "../ch8/ch8-05.html" >
< i class = "fa fa-check" > < / i >
< b > 8.5.< / b >
併行的循環
< / a >
< / li >
< li class = "chapter " data-level = "8.6" data-path = "ch8/ch8-06.html" >
< a href = "../ch8/ch8-06.html" >
< i class = "fa fa-check" > < / i >
< b > 8.6.< / b >
2015-12-21 04:55:18 +00:00
示例: 併發的Web爬蟲
2015-12-09 07:57:17 +00:00
< / a >
< / li >
< li class = "chapter " data-level = "8.7" data-path = "ch8/ch8-07.html" >
< a href = "../ch8/ch8-07.html" >
< i class = "fa fa-check" > < / i >
< b > 8.7.< / b >
2015-12-21 04:55:18 +00:00
基於select的多路複用
2015-12-09 07:57:17 +00:00
< / a >
< / li >
< li class = "chapter " data-level = "8.8" data-path = "ch8/ch8-08.html" >
< a href = "../ch8/ch8-08.html" >
< i class = "fa fa-check" > < / i >
< b > 8.8.< / b >
2015-12-21 04:55:18 +00:00
示例: 併發的字典遍歷
2015-12-09 07:57:17 +00:00
< / a >
< / li >
< li class = "chapter " data-level = "8.9" data-path = "ch8/ch8-09.html" >
< a href = "../ch8/ch8-09.html" >
< i class = "fa fa-check" > < / i >
< b > 8.9.< / b >
2015-12-28 08:08:26 +00:00
併發的退出
2015-12-09 07:57:17 +00:00
< / a >
< / li >
< li class = "chapter " data-level = "8.10" data-path = "ch8/ch8-10.html" >
< a href = "../ch8/ch8-10.html" >
< i class = "fa fa-check" > < / i >
< b > 8.10.< / b >
示例: 聊天服務
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "9" data-path = "ch9/ch9.html" >
< a href = "../ch9/ch9.html" >
< i class = "fa fa-check" > < / i >
< b > 9.< / b >
2015-12-21 04:55:18 +00:00
基於共享變量的併發
2015-12-09 07:57:17 +00:00
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "9.1" data-path = "ch9/ch9-01.html" >
< a href = "../ch9/ch9-01.html" >
< i class = "fa fa-check" > < / i >
< b > 9.1.< / b >
競爭條件
< / a >
< / li >
< li class = "chapter " data-level = "9.2" data-path = "ch9/ch9-02.html" >
< a href = "../ch9/ch9-02.html" >
< i class = "fa fa-check" > < / i >
< b > 9.2.< / b >
sync.Mutex互斥鎖
< / a >
< / li >
< li class = "chapter " data-level = "9.3" data-path = "ch9/ch9-03.html" >
< a href = "../ch9/ch9-03.html" >
< i class = "fa fa-check" > < / i >
< b > 9.3.< / b >
sync.RWMutex讀寫鎖
< / a >
< / li >
< li class = "chapter " data-level = "9.4" data-path = "ch9/ch9-04.html" >
< a href = "../ch9/ch9-04.html" >
< i class = "fa fa-check" > < / i >
< b > 9.4.< / b >
2015-12-21 04:55:18 +00:00
內存同步
2015-12-09 07:57:17 +00:00
< / a >
< / li >
< li class = "chapter " data-level = "9.5" data-path = "ch9/ch9-05.html" >
< a href = "../ch9/ch9-05.html" >
< i class = "fa fa-check" > < / i >
< b > 9.5.< / b >
sync.Once初始化
< / a >
< / li >
< li class = "chapter " data-level = "9.6" data-path = "ch9/ch9-06.html" >
< a href = "../ch9/ch9-06.html" >
< i class = "fa fa-check" > < / i >
< b > 9.6.< / b >
競爭條件檢測
< / a >
< / li >
< li class = "chapter " data-level = "9.7" data-path = "ch9/ch9-07.html" >
< a href = "../ch9/ch9-07.html" >
< i class = "fa fa-check" > < / i >
< b > 9.7.< / b >
2015-12-21 04:55:18 +00:00
示例: 併發的非阻塞緩存
2015-12-09 07:57:17 +00:00
< / a >
< / li >
< li class = "chapter " data-level = "9.8" data-path = "ch9/ch9-08.html" >
< a href = "../ch9/ch9-08.html" >
< i class = "fa fa-check" > < / i >
< b > 9.8.< / b >
2015-12-21 04:55:18 +00:00
Goroutines和線程
2015-12-09 07:57:17 +00:00
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "10" data-path = "ch10/ch10.html" >
< a href = "../ch10/ch10.html" >
< i class = "fa fa-check" > < / i >
< b > 10.< / b >
包和工具
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "10.1" data-path = "ch10/ch10-01.html" >
< a href = "../ch10/ch10-01.html" >
< i class = "fa fa-check" > < / i >
< b > 10.1.< / b >
簡介
< / a >
< / li >
< li class = "chapter " data-level = "10.2" data-path = "ch10/ch10-02.html" >
< a href = "../ch10/ch10-02.html" >
< i class = "fa fa-check" > < / i >
< b > 10.2.< / b >
導入路徑
< / a >
< / li >
< li class = "chapter " data-level = "10.3" data-path = "ch10/ch10-03.html" >
< a href = "../ch10/ch10-03.html" >
< i class = "fa fa-check" > < / i >
< b > 10.3.< / b >
包聲明
< / a >
< / li >
< li class = "chapter " data-level = "10.4" data-path = "ch10/ch10-04.html" >
< a href = "../ch10/ch10-04.html" >
< i class = "fa fa-check" > < / i >
< b > 10.4.< / b >
導入聲明
< / a >
< / li >
< li class = "chapter " data-level = "10.5" data-path = "ch10/ch10-05.html" >
< a href = "../ch10/ch10-05.html" >
< i class = "fa fa-check" > < / i >
< b > 10.5.< / b >
匿名導入
< / a >
< / li >
< li class = "chapter " data-level = "10.6" data-path = "ch10/ch10-06.html" >
< a href = "../ch10/ch10-06.html" >
< i class = "fa fa-check" > < / i >
< b > 10.6.< / b >
包和命名
< / a >
< / li >
< li class = "chapter " data-level = "10.7" data-path = "ch10/ch10-07.html" >
< a href = "../ch10/ch10-07.html" >
< i class = "fa fa-check" > < / i >
< b > 10.7.< / b >
工具
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "11" data-path = "ch11/ch11.html" >
< a href = "../ch11/ch11.html" >
< i class = "fa fa-check" > < / i >
< b > 11.< / b >
測試
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "11.1" data-path = "ch11/ch11-01.html" >
< a href = "../ch11/ch11-01.html" >
< i class = "fa fa-check" > < / i >
< b > 11.1.< / b >
go test
< / a >
< / li >
< li class = "chapter " data-level = "11.2" data-path = "ch11/ch11-02.html" >
< a href = "../ch11/ch11-02.html" >
< i class = "fa fa-check" > < / i >
< b > 11.2.< / b >
測試函數
< / a >
< / li >
< li class = "chapter " data-level = "11.3" data-path = "ch11/ch11-03.html" >
< a href = "../ch11/ch11-03.html" >
< i class = "fa fa-check" > < / i >
< b > 11.3.< / b >
測試覆蓋率
< / a >
< / li >
< li class = "chapter " data-level = "11.4" data-path = "ch11/ch11-04.html" >
< a href = "../ch11/ch11-04.html" >
< i class = "fa fa-check" > < / i >
< b > 11.4.< / b >
基準測試
< / a >
< / li >
< li class = "chapter " data-level = "11.5" data-path = "ch11/ch11-05.html" >
< a href = "../ch11/ch11-05.html" >
< i class = "fa fa-check" > < / i >
< b > 11.5.< / b >
剖析
< / a >
< / li >
< li class = "chapter " data-level = "11.6" data-path = "ch11/ch11-06.html" >
< a href = "../ch11/ch11-06.html" >
< i class = "fa fa-check" > < / i >
< b > 11.6.< / b >
示例函數
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "12" data-path = "ch12/ch12.html" >
< a href = "../ch12/ch12.html" >
< i class = "fa fa-check" > < / i >
< b > 12.< / b >
反射
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "12.1" data-path = "ch12/ch12-01.html" >
< a href = "../ch12/ch12-01.html" >
< i class = "fa fa-check" > < / i >
< b > 12.1.< / b >
2015-12-21 04:55:18 +00:00
爲何需要反射?
2015-12-09 07:57:17 +00:00
< / a >
< / li >
< li class = "chapter " data-level = "12.2" data-path = "ch12/ch12-02.html" >
< a href = "../ch12/ch12-02.html" >
< i class = "fa fa-check" > < / i >
< b > 12.2.< / b >
reflect.Type和reflect.Value
< / a >
< / li >
< li class = "chapter " data-level = "12.3" data-path = "ch12/ch12-03.html" >
< a href = "../ch12/ch12-03.html" >
< i class = "fa fa-check" > < / i >
< b > 12.3.< / b >
Display遞歸打印
< / a >
< / li >
< li class = "chapter " data-level = "12.4" data-path = "ch12/ch12-04.html" >
< a href = "../ch12/ch12-04.html" >
< i class = "fa fa-check" > < / i >
< b > 12.4.< / b >
2015-12-21 04:55:18 +00:00
示例: 編碼S表達式
2015-12-09 07:57:17 +00:00
< / a >
< / li >
< li class = "chapter " data-level = "12.5" data-path = "ch12/ch12-05.html" >
< a href = "../ch12/ch12-05.html" >
< i class = "fa fa-check" > < / i >
< b > 12.5.< / b >
通過reflect.Value脩改值
< / a >
< / li >
< li class = "chapter " data-level = "12.6" data-path = "ch12/ch12-06.html" >
< a href = "../ch12/ch12-06.html" >
< i class = "fa fa-check" > < / i >
< b > 12.6.< / b >
2015-12-21 04:55:18 +00:00
示例: 解碼S表達式
2015-12-09 07:57:17 +00:00
< / a >
< / li >
< li class = "chapter " data-level = "12.7" data-path = "ch12/ch12-07.html" >
< a href = "../ch12/ch12-07.html" >
< i class = "fa fa-check" > < / i >
< b > 12.7.< / b >
2015-12-28 08:08:26 +00:00
獲取結構體字段標識
2015-12-09 07:57:17 +00:00
< / a >
< / li >
< li class = "chapter " data-level = "12.8" data-path = "ch12/ch12-08.html" >
< a href = "../ch12/ch12-08.html" >
< i class = "fa fa-check" > < / i >
< b > 12.8.< / b >
顯示一個類型的方法集
< / a >
< / li >
< li class = "chapter " data-level = "12.9" data-path = "ch12/ch12-09.html" >
< a href = "../ch12/ch12-09.html" >
< i class = "fa fa-check" > < / i >
< b > 12.9.< / b >
幾點忠告
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "13" data-path = "ch13/ch13.html" >
< a href = "../ch13/ch13.html" >
< i class = "fa fa-check" > < / i >
< b > 13.< / b >
底層編程
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "13.1" data-path = "ch13/ch13-01.html" >
< a href = "../ch13/ch13-01.html" >
< i class = "fa fa-check" > < / i >
< b > 13.1.< / b >
unsafe.Sizeof, Alignof 和 Offsetof
< / a >
< / li >
< li class = "chapter " data-level = "13.2" data-path = "ch13/ch13-02.html" >
< a href = "../ch13/ch13-02.html" >
< i class = "fa fa-check" > < / i >
< b > 13.2.< / b >
unsafe.Pointer
< / a >
< / li >
< li class = "chapter " data-level = "13.3" data-path = "ch13/ch13-03.html" >
< a href = "../ch13/ch13-03.html" >
< i class = "fa fa-check" > < / i >
< b > 13.3.< / b >
示例: 深度相等判斷
< / a >
< / li >
< li class = "chapter " data-level = "13.4" data-path = "ch13/ch13-04.html" >
< a href = "../ch13/ch13-04.html" >
< i class = "fa fa-check" > < / i >
< b > 13.4.< / b >
通過cgo調用C代碼
< / a >
< / li >
< li class = "chapter " data-level = "13.5" data-path = "ch13/ch13-05.html" >
< a href = "../ch13/ch13-05.html" >
< i class = "fa fa-check" > < / i >
< b > 13.5.< / b >
幾點忠告
< / a >
< / li >
< / ul >
< / li >
2015-12-24 06:47:06 +00:00
< li class = "chapter " data-level = "14" data-path = "CONTRIBUTORS.html" >
2015-12-09 07:57:17 +00:00
2015-12-24 06:47:06 +00:00
< a href = "../CONTRIBUTORS.html" >
2015-12-09 07:57:17 +00:00
< i class = "fa fa-check" > < / i >
2015-12-21 04:55:18 +00:00
< b > 14.< / b >
2015-12-09 07:57:17 +00:00
2015-12-24 06:47:06 +00:00
附録
2015-12-09 07:57:17 +00:00
< / a >
< / li >
< li class = "divider" > < / li >
< li >
< a href = "https://www.gitbook.com" target = "blank" class = "gitbook-link" >
本書使用 GitBook 釋出
< / a >
< / li >
< / ul >
< / nav >
< / div >
< div class = "book-body" >
< div class = "body-inner" >
< div class = "book-header" role = "navigation" >
<!-- Actions Left -->
<!-- Title -->
< h1 >
< i class = "fa fa-circle-o-notch fa-spin" > < / i >
2015-12-31 08:20:27 +00:00
< a href = "../" > Go语言圣经< / a >
2015-12-09 07:57:17 +00:00
< / h1 >
< / div >
< div class = "page-wrapper" tabindex = "-1" role = "main" >
< div class = "page-inner" >
< section class = "normal" id = "section-" >
< h2 id = "32-浮點數" > 3.2. 浮 點 數 < / h2 >
2015-12-28 08:08:26 +00:00
< p > Go語 言 提 供 了 兩 種 精 度 的 浮 點 數 , float32和 float64。 它 們 的 算 術 規 范 由 IEEE754浮 點 數 国 際 標 準 定 義 , 該 浮 點 數 規 范 被 所 有 現 代 的 CPU支 持 。 < / p >
< p > 這 些 浮 點 數 類 型 的 取 值 范 圍 可 以 從 很 微 小 到 很 鉅 大 。 浮 點 數 的 范 圍 極 限 值 可 以 在 math包 找 到 。 常 量 math.MaxFloat32表 示 float32能 表 示 的 最 大 數 值 , 大 約 是 3.4e38; 對 應 的 math.MaxFloat64常 量 大 約 是 1.8e308。 它 們 分 别 能 表 示 的 最 小 值 近 似 爲 1.4e-45和 4.9e-324。 < / p >
< p > 一 個 float32類 型 的 浮 點 數 可 以 提 供 大 約 6個 十 進 製 數 的 精 度 , 而 float64則 可 以 提 供 約 15個 十 進 製 數 的 精 度 ; 通 常 應 該 優 先 使 用 float64類 型 , 因 爲 float32類 型 的 纍 計 計 算 誤 差 很 容 易 擴 散 , 併 且 float32能 精 確 表 示 的 正 整 數 併 不 是 很 大 ( 譯 註 : 因 爲 float32的 有 效 bit位 隻 有 23個 , 其 它 的 bit位 用 於 指 數 和 符 號 ; 當 整 數 大 於 23bit能 表 達 的 范 圍 時 , float32的 表 示 將 出 現 誤 差 ) : < / p >
2015-12-21 04:55:18 +00:00
< pre > < code class = "lang-Go" > < span class = "hljs-keyword" > var< / span > f < span class = "hljs-typename" > float32< / span > = < span class = "hljs-number" > 16777216< / span > < span class = "hljs-comment" > // 1 < < 24< / span >
fmt.Println(f == f+< span class = "hljs-number" > 1< / span > ) < span class = "hljs-comment" > // " true" !< / span >
< / code > < / pre >
2015-12-28 08:08:26 +00:00
< p > 浮 點 數 的 字 面 值 可 以 直 接 寫 小 數 部 分 , 像 這 樣 : < / p >
2015-12-21 04:55:18 +00:00
< pre > < code class = "lang-Go" > < span class = "hljs-keyword" > const< / span > e = < span class = "hljs-number" > 2.71828< / span > < span class = "hljs-comment" > // (approximately)< / span >
< / code > < / pre >
2015-12-28 08:08:26 +00:00
< p > 小 數 點 前 面 或 後 面 的 數 字 都 可 能 被 省 略 ( 例 如 .707或 1.) 。 很 小 或 很 大 的 數 最 好 用 科 學 計 數 法 書 寫 , 通 過 e或 E來 指 定 指 數 部 分 : < / p >
< pre > < code class = "lang-Go" > < span class = "hljs-keyword" > const< / span > Avogadro = < span class = "hljs-number" > 6.02214129e23< / span > < span class = "hljs-comment" > // 阿 伏 伽 德 羅 常 數 < / span >
< span class = "hljs-keyword" > const< / span > Planck = < span class = "hljs-number" > 6.62606957e-34< / span > < span class = "hljs-comment" > // 普 朗 剋 常 數 < / span >
2015-12-21 04:55:18 +00:00
< / code > < / pre >
2015-12-28 08:08:26 +00:00
< p > 用 Printf函 數 的 %g參 數 打 印 浮 點 數 , 將 采 用 更 緊 湊 的 表 示 形 式 打 印 , 併 提 供 足 夠 的 精 度 , 但 是 對 應 表 格 的 數 據 , 使 用 %e( 帶 指 數 ) 或 %f的 形 式 打 印 可 能 更 合 適 。 所 有 的 這 三 個 打 印 形 式 都 可 以 指 定 打 印 的 寬 度 和 控 製 打 印 精 度 。 < / p >
2015-12-21 04:55:18 +00:00
< pre > < code class = "lang-Go" > < span class = "hljs-keyword" > for< / span > x := < span class = "hljs-number" > 0< / span > ; x < < span class = "hljs-number" > 8< / span > ; x++ {
fmt.Printf(< span class = "hljs-string" > " x = %d e^x = %8.3f\n" < / span > , x, math.Exp(< span class = "hljs-typename" > float64< / span > (x)))
}
< / code > < / pre >
2015-12-28 08:08:26 +00:00
< p > 上 面 代 碼 打 印 e的 冪 , 打 印 精 度 是 小 數 點 後 三 個 小 數 精 度 和 8個 字 符 寬 度 : < / p >
2015-12-21 04:55:18 +00:00
< pre > < code > x = 0 e^x = 1.000
x = 1 e^x = 2.718
x = 2 e^x = 7.389
x = 3 e^x = 20.086
x = 4 e^x = 54.598
x = 5 e^x = 148.413
x = 6 e^x = 403.429
x = 7 e^x = 1096.633
2015-12-28 08:08:26 +00:00
< / code > < / pre > < p > math包 中 除 了 提 供 大 量 常 用 的 數 學 函 數 外 , 還 提 供 了 IEEE754浮 點 數 標 準 中 定 義 的 特 殊 值 的 創 建 和 測 試 : 正 無 窮 大 和 負 無 窮 大 , 分 别 用 於 表 示 太 大 溢 出 的 數 字 和 除 零 的 結 果 ; 還 有 NaN非 數 , 一 般 用 於 表 示 無 效 的 除 法 操 作 結 果 0/0或 Sqrt(-1).< / p >
2015-12-21 04:55:18 +00:00
< pre > < code class = "lang-Go" > < span class = "hljs-keyword" > var< / span > z < span class = "hljs-typename" > float64< / span >
fmt.Println(z, -z, < span class = "hljs-number" > 1< / span > /z, -< span class = "hljs-number" > 1< / span > /z, z/z) < span class = "hljs-comment" > // " 0 -0 +Inf -Inf NaN" < / span >
< / code > < / pre >
2015-12-28 08:08:26 +00:00
< p > 函 數 math.IsNaN用 於 測 試 一 個 數 是 否 是 非 數 NaN, math.NaN則 返 迴 非 數 對 應 的 值 。 雖 然 可 以 用 math.NaN來 表 示 一 個 非 法 的 結 果 , 但 是 測 試 一 個 結 果 是 否 是 非 數 NaN則 是 充 滿 風 險 的 , 因 爲 NaN和 任 何 數 都 是 不 相 等 的 ( 譯 註 : 在 浮 點 數 中 , NaN、 正 無 窮 大 和 負 無 窮 大 都 不 是 唯 一 的 , 每 個 都 有 非 常 多 種 的 bit模 式 表 示 ) : < / p >
2015-12-21 04:55:18 +00:00
< pre > < code class = "lang-Go" > nan := math.NaN()
fmt.Println(nan == nan, nan < nan, nan > nan) < span class = "hljs-comment" > // " false false false" < / span >
< / code > < / pre >
2015-12-28 08:08:26 +00:00
< p > 如 果 一 個 函 數 返 迴 的 浮 點 數 結 果 可 能 失 敗 , 最 好 的 做 法 是 用 單 獨 的 標 誌 報 告 失 敗 , 像 這 樣 : < / p >
2015-12-21 04:55:18 +00:00
< pre > < code class = "lang-Go" > < span class = "hljs-keyword" > func< / span > compute() (value < span class = "hljs-typename" > float64< / span > , ok < span class = "hljs-typename" > bool< / span > ) {
< span class = "hljs-comment" > // ...< / span >
< span class = "hljs-keyword" > if< / span > failed {
< span class = "hljs-keyword" > return< / span > < span class = "hljs-number" > 0< / span > , < span class = "hljs-constant" > false< / span >
}
< span class = "hljs-keyword" > return< / span > result, < span class = "hljs-constant" > true< / span >
}
< / code > < / pre >
2015-12-28 08:08:26 +00:00
< p > 接 下 來 的 程 序 演 示 了 通 過 浮 點 計 算 生 成 的 圖 形 。 它 是 帶 有 兩 個 參 數 的 z = f(x, y)函 數 的 三 維 形 式 , 使 用 了 可 縮 放 矢 量 圖 形 ( SVG) 格 式 輸 出 , SVG是 一 個 用 於 矢 量 線 繪 製 的 XML標 準 。 圖 3.1顯 示 了 sin(r)/r函 數 的 輸 出 圖 形 , 其 中 r是 sqrt(x< em > x+y< / em > y)。 < / p >
2015-12-21 04:55:18 +00:00
< p > < img src = "../images/ch3-01.png" alt = "" > < / p >
< pre > < code class = "lang-Go" > gopl.io/ch3/surface
< span class = "hljs-comment" > // Surface computes an SVG rendering of a 3-D surface function.< / span >
< span class = "hljs-keyword" > package< / span > main
< span class = "hljs-keyword" > import< / span > (
< span class = "hljs-string" > " fmt" < / span >
< span class = "hljs-string" > " math" < / span >
)
< span class = "hljs-keyword" > const< / span > (
width, height = < span class = "hljs-number" > 600< / span > , < span class = "hljs-number" > 320< / span > < span class = "hljs-comment" > // canvas size in pixels< / span >
cells = < span class = "hljs-number" > 100< / span > < span class = "hljs-comment" > // number of grid cells< / span >
xyrange = < span class = "hljs-number" > 30.0< / span > < span class = "hljs-comment" > // axis ranges (-xyrange..+xyrange)< / span >
xyscale = width / < span class = "hljs-number" > 2< / span > / xyrange < span class = "hljs-comment" > // pixels per x or y unit< / span >
zscale = height * < span class = "hljs-number" > 0.4< / span > < span class = "hljs-comment" > // pixels per z unit< / span >
angle = math.Pi / < span class = "hljs-number" > 6< / span > < span class = "hljs-comment" > // angle of x, y axes (=30° )< / span >
)
< span class = "hljs-keyword" > var< / span > sin30, cos30 = math.Sin(angle), math.Cos(angle) < span class = "hljs-comment" > // sin(30° ), cos(30° )< / span >
< span class = "hljs-keyword" > func< / span > main() {
fmt.Printf(< span class = "hljs-string" > " < svg xmlns=' http://www.w3.org/2000/svg' " < / span > +
< span class = "hljs-string" > " style=' stroke: grey; fill: white; stroke-width: 0.7' " < / span > +
< span class = "hljs-string" > " width=' %d' height=' %d' > " < / span > , width, height)
< span class = "hljs-keyword" > for< / span > i := < span class = "hljs-number" > 0< / span > ; i < cells; i++ {
< span class = "hljs-keyword" > for< / span > j := < span class = "hljs-number" > 0< / span > ; j < cells; j++ {
ax, ay := corner(i+< span class = "hljs-number" > 1< / span > , j)
bx, by := corner(i, j)
cx, cy := corner(i, j+< span class = "hljs-number" > 1< / span > )
dx, dy := corner(i+< span class = "hljs-number" > 1< / span > , j+< span class = "hljs-number" > 1< / span > )
fmt.Printf(< span class = "hljs-string" > " < polygon points=' %g,%g %g,%g %g,%g %g,%g' /> \n" < / span > ,
ax, ay, bx, by, cx, cy, dx, dy)
}
}
fmt.Println(< span class = "hljs-string" > " < /svg> " < / span > )
}
< span class = "hljs-keyword" > func< / span > corner(i, j < span class = "hljs-typename" > int< / span > ) (< span class = "hljs-typename" > float64< / span > , < span class = "hljs-typename" > float64< / span > ) {
< span class = "hljs-comment" > // Find point (x,y) at corner of cell (i,j).< / span >
x := xyrange * (< span class = "hljs-typename" > float64< / span > (i)/cells - < span class = "hljs-number" > 0.5< / span > )
y := xyrange * (< span class = "hljs-typename" > float64< / span > (j)/cells - < span class = "hljs-number" > 0.5< / span > )
< span class = "hljs-comment" > // Compute surface height z.< / span >
z := f(x, y)
< span class = "hljs-comment" > // Project (x,y,z) isometrically onto 2-D SVG canvas (sx,sy).< / span >
sx := width/< span class = "hljs-number" > 2< / span > + (x-y)*cos30*xyscale
sy := height/< span class = "hljs-number" > 2< / span > + (x+y)*sin30*xyscale - z*zscale
< span class = "hljs-keyword" > return< / span > sx, sy
}
< span class = "hljs-keyword" > func< / span > f(x, y < span class = "hljs-typename" > float64< / span > ) < span class = "hljs-typename" > float64< / span > {
r := math.Hypot(x, y) < span class = "hljs-comment" > // distance from (0,0)< / span >
< span class = "hljs-keyword" > return< / span > math.Sin(r) / r
}
< / code > < / pre >
2015-12-28 08:08:26 +00:00
< p > 要 註 意 的 是 corner函 數 返 迴 了 兩 個 結 果 , 分 别 對 應 每 個 網 格 頂 點 的 坐 標 參 數 。 < / p >
< p > 要 解 釋 這 個 程 序 是 如 何 工 作 的 需 要 一 些 基 本 的 幾 何 學 知 識 , 但 是 我 們 可 以 跳 過 幾 何 學 原 理 , 因 爲 程 序 的 重 點 是 演 示 浮 點 數 運 算 。 程 序 的 本 質 是 三 個 不 同 的 坐 標 繫 中 映 射 關 繫 , 如 圖 3.2所 示 。 第 一 個 是 100x100的 二 維 網 格 , 對 應 整 數 整 數 坐 標 (i,j), 從 遠 處 的 (0, 0)位 置 開 始 。 我 們 從 遠 處 向 前 面 繪 製 , 因 此 遠 處 先 繪 製 的 多 邊 形 有 可 能 被 前 面 後 繪 製 的 多 邊 形 覆 蓋 。 < / p >
< p > 第 二 個 坐 標 繫 是 一 個 三 維 的 網 格 浮 點 坐 標 (x,y,z), 其 中 x和 y是 i和 j的 線 性 函 數 , 通 過 平 移 轉 換 位 網 格 單 元 的 中 心 , 然 後 用 xyrange繫 數 縮 放 。 高 度 z是 函 數 f(x,y)的 值 。 < / p >
< p > 第 三 個 坐 標 繫 是 一 個 二 維 的 畵 布 , 起 點 (0,0)在 左 上 角 。 畵 布 中 點 的 坐 標 用 (sx, sy)表 示 。 我 們 使 用 等 角 投 影 將 三 維 點 < / p >
2015-12-21 04:55:18 +00:00
< p > < img src = "../images/ch3-02.png" alt = "" > < / p >
2015-12-28 08:08:26 +00:00
< p > (x,y,z)投 影 到 二 維 的 畵 布 中 。 畵 布 中 從 遠 處 到 右 邊 的 點 對 應 較 大 的 x值 和 較 大 的 y值 。 併 且 畵 布 中 x和 y值 越 大 , 則 對 應 的 z值 越 小 。 x和 y的 垂 直 和 水 平 縮 放 繫 數 來 自 30度 角 的 正 絃 和 餘 絃 值 。 z的 縮 放 繫 數 0.4, 是 一 個 任 意 選 擇 的 參 數 。 < / p >
< p > 對 於 二 維 網 格 中 的 每 一 個 網 格 單 元 , main函 數 計 算 單 元 的 四 個 頂 點 在 畵 布 中 對 應 多 邊 形 ABCD的 頂 點 , 其 中 B對 應 (i,j)頂 點 位 置 , A、 C和 D是 其 它 相 鄰 的 頂 點 , 然 後 輸 出 SVG的 繪 製 指 令 。 < / p >
< p > < strong > 練 習 3.1: < / strong > 如 果 f函 數 返 迴 的 是 無 限 製 的 float64值 , 那 麽 SVG文 件 可 能 輸 出 無 效 的 < polygon > < / polygon > 多 邊 形 元 素 ( 雖 然 許 多 SVG渲 染 器 會 妥 善 處 理 這 類 問 題 ) 。 脩 改 程 序 跳 過 無 效 的 多 邊 形 。 < / p >
< p > < strong > 練 習 3.2: < / strong > 試 驗 math包 中 其 他 函 數 的 渲 染 圖 形 。 你 是 否 能 輸 出 一 個 egg box、 moguls或 a saddle圖 案 ?< / p >
< p > < strong > 練 習 3.3: < / strong > 根 據 高 度 給 每 個 多 邊 形 上 色 , 那 樣 峯 值 部 將 是 紅 色 (#ff0000), 谷 部 將 是 藍 色 (#0000ff)。 < / p >
< p > < strong > 練 習 3.4: < / strong > 參 考 1.7節 Lissajous例 子 的 函 數 , 構 造 一 個 web服 務 器 , 用 於 計 算 函 數 麴 面 然 後 返 迴 SVG數 據 給 客 戶 端 。 服 務 器 必 鬚 設 置 Content-Type頭 部 : < / p >
2015-12-21 04:55:18 +00:00
< pre > < code class = "lang-Go" > w.Header().Set(< span class = "hljs-string" > " Content-Type" < / span > , < span class = "hljs-string" > " image/svg+xml" < / span > )
< / code > < / pre >
2015-12-28 08:08:26 +00:00
< p > ( 這 一 步 在 Lissajous例 子 中 不 是 必 鬚 的 , 因 爲 服 務 器 使 用 標 準 的 PNG圖 像 格 式 , 可 以 根 據 前 面 的 512個 字 節 自 動 輸 出 對 應 的 頭 部 。 ) 允 許 客 戶 端 通 過 HTTP請 求 參 數 設 置 高 度 、 寬 度 和 顔 色 等 參 數 。 < / p >
2015-12-09 07:57:17 +00:00
< / section >
< / div >
< / div >
< / div >
< a href = "../ch3/ch3-01.html" class = "navigation navigation-prev " aria-label = "Previous page: 整型" > < i class = "fa fa-angle-left" > < / i > < / a >
2015-12-21 04:55:18 +00:00
< a href = "../ch3/ch3-03.html" class = "navigation navigation-next " aria-label = "Next page: 複數" > < i class = "fa fa-angle-right" > < / i > < / a >
2015-12-09 07:57:17 +00:00
< / div >
< / div >
< script src = "../gitbook/app.js" > < / script >
< script src = "../gitbook/plugins/gitbook-plugin-sharing/buttons.js" > < / script >
< script src = "../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js" > < / script >
< script >
require(["gitbook"], function(gitbook) {
2015-12-28 08:08:26 +00:00
var config = {"katex":{},"highlight":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
2015-12-09 07:57:17 +00:00
gitbook.start(config);
});
< / script >
< / body >
< / html >