Skip to content

Commit 9bcf35b

Browse files
Turtel216kashifkhan0771coderabbitai[bot]
authored
Added Math Package (#71)
* 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>
1 parent 6c9534e commit 9bcf35b

File tree

4 files changed

+1127
-0
lines changed

4 files changed

+1127
-0
lines changed

EXAMPLES.md

Lines changed: 325 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ This document provides practical examples of how to use the library's features.
1313
9. [Structs](#9-structs)
1414
10. [Templates](#10-templates)
1515
11. [URLs](#11-urls)
16+
12. [Math](#12-math)
1617

1718
## 1. Boolean
1819

@@ -1303,3 +1304,327 @@ if err != nil {
13031304
// Output: bar
13041305
```
13051306
---
1307+
1308+
## 12. Math
1309+
1310+
## 1. `Abs`
1311+
1312+
### Calculate the absolute value of a number
1313+
```go
1314+
import (
1315+
"fmt"
1316+
1317+
utils "github.com/kashifkhan0771/utils/math"
1318+
)
1319+
1320+
func main() {
1321+
fmt.Println(utils.Abs(-5))
1322+
fmt.Println(utils.Abs(10))
1323+
fmt.Println(utils.Abs(0))
1324+
}
1325+
```
1326+
#### Output:
1327+
```
1328+
5
1329+
10
1330+
0
1331+
```
1332+
1333+
---
1334+
1335+
## 2. `Sign`
1336+
1337+
### Determine the sign of a number
1338+
```go
1339+
package main
1340+
1341+
import (
1342+
"fmt"
1343+
1344+
utils "github.com/kashifkhan0771/utils/math"
1345+
)
1346+
1347+
func main() {
1348+
fmt.Println(utils.Sign(15)) // Positive number
1349+
fmt.Println(utils.Sign(-10)) // Negative number
1350+
fmt.Println(utils.Sign(0)) // Zero
1351+
}
1352+
```
1353+
#### Output:
1354+
```
1355+
1
1356+
-1
1357+
0
1358+
```
1359+
1360+
---
1361+
1362+
## 3. `Min`
1363+
1364+
### Find the smaller of two numbers
1365+
```go
1366+
package main
1367+
1368+
import (
1369+
"fmt"
1370+
1371+
utils "github.com/kashifkhan0771/utils/math"
1372+
)
1373+
1374+
func main() {
1375+
fmt.Println(utils.Min(10, 20))
1376+
fmt.Println(utils.Min(25, 15))
1377+
fmt.Println(utils.Min(7, 7))
1378+
}
1379+
```
1380+
#### Output:
1381+
```
1382+
10
1383+
15
1384+
7
1385+
```
1386+
1387+
---
1388+
1389+
## 4. `Max`
1390+
1391+
### Find the larger of two numbers
1392+
```go
1393+
package main
1394+
1395+
import (
1396+
"fmt"
1397+
1398+
utils "github.com/kashifkhan0771/utils/math"
1399+
)
1400+
1401+
func main() {
1402+
fmt.Println(utils.Max(10, 20))
1403+
fmt.Println(utils.Max(25, 15))
1404+
fmt.Println(utils.Max(7, 7))
1405+
}
1406+
```
1407+
#### Output:
1408+
```
1409+
20
1410+
25
1411+
7
1412+
```
1413+
1414+
---
1415+
1416+
## 5. `Clamp`
1417+
1418+
### Clamp a value to stay within a range
1419+
```go
1420+
package main
1421+
1422+
import (
1423+
"fmt"
1424+
1425+
utils "github.com/kashifkhan0771/utils/math"
1426+
)
1427+
1428+
func main() {
1429+
fmt.Println(utils.Clamp(1, 10, 5)) // Value within range
1430+
fmt.Println(utils.Clamp(1, 10, 0)) // Value below range
1431+
fmt.Println(utils.Clamp(1, 10, 15)) // Value above range
1432+
}
1433+
```
1434+
#### Output:
1435+
```
1436+
5
1437+
1
1438+
10
1439+
```
1440+
1441+
---
1442+
1443+
## 6. `IntPow`
1444+
1445+
### Compute integer powers
1446+
```go
1447+
package main
1448+
1449+
import (
1450+
"fmt"
1451+
1452+
utils "github.com/kashifkhan0771/utils/math"
1453+
)
1454+
1455+
func main() {
1456+
fmt.Println(utils.IntPow(2, 3)) // 2^3
1457+
fmt.Println(utils.IntPow(5, 0)) // 5^0
1458+
fmt.Println(utils.IntPow(3, 2)) // 3^2
1459+
fmt.Println(utils.IntPow(2, -3)) // 3^(-3)
1460+
}
1461+
```
1462+
#### Output:
1463+
```
1464+
8
1465+
1
1466+
9
1467+
0.125
1468+
```
1469+
1470+
---
1471+
1472+
## 7. `IsEven`
1473+
1474+
### Check if a number is even
1475+
```go
1476+
package main
1477+
1478+
import (
1479+
"fmt"
1480+
1481+
utils "github.com/kashifkhan0771/utils/math"
1482+
)
1483+
1484+
func main() {
1485+
fmt.Println(utils.IsEven(8)) // Even number
1486+
fmt.Println(utils.IsEven(7)) // Odd number
1487+
fmt.Println(utils.IsEven(0)) // Zero
1488+
}
1489+
```
1490+
#### Output:
1491+
```
1492+
true
1493+
false
1494+
true
1495+
```
1496+
1497+
---
1498+
1499+
## 8. `IsOdd`
1500+
1501+
### Check if a number is odd
1502+
```go
1503+
package main
1504+
1505+
import (
1506+
"fmt"
1507+
1508+
utils "github.com/kashifkhan0771/utils/math"
1509+
)
1510+
1511+
func main() {
1512+
fmt.Println(utils.IsOdd(7)) // Odd number
1513+
fmt.Println(utils.IsOdd(8)) // Even number
1514+
fmt.Println(utils.IsOdd(0)) // Zero
1515+
}
1516+
```
1517+
#### Output:
1518+
```
1519+
true
1520+
false
1521+
false
1522+
```
1523+
1524+
---
1525+
1526+
## 9. `Swap`
1527+
1528+
### Swap two variables
1529+
```go
1530+
package main
1531+
1532+
import (
1533+
"fmt"
1534+
1535+
utils "github.com/kashifkhan0771/utils/math"
1536+
)
1537+
1538+
func main() {
1539+
x, y := 10, 20
1540+
utils.Swap(&x, &y)
1541+
fmt.Println(x, y)
1542+
}
1543+
```
1544+
#### Output:
1545+
```
1546+
20 10
1547+
```
1548+
1549+
---
1550+
1551+
## 10. `Factorial`
1552+
1553+
### Calculate the factorial of a number
1554+
```go
1555+
package main
1556+
1557+
import (
1558+
"fmt"
1559+
1560+
utils "github.com/kashifkhan0771/utils/math"
1561+
)
1562+
1563+
func main() {
1564+
result, err := utils.Factorial(5)
1565+
if err != nil {
1566+
fmt.Printf("%v\n", err)
1567+
}
1568+
1569+
fmt.Println(result)
1570+
}
1571+
```
1572+
#### Output:
1573+
```
1574+
120
1575+
```
1576+
1577+
---
1578+
1579+
## 11. `GCD`
1580+
1581+
### Find the greatest common divisor of two numbers
1582+
```go
1583+
package main
1584+
1585+
import (
1586+
"fmt"
1587+
1588+
utils "github.com/kashifkhan0771/utils/math"
1589+
)
1590+
1591+
func main() {
1592+
fmt.Println(utils.GCD(12, 18))
1593+
fmt.Println(utils.GCD(17, 19)) // Prime numbers
1594+
fmt.Println(utils.GCD(0, 5)) // Zero input
1595+
}
1596+
```
1597+
#### Output:
1598+
```
1599+
6
1600+
1
1601+
5
1602+
```
1603+
1604+
---
1605+
1606+
## 12. `LCM`
1607+
1608+
### Find the least common multiple of two numbers
1609+
```go
1610+
package main
1611+
1612+
import (
1613+
"fmt"
1614+
1615+
utils "github.com/kashifkhan0771/utils/math"
1616+
)
1617+
1618+
func main() {
1619+
fmt.Println(utils.LCM(4, 6))
1620+
fmt.Println(utils.LCM(7, 13)) // Prime numbers
1621+
fmt.Println(utils.LCM(0, 5)) // Zero input
1622+
}
1623+
```
1624+
#### Output:
1625+
```
1626+
12
1627+
91
1628+
0
1629+
```
1630+
---

0 commit comments

Comments
 (0)