From the course: Learning Go

Use the math package - Go Tutorial

From the course: Learning Go

Use the math package

- [Instructor] Go has many tools for mathematical operations, both operators and members of the math package, including functions and constants. And I'll show you how to use some of them here. I'll start in the main function for this file in my practice directory and I'm going to create three integer variables, and here's a new bit of syntax that we haven't covered. When you want to declare more than one variable of the same type, you can do it all in a single statement. I'll create variables named i1, i2 and i3, and I'll set them to literal values of 12, 45 and 68. And the Go compiler recognizes that those are integers and then implicitly sets the types of the variables to int. Now I'll add those values together. I'll create a variable called intSum and I'll add them with the plus operator. i1 plus i2 plus i3. Then I'll use this Println command and I'll say integer sum is, and then I'll pass in intSum. I'll make sure that I'm viewing my debug console. And I see the output that the sum is 125. So far, so good. Now I'll add some floating numbers together. I'll create a variable called f1, another one named f2, another one named f3, and I'll assign these with 23.5 65.1 and 76.3. I'll create a variable called floatSum and I'll add those values together. And then I'll copy and paste this line of code. Now I'll change the label to floatSum and the variable I'm outputting. And I get the result, but notice the odd precision problem after the decimal point. This is because, just like in Java and some other languages, floating values in Go are stored in binary format. So even when you're doing very simple mathematical operations, you can't always depend on getting the precision you hope for. There is a package that can fix some of this for you named math slash big. Like in Java, it has types that can manage more complex numbers, but it still doesn't guarantee precision. So here's a very simple way of managing the precision issue with, of course, some math. For this, I'm going to use the math package. So I'll go to my import block and add math in quotes. Then I'll come down to the end of my code. I'll reassign the value of floatSum like this. I'll say floatSum = math.round and then pass in floatSum. Notice I'm using the equals operator this time and not colon equals because this variable has already been initialized; I'm just resetting its value. And then I'll output that value with fmt.Println. I'll pass in a literal string of "The sum is now." Then I'll pass in floatSum and I'll run that code. And that gets rid of all the extra digits, but it also rounds to an actual integer and that's not really what I want. I want to round, let's say, to the nearest two decimals. So I'll change this calculation like this. Within math.round, I'll multiply floatSum by 100. And then after the rounding operation, I'll divide by a hundred. And now I get the expected answer of 164.9. This turns out to be the safest and easiest way in Go of rounding a fractional number and holding onto the digits after the decimal point. And I'll show you one more tool from the math package. It has some useful constants. I'll create a variable that I'll call circleRadius and I'll set its value to 15.5, a floating number. Then I'll create a variable called circumference and I'll set that with circleRadius times two, that's the diameter, and then I'll use the constant math.Pi. Then I'll output the circumference, and this time I'll use print F, because I need to do some formatting on that number. I'll pass in a string of circumference and then I'll use a verb that looks like this: %.2f. That means a floating number with two digits after the decimal. Then align feed, and then I'll pass in the circumference variable, and I'll run that code. And I get a circumference of 97.39. The math package has a bunch of other constants and functions that can help you with your mathematical operations. You can learn a lot more about what you can do with the math package in the developer documentation on the Go Lang website.

Contents