Skip to content

Commit

Permalink
Add exp/log/ln operations
Browse files Browse the repository at this point in the history
- As requested in #18
  • Loading branch information
marcopaganini committed Dec 13, 2024
1 parent 592cb31 commit d789957
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
5 changes: 4 additions & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ func TestRPN(t *testing.T) {
{input: "rad 3 sqr atan", want: big().Quo(ctx.Pi(big()), bigUint(3))}, // Pi / 3
{input: "deg 1 180 * PI / atan", want: big().Quo(ctx.Pi(big()), bigUint(4))}, // Pi / 4

// Log functions
{input: "c E ln", want: bigUint(1)},
{input: "c 1000 log", want: bigUint(3)},

// Bitwise operations and base input modes.
{input: "0x00ff 0xff00 or", want: bigUint(0xffff)},
{input: "0x0ff0 and", want: bigUint(0x0ff0)},
Expand Down Expand Up @@ -187,7 +191,6 @@ func TestRPN(t *testing.T) {
}
got := decimal.WithPrecision(precision).Set(stack.top())
want := decimal.WithPrecision(precision).Set(tt.want)

if got.CmpTotal(want) != 0 {
t.Fatalf("diff: input: %s, want: %s, got: %s", tt.input, want, got)
}
Expand Down
14 changes: 13 additions & 1 deletion operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func newOpsType(ctx decimal.Context, stack *stackType) *opsType {
return []*decimal.Big{bigUint(z)}, 2, nil
}},
"",
"BOLD:Trigonometric Operations",
"BOLD:Trigonometric and Log Operations",
ophandler{"sin", "Sine of x", 1, func(a []*decimal.Big) ([]*decimal.Big, int, error) {
z := ctx.Sin(big(), radOrDeg(ctx, a[0], ret.degmode))
return []*decimal.Big{z}, 1, nil
Expand All @@ -206,6 +206,18 @@ func newOpsType(ctx decimal.Context, stack *stackType) *opsType {
z := ctx.Atan(big(), radOrDeg(ctx, a[0], ret.degmode))
return []*decimal.Big{z}, 1, nil
}},
ophandler{"exp", "Calculate e ^ x", 1, func(a []*decimal.Big) ([]*decimal.Big, int, error) {
z := ctx.Exp(big(), a[0])
return []*decimal.Big{z}, 1, nil
}},
ophandler{"ln", "Natural logarithm of x", 1, func(a []*decimal.Big) ([]*decimal.Big, int, error) {
z := ctx.Log(big(), a[0])
return []*decimal.Big{z}, 1, nil
}},
ophandler{"log", "Common logarithm of x", 1, func(a []*decimal.Big) ([]*decimal.Big, int, error) {
z := ctx.Log10(big(), a[0])
return []*decimal.Big{z}, 1, nil
}},

"",
"BOLD:Miscellaneous Operations",
Expand Down

0 comments on commit d789957

Please # to comment.