Skip to content

Commit

Permalink
Added Math Package (#71)
Browse files Browse the repository at this point in the history
* added math package

* fixed Factorial issue

* fixed Abs issue

* fixed IntBow issue

* Update math/math.go

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Signed-off-by: Kashif Khan <[email protected]>

* updated imports in example

* fixed IntPow 1/0 issue

* fixed GCD issue

---------

Signed-off-by: Kashif Khan <[email protected]>
Co-authored-by: Kashif Khan <[email protected]>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Dec 10, 2024
1 parent 6c9534e commit 9bcf35b
Show file tree
Hide file tree
Showing 4 changed files with 1,127 additions and 0 deletions.
325 changes: 325 additions & 0 deletions EXAMPLES.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ This document provides practical examples of how to use the library's features.
9. [Structs](#9-structs)
10. [Templates](#10-templates)
11. [URLs](#11-urls)
12. [Math](#12-math)

## 1. Boolean

Expand Down Expand Up @@ -1303,3 +1304,327 @@ if err != nil {
// Output: bar
```
---

## 12. Math

## 1. `Abs`

### Calculate the absolute value of a number
```go
import (
"fmt"

utils "github.com/kashifkhan0771/utils/math"
)

func main() {
fmt.Println(utils.Abs(-5))
fmt.Println(utils.Abs(10))
fmt.Println(utils.Abs(0))
}
```
#### Output:
```
5
10
0
```

---

## 2. `Sign`

### Determine the sign of a number
```go
package main

import (
"fmt"

utils "github.com/kashifkhan0771/utils/math"
)

func main() {
fmt.Println(utils.Sign(15)) // Positive number
fmt.Println(utils.Sign(-10)) // Negative number
fmt.Println(utils.Sign(0)) // Zero
}
```
#### Output:
```
1
-1
0
```

---

## 3. `Min`

### Find the smaller of two numbers
```go
package main

import (
"fmt"

utils "github.com/kashifkhan0771/utils/math"
)

func main() {
fmt.Println(utils.Min(10, 20))
fmt.Println(utils.Min(25, 15))
fmt.Println(utils.Min(7, 7))
}
```
#### Output:
```
10
15
7
```

---

## 4. `Max`

### Find the larger of two numbers
```go
package main

import (
"fmt"

utils "github.com/kashifkhan0771/utils/math"
)

func main() {
fmt.Println(utils.Max(10, 20))
fmt.Println(utils.Max(25, 15))
fmt.Println(utils.Max(7, 7))
}
```
#### Output:
```
20
25
7
```

---

## 5. `Clamp`

### Clamp a value to stay within a range
```go
package main

import (
"fmt"

utils "github.com/kashifkhan0771/utils/math"
)

func main() {
fmt.Println(utils.Clamp(1, 10, 5)) // Value within range
fmt.Println(utils.Clamp(1, 10, 0)) // Value below range
fmt.Println(utils.Clamp(1, 10, 15)) // Value above range
}
```
#### Output:
```
5
1
10
```

---

## 6. `IntPow`

### Compute integer powers
```go
package main

import (
"fmt"

utils "github.com/kashifkhan0771/utils/math"
)

func main() {
fmt.Println(utils.IntPow(2, 3)) // 2^3
fmt.Println(utils.IntPow(5, 0)) // 5^0
fmt.Println(utils.IntPow(3, 2)) // 3^2
fmt.Println(utils.IntPow(2, -3)) // 3^(-3)
}
```
#### Output:
```
8
1
9
0.125
```

---

## 7. `IsEven`

### Check if a number is even
```go
package main

import (
"fmt"

utils "github.com/kashifkhan0771/utils/math"
)

func main() {
fmt.Println(utils.IsEven(8)) // Even number
fmt.Println(utils.IsEven(7)) // Odd number
fmt.Println(utils.IsEven(0)) // Zero
}
```
#### Output:
```
true
false
true
```

---

## 8. `IsOdd`

### Check if a number is odd
```go
package main

import (
"fmt"

utils "github.com/kashifkhan0771/utils/math"
)

func main() {
fmt.Println(utils.IsOdd(7)) // Odd number
fmt.Println(utils.IsOdd(8)) // Even number
fmt.Println(utils.IsOdd(0)) // Zero
}
```
#### Output:
```
true
false
false
```

---

## 9. `Swap`

### Swap two variables
```go
package main

import (
"fmt"

utils "github.com/kashifkhan0771/utils/math"
)

func main() {
x, y := 10, 20
utils.Swap(&x, &y)
fmt.Println(x, y)
}
```
#### Output:
```
20 10
```

---

## 10. `Factorial`

### Calculate the factorial of a number
```go
package main

import (
"fmt"

utils "github.com/kashifkhan0771/utils/math"
)

func main() {
result, err := utils.Factorial(5)
if err != nil {
fmt.Printf("%v\n", err)
}

fmt.Println(result)
}
```
#### Output:
```
120
```

---

## 11. `GCD`

### Find the greatest common divisor of two numbers
```go
package main

import (
"fmt"

utils "github.com/kashifkhan0771/utils/math"
)

func main() {
fmt.Println(utils.GCD(12, 18))
fmt.Println(utils.GCD(17, 19)) // Prime numbers
fmt.Println(utils.GCD(0, 5)) // Zero input
}
```
#### Output:
```
6
1
5
```

---

## 12. `LCM`

### Find the least common multiple of two numbers
```go
package main

import (
"fmt"

utils "github.com/kashifkhan0771/utils/math"
)

func main() {
fmt.Println(utils.LCM(4, 6))
fmt.Println(utils.LCM(7, 13)) // Prime numbers
fmt.Println(utils.LCM(0, 5)) // Zero input
}
```
#### Output:
```
12
91
0
```
---
Loading

0 comments on commit 9bcf35b

Please sign in to comment.