Skip to content

Commit

Permalink
Feat/sin (TheAlgorithms#476)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* Updated Documentation in README.md

* Update math/binary/abs.go

Co-authored-by: Taj <[email protected]>

* Updated Documentation in README.md

* fix: rename func and move binary test

* add package description and add function name to comment

* Updated Documentation in README.md

* rename Intercept method to YIntercept

* Updated Documentation in README.md

* fix strings

* Updated Documentation in README.md

* fix commit

* fix name cos tests and rename epsilon to epsilonCos

* feat: sin

* Updated Documentation in README.md

* fix test for sin

* fix epsilon

* rename package to test

* fix afte review

Co-authored-by: Rak Laptudirm <[email protected]>
Co-authored-by: github-action <${GITHUB_ACTOR}@users.noreply.github.com>
Co-authored-by: Taj <[email protected]>
  • Loading branch information
4 people authored Mar 31, 2022
1 parent bd8e6d5 commit 628336b
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,7 @@ Read our [Contribution Guidelines](CONTRIBUTING.md) before you contribute.
4. [`FindKthMin`](./math/kthnumber.go#L19): FindKthMin returns kth small element given an integer slice with nil `error` if found and returns -1 with `error` `search.ErrNotFound` if not found. NOTE: The `nums` slice gets mutated in the process.
5. [`IsPowOfTwoUseLog`](./math/checkisnumberpoweroftwo.go#L10): IsPowOfTwoUseLog This function checks if a number is a power of two using the logarithm. The limiting degree can be from 0 to 63. See alternatives in the binary package.
6. [`Phi`](./math/eulertotient.go#L5): Phi is the Euler totient function. This function computes the number of numbers less then n that are coprime with n.
7. [`Sin`](./math/sin.go#L9): Sin returns the sine of the radian argument x. [See more](https://en.wikipedia.org/wiki/Sine_and_cosine)

---
</details><details>
Expand Down
19 changes: 10 additions & 9 deletions math/cos_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package math
package math_test

import (
"math"
algmath "github.com/TheAlgorithms/Go/math"
stdmath "math"
"testing"
)

Expand All @@ -17,29 +18,29 @@ func TestCos(t *testing.T) {
{"cos(90)", 90, -0.447},
{"cos(180)", 180, -0.598},
{"cos(1)", 1, 0.540},
{"cos(1)", math.Pi, -1},
{"cos(1)", math.Pi / 2, 0},
{"cos(π)", stdmath.Pi, -1},
{"cos(π/2)", stdmath.Pi / 2, 0},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got := Cos(test.n)
if math.Abs(got-test.want) >= epsilon {
got := algmath.Cos(test.n)
if stdmath.Abs(got-test.want) >= epsilon {
t.Errorf("Cos() = %v, want %v", got, test.want)
t.Errorf("MATH Cos() = %v", math.Cos(test.n))
t.Errorf("MATH Cos() = %v", stdmath.Cos(test.n))
}
})
}
}

func BenchmarkCos(b *testing.B) {
for i := 0; i < b.N; i++ {
Cos(180)
algmath.Cos(180)
}
}

// BenchmarkMathCos is slower because the standard library `math.Cos` calculates a more accurate value.
func BenchmarkMathCos(b *testing.B) {
for i := 0; i < b.N; i++ {
math.Cos(180)
stdmath.Cos(180)
}
}
11 changes: 11 additions & 0 deletions math/sin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// author(s) [red_byte](https://github.com/i-redbyte)
// see sin_test.go

package math

import "math"

// Sin returns the sine of the radian argument x. [See more](https://en.wikipedia.org/wiki/Sine_and_cosine)
func Sin(x float64) float64 {
return Cos((math.Pi / 2) - x)
}
43 changes: 43 additions & 0 deletions math/sin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package math_test

import (
algmath "github.com/TheAlgorithms/Go/math"
stdmath "math"
"testing"
)

func TestSin(t *testing.T) {
tests := []struct {
name string
n float64
want float64
}{
{"sin(0)", 0, 0},
{"sin(3π/2)", (3 * stdmath.Pi) / 2, -1},
{"sin(π/2)", stdmath.Pi / 2, 1},
{"sin(π/6)", stdmath.Pi / 6, 0.5},
{"sin(90)", 90, 0.893996663600558},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got := algmath.Sin(test.n)
if stdmath.Abs(got-test.want) >= epsilon {
t.Errorf("Sin() = %v, want %v", got, test.want)
t.Errorf("MATH Sin() = %v", stdmath.Sin(test.n))
}
})
}
}

func BenchmarkSin(b *testing.B) {
for i := 0; i < b.N; i++ {
algmath.Sin(180)
}
}

// BenchmarkMathSin is slower because the standard library `math.Sin` calculates a more accurate value.
func BenchmarkMathSin(b *testing.B) {
for i := 0; i < b.N; i++ {
stdmath.Sin(180)
}
}

0 comments on commit 628336b

Please sign in to comment.