Skip to content

Commit

Permalink
Add Math.log() operator (#320)
Browse files Browse the repository at this point in the history
I had a need for Math.log in an Animation calculation. This code all seemed to work and I think I found all the touch points, but I'm not well versed on the native side of things so please let me know if anything needs to change.
  • Loading branch information
simonbuerger authored and osdnk committed Jun 22, 2019
1 parent f6c67ca commit a1caabb
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 0 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,15 @@ sqrt(nodeOrNumber)

The square root of the given node. If the number is negative, an error is thrown.

---
### `log`

```js
log(nodeOrNumber)
```

The log of the given node.

---
### `sin`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ public double eval(Double x) {
return Math.sqrt(x);
}
};
private static final Operator LOG = new SingleOperator() {
@Override
public double eval(Double x) {
return Math.log(x);
}
};
private static final Operator SIN = new SingleOperator() {
@Override
public double eval(Double x) {
Expand Down Expand Up @@ -235,6 +241,8 @@ public OperatorNode(int nodeID, ReadableMap config, NodesManager nodesManager) {
mOperator = MODULO;
} else if ("sqrt".equals(op)) {
mOperator = SQRT;
} else if ("log".equals(op)) {
mOperator = LOG;
} else if ("sin".equals(op)) {
mOperator = SIN;
} else if ("cos".equals(op)) {
Expand Down
1 change: 1 addition & 0 deletions ios/Nodes/REAOperatorNode.m
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ - (instancetype)initWithID:(REANodeID)nodeID config:(NSDictionary<NSString *,id>
@"pow": REA_REDUCE(pow(a, b)),
@"modulo": REA_REDUCE(fmodf(fmodf(a, b) + b, b)),
@"sqrt": REA_SINGLE(sqrt(a)),
@"log": REA_SINGLE(log(a)),
@"sin": REA_SINGLE(sin(a)),
@"cos": REA_SINGLE(cos(a)),
@"tan": REA_SINGLE(tan(a)),
Expand Down
1 change: 1 addition & 0 deletions mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ module.exports = {
pow: NOOP,
modulo: NOOP,
sqrt: NOOP,
log: NOOP,
sin: NOOP,
cos: NOOP,
tan: NOOP,
Expand Down
1 change: 1 addition & 0 deletions react-native-reanimated.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ declare module 'react-native-reanimated' {
export const pow: MultiOperator;
export const modulo: MultiOperator;
export const sqrt: UnaryOperator;
export const log: UnaryOperator;
export const sin: UnaryOperator;
export const cos: UnaryOperator;
export const tan: UnaryOperator;
Expand Down
1 change: 1 addition & 0 deletions src/core/AnimatedOperator.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const OPERATIONS = {
pow: reduce((a, b) => Math.pow(a, b)),
modulo: reduce((a, b) => ((a % b) + b) % b),
sqrt: single(a => Math.sqrt(a)),
log: single(a => Math.log(a)),
sin: single(a => Math.sin(a)),
cos: single(a => Math.cos(a)),
tan: single(a => Math.tan(a)),
Expand Down
1 change: 1 addition & 0 deletions src/operators.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const divide = operator('divide');
export const pow = operator('pow');
export const modulo = operator('modulo');
export const sqrt = operator('sqrt');
export const log = operator('log');
export const sin = operator('sin');
export const cos = operator('cos');
export const exp = operator('exp');
Expand Down

0 comments on commit a1caabb

Please sign in to comment.