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 the range of floating-point numbers can be obtained in the math package, math. MaxFloat32 represents the maximum value of float32, approximately 3.4e38,math.maxfloat64 is about 1.8e308. The minimum non-negative values of two types are 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. The float64 is usually preferred, so the accuracy of the float32 is low, the error spreads quickly in the cumulative calculation, and the smallest positive integer that the float32 can accurately express is not large, because the underlying interpretation of floating-point numbers and integers 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//(approximately)
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 the exponent of e with a precision of 3 digits after the decimal point, and then the print width of x is 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, the result of being removed by 0, and NaN (not a numeric value) used to denote the result of an invalid operation, 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. Warrior. 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 here returns two values, respectively, of the x, y coordinates of the mesh vertex.
If you want to explain the principle of image generation in depth, you need some geometry knowledge. But here we skip the principle of geometry, 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 Xs and y are linear functions of I and J, the origin is changed to the center point by the translation 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. Canvas Center Coordinates (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.
Go Language Core technology (Volume I) of 2.2-floating point