Skip to content

Commit

Permalink
Merge pull request #2235 from quantified-uncertainty/complex-error-on…
Browse files Browse the repository at this point in the history
…-power-fix

Support dist^integer for negative values
  • Loading branch information
berekuk committed Aug 21, 2023
2 parents 658c2a2 + 3254b77 commit 04c5577
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/tough-flowers-tap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@quri/squiggle-lang": patch
---

Support pow(dist, integer) on distributions with negative samples
14 changes: 14 additions & 0 deletions packages/squiggle-lang/__tests__/library/distOperations_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { testEvalToBe } from "../helpers/reducerHelpers.js";

// TODO - it'd be useful to do most of `./sym_test.ts` tests here, without `Sym.` prefix
describe("Dist operations", () => {
describe("Power on negative samples", () => {
// ok when power is integer
testEvalToBe("normal(-100, 1) ^ 2", "Sample Set Distribution");
// fails when power is not an integer
testEvalToBe(
"normal(-100, 1) ^ 2.5",
"Error(Distribution Math Error: Operation returned complex result)"
);
});
});
9 changes: 6 additions & 3 deletions packages/squiggle-lang/src/operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,14 @@ export const Convolution = {
type OperationFn = (a: number, b: number) => result<number, OperationError>;

const power: OperationFn = (a, b) => {
if (a >= 0) {
return Ok(a ** b);
} else {
const result = a ** b;
if (Number.isNaN(result)) {
if (Number.isNaN(a) || Number.isNaN(b)) {
return Ok(result); // bad, but the issue is upstream of `power` operation
}
return Result.Err(new ComplexNumberError());
}
return Ok(result);
};

const add: OperationFn = (a, b) => Ok(a + b);
Expand Down

3 comments on commit 04c5577

@vercel
Copy link

@vercel vercel bot commented on 04c5577 Aug 21, 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 04c5577 Aug 21, 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 04c5577 Aug 21, 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.