Floating point Data types in Go lang
As we have discussed, In our previous article Data types in go lang, In Go lang Numbers can be categorized into two broad categories.
- Integral Numbers
- Floating point Numbers
We have already discussed integral numbers In this article we will discuss all floating point numbers.
Floating point Numbers
Floating point numbers are those which contains decimal places, for example (2.34 , -90.435 , 0.00007)
Rules for floating point numbers
- Floating point number can contain decimal places, For example (0.006, 2.45)
- Can be +ve and -ve.
- Floating point number can be written in two formats
- Fixed format, example (0.007)
- Exponent format, example (0.7 E -4)
- 0.7 E -4 is equivalent to 0.7 * 10 <sup>-4</sup>
- In 0.7 E -4, 0.7 (part before e) is called mantissa and -4 (part after E) is called exponent.
- Exponent format is used when a number is too large or too small to represent in decimal places.
- Floating point number can be of 32bits or 64bits in size in go lang.
- float32 represents 32 bits floating point numbers also called single precision. It allows maximum 6-7 digits of precision(number of digits after decimal point).
- float64 represents 64 bits floating point numbers also called double precision. It allows 14-17 digits precision(number of digits after decimal point)
- floating points numbers includes some constant values for undefined results as given below with examples.
- 1.0/0 results +INF
- -1.0/0 results -INF
- Along with the floating point, numbers go also allows a data type for storing complex numbers like 2 + 3i, where 3 is an imaginary number.
- For representing complex number complex64 (real float32 , img float32) and complex128 (real float64, img float64) are given.
- In complex64 real number is of type float32 and the imaginary part is also float32 so it occupies 64 bits in memory similarly in the complex128 real and imaginary part of size float64.
So if we finally conclude this article then we can say that floating data types includes four data types in go lang to work with floating numbers which are listed here.
- float32
- float64
- complex64
- complex128
Comments