Skip to content

Commit 9466887

Browse files
committed
[IMP] functions: add LOG function
This commit adds the LOG function (logarithm of base N). closes #5714 Task: 4547230 X-original-commit: 08ee2d3 Signed-off-by: Lucas Lefèvre (lul) <[email protected]> Signed-off-by: Adrien Minne (adrm) <[email protected]>
1 parent 5ee8d30 commit 9466887

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

src/functions/module_math.ts

+23
Original file line numberDiff line numberDiff line change
@@ -844,6 +844,29 @@ export const LN = {
844844
isExported: true,
845845
} satisfies AddFunctionDescription;
846846

847+
// -----------------------------------------------------------------------------
848+
// LOG
849+
// -----------------------------------------------------------------------------
850+
export const LOG: AddFunctionDescription = {
851+
description: _t("The logarithm of a number, for a given base."),
852+
args: [
853+
arg("value (number)", _t("The value for which to calculate the logarithm.")),
854+
arg("base (number, default=10)", _t("The base of the logarithm.")),
855+
],
856+
compute: function (
857+
value: Maybe<FunctionResultObject>,
858+
base: Maybe<FunctionResultObject> = { value: 10 }
859+
): number {
860+
const _value = toNumber(value, this.locale);
861+
const _base = toNumber(base, this.locale);
862+
assert(() => _value > 0, _t("The value (%s) must be strictly positive.", _value.toString()));
863+
assert(() => _base > 0, _t("The base (%s) must be strictly positive.", _base.toString()));
864+
assert(() => _base !== 1, _t("The base must be different from 1."));
865+
return Math.log10(_value) / Math.log10(_base);
866+
},
867+
isExported: true,
868+
};
869+
847870
// -----------------------------------------------------------------------------
848871
// MOD
849872
// -----------------------------------------------------------------------------

tests/functions/module_math.test.ts

+31
Original file line numberDiff line numberDiff line change
@@ -1698,6 +1698,37 @@ describe("LN formula", () => {
16981698
});
16991699
});
17001700

1701+
describe("LOG function", () => {
1702+
test("LOG takes 1-2 arguments", () => {
1703+
expect(evaluateCell("A1", { A1: "=LOG()" })).toBe("#BAD_EXPR");
1704+
expect(evaluateCell("A1", { A1: "=LOG(10)" })).toBe(1);
1705+
expect(evaluateCell("A1", { A1: "=LOG(10, 10)" })).toBe(1);
1706+
expect(evaluateCell("A1", { A1: "=LOG(10, 10, 0)" })).toBe("#BAD_EXPR");
1707+
});
1708+
1709+
test("Value argument bust be strictly positive", () => {
1710+
expect(evaluateCell("A1", { A1: "=LOG(0)" })).toBe("#ERROR");
1711+
expect(evaluateCell("A1", { A1: "=LOG(-1)" })).toBe("#ERROR");
1712+
});
1713+
1714+
test("Base argument must be positive and different from 1", () => {
1715+
expect(evaluateCell("A1", { A1: "=LOG(10, -1)" })).toBe("#ERROR");
1716+
expect(evaluateCell("A1", { A1: "=LOG(10, 0)" })).toBe("#ERROR");
1717+
expect(evaluateCell("A1", { A1: "=LOG(10, 1)" })).toBe("#ERROR");
1718+
});
1719+
1720+
test.each([
1721+
[1, 2, 0],
1722+
[100, 10, 2],
1723+
[16, 2, 4],
1724+
[5, 6, 0.898244402],
1725+
[125.15, 0.1, -2.097430854],
1726+
])("function result =LOG(%s, %s)", (arg0: number, arg1: number, expectedResult: number) => {
1727+
const cellValue = evaluateCell("A1", { A1: `=LOG(${arg0}, ${arg1})` });
1728+
expect(cellValue).toBeCloseTo(expectedResult, 4);
1729+
});
1730+
});
1731+
17011732
describe("MOD formula", () => {
17021733
test.each([
17031734
["-42", "-10", -2],

0 commit comments

Comments
 (0)