Skip to content

Commit

Permalink
added flip_sign
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielJDufour committed Apr 26, 2023
1 parent 86988f9 commit 6011b96
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 0 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ The following functions are supported:
- [divide](#divide)
- [exp](#exp)
- [factorial](#factorial)
- [flip_sign](#flip-sign)
- [floor](#floor)
- [hypotenuse](#hypotenuse)
- [max](#max)
Expand Down Expand Up @@ -169,6 +170,21 @@ factorial("10");
"3628800"
```

### flip_sign
```js
import flip_sign from "preciso/flip_sign.js";

flip_sign(10);
-10

flip_sign(-99)
99

// zero has no sign change
flip_sign(0)
0
```

### floor
```js
import floor from "preciso/floor.js";
Expand Down
22 changes: 22 additions & 0 deletions flip_sign.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"use strict";

const absolute = require("./absolute.js");
const clean = require("./clean.js");
const sign = require("./sign.js");

function flip_sign(n) {
n = clean(n);
const s = sign(n);
if (s === "") {
return n;
} else if (s === "-") {
return absolute(n);
} else if (s === "+") {
return "-" + n;
}

return sum;
}

module.exports = flip_sign;
module.exports.default = flip_sign;
1 change: 1 addition & 0 deletions preciso.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export function cube(
export function cube_root(radicand: string, options?: { imaginary?: boolean; max_decimal_digits?: number }): string;
export function divide(dividend: string, divisor: string, options?: { max_decimal_digits: number; ellipsis: boolean }): string;
export function factorial(n: string): string;
export function flip_sign(n: string): string;
export function floor(n: string): string;

// you can also pass in max(a, b, c, d) but I'm not sure how to type that
Expand Down
2 changes: 2 additions & 0 deletions preciso.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const eulers_number = require("./eulers_number.js");
const exp = require("./exp.js");
const expand = require("./expand.js");
const factorial = require("./factorial.js");
const flip_sign = require("./flip_sign.js");
const floor = require("./floor.js");
const fraction = require("./fraction.js");
// const gregory_leibniz = require("./gregory_leibniz.js");
Expand Down Expand Up @@ -81,6 +82,7 @@ const module_exports = {
exp,
expand,
factorial,
flip_sign,
floor,
fraction,
// gregory_leibniz,
Expand Down
1 change: 1 addition & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const {
eulers_number,
exp,
factorial,
flip_sign,
fraction,
floor,
// gregory_leibniz,
Expand Down

0 comments on commit 6011b96

Please sign in to comment.