Skip to content

Commit

Permalink
Merge pull request #2759 from quantified-uncertainty/percentage-tagging
Browse files Browse the repository at this point in the history
Tag percentages with certain format
  • Loading branch information
OAGr committed Dec 18, 2023
2 parents e681c2e + 24079c2 commit 3df0469
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/few-panthers-wonder.md
@@ -0,0 +1,5 @@
---
"@quri/squiggle-lang": patch
---

Tag percentates with percentage format
2 changes: 1 addition & 1 deletion packages/squiggle-lang/__tests__/ast/parse_test.ts
Expand Up @@ -48,7 +48,7 @@ describe("Peggy parse", () => {
});

describe("units", () => {
testEvalToBe("100%", "1");
testEvalToBe("100%", '1, with params numberFormat: ".2~p"');
testEvalToBe("1-0%", "1");
});

Expand Down
19 changes: 14 additions & 5 deletions packages/squiggle-lang/src/fr/units.ts
@@ -1,6 +1,7 @@
import { makeDefinition } from "../library/registry/fnDefinition.js";
import { frNumber } from "../library/registry/frTypes.js";
import { frForceBoxed, frNumber } from "../library/registry/frTypes.js";
import { FnFactory } from "../library/registry/helpers.js";
import { BoxedArgs } from "../value/boxed.js";

const maker = new FnFactory({
nameSpace: "",
Expand All @@ -10,24 +11,32 @@ const maker = new FnFactory({
const makeUnitFn = (
shortName: string,
fullName: string,
multiplier: number
multiplier: number,
format?: string
) => {
return maker.make({
output: "Number",
output: format ? "Boxed" : "Number",
name: "fromUnit_" + shortName,
description: `Unit conversion from ${fullName}.`,
examples: [`3${shortName} // ${3 * multiplier}`],
isUnit: true,
definitions: [
makeDefinition([frNumber], frNumber, ([x]) => x * multiplier),
format
? makeDefinition([frNumber], frForceBoxed(frNumber), ([x]) => {
return {
value: x * multiplier,
args: new BoxedArgs({ numberFormat: format }),
};
})
: makeDefinition([frNumber], frNumber, ([x]) => x * multiplier),
],
});
};

export const library = [
makeUnitFn("n", "nano", 1e-9),
makeUnitFn("m", "mili", 1e-3),
makeUnitFn("%", "percent", 1e-2),
makeUnitFn("%", "percent", 1e-2, ".2~p"),
makeUnitFn("k", "kilo", 1e3),
makeUnitFn("M", "mega", 1e6),
makeUnitFn("B", "billion", 1e9),
Expand Down

2 comments on commit 3df0469

@vercel
Copy link

@vercel vercel bot commented on 3df0469 Dec 18, 2023

Choose a reason for hiding this comment

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

@vercel
Copy link

@vercel vercel bot commented on 3df0469 Dec 18, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.