Go Dim()
Published Oct 3, 2023
Contribute to Docs
The Dim() function takes two floating-point numbers (x and y), calculates the difference (x - y), and then returns either this value, or 0 (if the difference is negative). The result is a single floating-point number that represents the maximum difference between x and y.
Syntax
difference := math.Dim(x, y)
The Dim() function returns a single value of type float64. If the difference (x-y) is greater than 0, it is returned; else, 0 is returned.
Note these special cases:
- Passing
NaNin this function will returnNaN. - Passing positive or negative
Infin this function will returnNaN.
Example
In this Go code example, we have a simple program that calculates and prints the positive difference between two numbers, 7.0 and 4.0, using the Dim() function from the math package.
package mainimport ("fmt""math")func main() {// Define two numbersa := 7.0b := 4.0// Calculate the positive difference between a and b using math.Dim()difference := math.Dim(a, b)fmt.Printf("The positive difference between %.1f and %.1f is %.1f\n", a, b, difference)}
The above code results in the following output:
The positive difference between 7 and 4 = 3
Codebyte Example
The example below is runnable and demonstrates the Dim() function.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Go on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn how to use Go (Golang), an open-source programming language supported by Google!
- Beginner Friendly.6 hours