Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PiNumber() implemented (with tests) issue #185 #214

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -1418,7 +1418,7 @@ func (d Decimal) xatan() Decimal {
func (d Decimal) satan() Decimal {
Morebits := NewFromFloat(6.123233995736765886130e-17) // pi/2 = PIO2 + Morebits
Tan3pio8 := NewFromFloat(2.41421356237309504880) // tan(3*pi/8)
pi := NewFromFloat(3.14159265358979323846264338327950288419716939937510582097494459)
pi := PiNumber()

if d.LessThanOrEqual(NewFromFloat(0.66)) {
return d.xatan()
Expand Down Expand Up @@ -1601,3 +1601,9 @@ func (d Decimal) Tan() Decimal {
}
return y
}

//PiNumber Returns Pi Number For Calculations
func PiNumber() Decimal {
pi, _ := NewFromString("3.14159265358979323846264338327950288419716939937510582097494459")
return pi
}
Comment on lines +1606 to +1609
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As the PiNumber function does not provide the capability to set the precision of the number Pi, maybe we should make it a global variable instead of a function?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe sending an argument to this function in order to determine the precision wouldn't be a bad idea?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be the best decision here, but it would require more work for sure.

67 changes: 67 additions & 0 deletions decimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,73 @@ func TestNewFromFloatRandom(t *testing.T) {
}
}

func TestPiNumber(t *testing.T) {
a := PiNumber()
b := New(-1234, 2)

if a.Cmp(b) != 1 {
t.Errorf("Error")
}
}

func TestPiNumberE(t *testing.T) {
a := PiNumber()
b := New(1274, 3)
c := New(1234, 4)

if a.Equal(b) {
t.Errorf("%q should equal %q", a, b)
}
if a.Equal(c) {
t.Errorf("%q should not equal %q", a, c)
}

// note, this block should be deprecated, here for backwards compatibility
if a.Equals(b) {
t.Errorf("%q should equal %q", a, b)
}

if !c.GreaterThan(b) {
t.Errorf("%q should be greater than %q", c, b)
}
if b.GreaterThan(c) {
t.Errorf("%q should not be greater than %q", b, c)
}
if a.GreaterThanOrEqual(b) {
t.Errorf("%q should be greater or equal %q", a, b)
}
if !c.GreaterThanOrEqual(b) {
t.Errorf("%q should be greater or equal %q", c, b)
}
if b.GreaterThanOrEqual(c) {
t.Errorf("%q should not be greater or equal %q", b, c)
}
if !b.LessThan(c) {
t.Errorf("%q should be less than %q", a, b)
}
if c.LessThan(b) {
t.Errorf("%q should not be less than %q", a, b)
}
if !a.LessThanOrEqual(b) {
t.Errorf("%q should be less than or equal %q", a, b)
}
if !b.LessThanOrEqual(c) {
t.Errorf("%q should be less than or equal %q", a, b)
}
if c.LessThanOrEqual(b) {
t.Errorf("%q should not be less than or equal %q", a, b)
}
}

func TestDpi_Cmp(t *testing.T) {
a := PiNumber()
b := PiNumber()

if a.Cmp(b) == -1 {
t.Errorf("Error")
}
}

func TestNewFromFloatQuick(t *testing.T) {
err := quick.Check(func(f float64) bool {
want, werr := NewFromString(strconv.FormatFloat(f, 'f', -1, 64))
Expand Down