This is a creation in Article, where the information may have evolved or changed.
Go provides two size floating-point numbers, float32 and float64. Their arithmetic specifications are defined by the IEEE754 International Standard, and modern CPUs have implemented this specification.
The range of floating-point numbers can be expressed from very small to very large, and this limit range can be obtained in the math package, math. MaxFloat32 represents the maximum value of float32, approximately 3.4e38,math.maxfloat64 is approximately 1.8e308, and the smallest non-negative value of two types is approximately 1.4e-45 and 4.9e-324.
Float32 can provide a precision of 6 digits after the decimal point, and as a comparison, the float64 can provide a precision of 15 digits after the decimal point. Usually the case should be preferred float64, so the accuracy of the float32 is low, the error spread quickly in the cumulative calculation, and the float32 can accurately express the smallest positive integer is not big, because the floating-point number and the whole number of the underlying interpretation is completely different, see IEEE754 detailed.
var f float32 = 16777216//1 << 24fmt. Println (f = = f+1) //"true"!
Floating-point literals can be expressed in decimal digits:
Const E = 2.71828//(non-exact value)
Digits before or after the decimal point can be omitted, for example:. 707, 1. , it is best to use scientific notation for a small or large number, plus E or e in front of the exponent:
Const AVOGADRO = 6.02214129e23 //Agamemnon constant const Planck = 6.62606957e-34//Planck constant
When the FMT prints floating-point numbers, the%g parameter is used to print in a more compact, more accurate form, but the%e (exponent) or%f (non-exponential) Form may be more appropriate when printing tabular data, and the above three parameters control the width and accuracy of the print:
For x: = 0; x < 8; X + + { FMT. Printf ("x =%d e^x =%8.3f\n", x, Math. EXP (float64 (x)))}
The above code prints with a precision of 3 digits after the decimal point, with a print width of 8 characters:
x = 0 ex = 1.000x = 1 ex = 2.718x = 2 ex = 7.389x = 3 ex = 20.086x = 4 ex = 54.598 x = 5 ex = 148.413x = 6 ex = 403.429x = 7 ex = 1096.633
The math package contains not only a large number of mathematical functions, but also the creation and viewing of special floating-point numbers under the IEEE754 specification: Positive infinity, which indicates that the number is too large to overflow, negative infinity, which represents the result of being removed by 0, and NaN (not a numeric value) that represents the result of an invalid operation, such as 0/0, math SQRT (-1).
var z float64fmt. Println (z,-Z, 1/z, -1/z, z/z)// "0-0 +inf-inf NaN"
function Math. isNaN tests if a value is Nan,math. Nan returns a Nan value. Although it is possible to use Nan as a sentinel value in numeric calculations, it is dangerous to test whether a computed result is equal to Nan, because any value compared to Nan results in false:
Nan: = Math. NaN () fmt. Println (Nan = = Nan, nan < Nan, Nan > Nan)//"False false"
If a function that returns a floating-point number may fail, it is better to have a separate report failure:
Func compute () (value float64, OK bool) { //... If failed { return 0, False } return result, true}
The following program demonstrates generating a graph by floating-point number calculations, using z = f (x, Y) for three-dimensional modeling, using SVG format for image output, and SVG as an XML standard for drawing vector lines. Shows the graph generated by the sin (r)/R function, R = sqrt (x*x + y*y):
Surface computes an SVG rendering of a-surface function.package mainimport ("FMT" "math") const (width, h eight = +, +//canvas size in pixels cells = +//number of grid cells xyr Ange = 30.0//axis ranges (-xyrange. +xyrange) Xyscale = WIDTH/2/xyrange//pixels per x or y unit zscale = height * 0.4//Pixel s per z unit angle = Math. PI/6//angle of x, y axes (=30°)) var sin30, cos30 = Math. Sin (angle), math. cos (angle)//sin (30°), cos (30°) func main () {FMT. Printf ("<svg xmlns= ' http://www.w3.org/2000/svg '" + "style= ' Stroke:grey; Fill:white; stroke-width:0.7 ' "+" width= '%d ' height= '%d ' > ", width, height) for i: = 0; I < cells; i++ {for J: = 0; J < cells; J + + {ax, ay: = Corner (i+1, J) bx, By: = Corner (i, J) CX, cy: = Corner (i, j+1) dx, dy: = Corner (i+1, j+1) FMt. Printf ("<polygon points= '%g,%g%g,%g%g,%g%g,%g '/>\n ', ax, Ay, BX, by, CX, CY, DX, DY)}} Fmt. Println ("</svg>")}func Corner (i, J int) (float64, float64) {//Find point (x, y) at corner of Cell (I,J). x: = Xyrange * (float64 (i)/cells-0.5) y: = Xyrange * (float64 (j)/cells-0.5)//Compute surface height Z. Z: = f (x, Y)//Project (z/y) isometrically onto 2-d SVG canvas (sx,sy). SX: = WIDTH/2 + (x-y) *cos30*xyscale sy: = HEIGHT/2 + (x+y) *sin30*xyscale-z*zscale return SX, Sy}func f (x, y Float6 4) float64 {r: = Math. Hypot (x, y)//Distance from (0,0) return math. Sin (R)/R}
The corner function returns two values, the x, y coordinates of the mesh vertex, respectively.
If we want to explain the principle of image generation in depth, we also need some geometry knowledge. But this is going to skip these geometrical principles, after all, this program is mainly to demonstrate the operation of floating-point numbers. The program is essentially a mapping between three coordinate systems, as shown in the first two-dimensional mesh of 100*100, each with coordinates (I,J), extending from the coordinate system origin (0,0). Drawing is drawn from a distance, so a polygon drawn at a distance may be overwritten by a polygon that is drawn later.
The second coordinate system is composed of three-dimensional meshes, coordinates (x, y, z), where × and y are linear functions of I and J, the origin is changed to the center point by the coordinate transformation and then scaled by Xyrange. Height Z is the value of f (x, y).
The third coordinate system is a two-dimensional canvas, starting point (0,0) in the upper-left corner. Coordinates of any point on the canvas (sx,sy), we use conformal projections to project three-dimensional points (x, y, z) into a two-dimensional canvas. The farther away the point on the canvas is from the right, the greater the X and Y values, the smaller the z-value. The vertical scaling factor for x and y is the sin value at 30 degrees, and the horizontal scale system is the COS value at the 30 degree angle. The zoom factor of Z 0.4 is an arbitrary value.
For each grid cell in a two-dimensional mesh, the main function calculates the vertex of the polygon ABCD that corresponds to the element on the canvas, B corresponds to the vertex (i,j), A, C, D is the adjacency point of B, and then outputs the drawing instruction of SVG.
Article ownership: Golang Contact: Sun Fei, cto@188.com!