Skip to content

Commit

Permalink
Feat/cos (TheAlgorithms#475)
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

* added Cosine function

* Updated Documentation in README.md

* fix after review

* Updated Documentation in README.md

* fixes

* fix: doc comment

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 20, 2022
1 parent 3c64010 commit 096521a
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 4 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -548,10 +548,11 @@ Read our [Contribution Guidelines](CONTRIBUTING.md) before you contribute.
##### Functions:

1. [`Abs`](./math/abs.go#L11): Abs returns absolute value
2. [`FindKthMax`](./math/kthnumber.go#L11): FindKthMax returns the kth large 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.
3. [`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.
4. [`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.
5. [`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.
2. [`Cos`](./math/cos.go#L10): Cos returns the cosine of the radian argument x. [See more](https://en.wikipedia.org/wiki/Sine_and_cosine) [Based on the idea of Bhaskara approximation of cos(x)](https://math.stackexchange.com/questions/3886552/bhaskara-approximation-of-cosx)
3. [`FindKthMax`](./math/kthnumber.go#L11): FindKthMax returns the kth large 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.
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.

---
</details><details>
Expand Down
17 changes: 17 additions & 0 deletions math/cos.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// author(s) [red_byte](https://github.com/i-redbyte)
// see cos_test.go

package math

import "math"

// Cos returns the cosine of the radian argument x. [See more](https://en.wikipedia.org/wiki/Sine_and_cosine)
// [Based on the idea of Bhaskara approximation of cos(x)](https://math.stackexchange.com/questions/3886552/bhaskara-approximation-of-cosx)
func Cos(x float64) float64 {
tp := 1.0 / (2.0 * math.Pi)
x *= tp
x -= 0.25 + math.Floor(x+0.25)
x *= 16.0 * (math.Abs(x) - 0.5)
x += 0.225 * x * (math.Abs(x) - 1.0) //Extra precision
return x
}
45 changes: 45 additions & 0 deletions math/cos_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package math

import (
"math"
"testing"
)

const epsilon = 0.001

func TestCos(t *testing.T) {
tests := []struct {
name string
n float64
want float64
}{
{"cos(0)", 0, 1},
{"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},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got := Cos(test.n)
if math.Abs(got-test.want) >= epsilon {
t.Errorf("Cos() = %v, want %v", got, test.want)
t.Errorf("MATH Cos() = %v", math.Cos(test.n))
}
})
}
}

func BenchmarkCos(b *testing.B) {
for i := 0; i < b.N; i++ {
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)
}
}

0 comments on commit 096521a

Please sign in to comment.