forked from TheAlgorithms/Go
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: bitwise min * fix: bitwise min, change for vararg * fix: change min tests * fix: benchmark for bitwise * fix: rename tests * fix: add value param * fix: change condition * feat: added description to some functions * Updated Documentation in README.md * fix: change descriptions * Updated Documentation in README.md * Updated Documentation in README.md * Updated Documentation in README.md * feat: abs algo * Updated Documentation in README.md * fix: add comment * Updated Documentation in README.md * fix: new comment * Updated Documentation in README.md * fix: rename func and remove package * Updated Documentation in README.md * Update math/abs.go Co-authored-by: Taj <tjgurwara99@users.noreply.github.com> * Updated Documentation in README.md * Update math/binary/abs.go Co-authored-by: Taj <tjgurwara99@users.noreply.github.com> * Updated Documentation in README.md * fix: rename func and move binary test Co-authored-by: Rak Laptudirm <raklaptudirm@gmail.com> Co-authored-by: github-action <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Taj <tjgurwara99@users.noreply.github.com>
- Loading branch information
1 parent
7fcbe26
commit 75b615c
Showing
5 changed files
with
139 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// abs.go | ||
// description: Absolute value | ||
// details: | ||
// In mathematics, the absolute value or modulus of a real number x, denoted |x|, is the non-negative value of x without regard to its sign. [Absolute value](https://en.wikipedia.org/wiki/Average#Arithmetic_mean) | ||
// author(s) [red_byte](https://github.com/i-redbyte) | ||
// see abs_test.go | ||
|
||
package math | ||
|
||
// Abs returns absolute value | ||
func Abs(n int) int { | ||
if n < 0 { | ||
return -n | ||
} | ||
return n | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package math | ||
|
||
import ( | ||
"github.com/TheAlgorithms/Go/math/binary" | ||
"math" | ||
"testing" | ||
) | ||
|
||
func TestAbs(t *testing.T) { | ||
tests := getTests() | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
if got := Abs(test.n); got != test.want { | ||
t.Errorf("Abs() = %v, want %v", got, test.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func getTests() []struct { | ||
name string | ||
n int | ||
want int | ||
} { | ||
tests := []struct { | ||
name string | ||
n int | ||
want int | ||
}{ | ||
{"-1 = |1| ", -1, 1}, | ||
{"-255 = |255| ", -255, 255}, | ||
{"0 = |0| ", 0, 0}, | ||
{"5 = |5| ", 5, 5}, | ||
{"-5 = |5| ", -5, 5}, | ||
{"-98368972 = |98368972| ", -98368972, 98368972}, | ||
} | ||
return tests | ||
} | ||
|
||
func BenchmarkSimpleAbs(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
Abs(-1024) | ||
} | ||
} | ||
func BenchmarkBinaryAbs32(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
binary.Abs(32, -1024) | ||
} | ||
} | ||
|
||
func BenchmarkBinaryAbs64(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
binary.Abs(64, -1024) | ||
} | ||
} | ||
|
||
func BenchmarkStdLibAbs(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
math.Abs(-1024) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package binary | ||
|
||
// Abs returns absolute value using binary operation | ||
// Principle of operation: | ||
// 1) Get the mask by right shift by the base | ||
// 2) Base is the size of an integer variable in bits, for example, for int32 it will be 32, for int64 it will be 64 | ||
// 3) For negative numbers, above step sets mask as 1 1 1 1 1 1 1 1 and 0 0 0 0 0 0 0 0 for positive numbers. | ||
// 4) Add the mask to the given number. | ||
// 5) XOR of mask + n and mask gives the absolute value. | ||
func Abs(base, n int) int { | ||
m := n >> (base - 1) | ||
return (n + m) ^ m | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package binary | ||
|
||
import "testing" | ||
|
||
func TestAbs(t *testing.T) { | ||
tests := getAbsTests() | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
if got := Abs(test.base, test.n); got != test.want { | ||
t.Errorf("Abs() = %v, want %v", got, test.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func getAbsTests() []struct { | ||
name string | ||
base int | ||
n int | ||
want int | ||
} { | ||
tests := []struct { | ||
name string | ||
base int | ||
n int | ||
want int | ||
}{ | ||
{"-1 = |1| ", 32, -1, 1}, | ||
{"-255 = |255| ", 32, -255, 255}, | ||
{"0 = |0| ", 64, 0, 0}, | ||
{"5 = |5| ", 16, 5, 5}, | ||
{"-5 = |5| ", 32, -5, 5}, | ||
{"-98368972 = |98368972| ", 64, -98368972, 98368972}, | ||
{"-110298368972 = |110298368972| ", 64, -98368972, 98368972}, | ||
} | ||
return tests | ||
} |