0

I am attempting to test scientific software written in TypeScript using Jest.

I want to test all my floating point results to 8 significant figures, but it seems the only test method available in ts-jest, "toBeCloseTo", works on matching figures after the decimal point in decimal notation.

https://jestjs.io/docs/en/expect#tobeclosetonumber-numdigits

The scientific and engineering community works to significant figures, rather than numbers after the decimal point. Have I overlooked some functionality. If not, is there a plan to add it?

In the meantime, I have a workaround which would only work for numbers where the significant figures after the decimal point in decimal notation are within the range of the significant figures required in scientific notation.

test("MyFunction", () =>{
    let sigAfterDecimal = SIG_FIG - Math.floor((Math.log(ExpectedResult)/Math.log(10)));
    expect(IF97_B23T(functionInput)).toBeCloseTo(SigAfterDecimal, thisTestAccuracy);
});

But this wouldn't work for checking eg. the Avogadro constant, 6.02214076×10^23 to 8 significant figures, as none of the relevant significant figures appear after the decimal point in decimal notation.

Teneff
  • 30,564
  • 13
  • 72
  • 103
C4rnot
  • 49
  • 1

1 Answers1

0

Well, I've had a go myself and have created a pull request to the main facebook/jest team to include my fork "float_sig_fig" which adds toBeCloseToSigFig which that the powers in normalized scientific notation are the same and that the mantissas match to a required number of significant figures. Thanks to Chux I made sure the log(0) case is covered.

It would be my first source contribution to an open source software project if it is accepted.

C4rnot
  • 49
  • 1
  • Message from the favebook/jest maintainer:Thanks for the PR! Unfortunately, we don't really accept new matchers at this time. I suggest you open a PR at jest-extended [link](https://github.com/jest-community/jest-extended) instead – C4rnot May 04 '20 at 10:05