Git Product home page Git Product logo

Comments (7)

mourner avatar mourner commented on June 14, 2024 1

I agree that the original form of the issue report was not very productive, but the underlying criticism is sound — toFixed should be used only for formatting, not for numeric operations. In this case it's better not only as a potential micro-optimization, but also for consistency, since the table values are rounded numerically (with Math.round(1e4 * value) / 1e4):

return (
Math.round((0.5 + (sum / SQRT_2PI) * Math.exp((-z * z) / 2)) * 1e4) /
1e4
);

from simple-statistics.

tmcw avatar tmcw commented on June 14, 2024

So, the lines are:

// The index we calculate must be in the table as a positive value,
// but we still pay attention to whether the input is positive
// or negative, and flip the output value as a last step.
if (z >= 0) {
return standardNormalTable[index];
} else {
// due to floating-point arithmetic, values in the table with
// 4 significant figures can nevertheless end up as repeating
// fractions when they're computed here.
return +(1 - standardNormalTable[index]).toFixed(4);
}

There's an explanatory comment explaining why, in this method that's trying to return things with 4 significant figures, it uses .toFixed.

And, the expression +(1 - standardNormalTable[index]).toFixed(4); does not return a string. Does it for you, or was that an assumption?

from simple-statistics.

TheDirigible avatar TheDirigible commented on June 14, 2024

from simple-statistics.

tmcw avatar tmcw commented on June 14, 2024

It's a normal distribution table, which has either four or five digits of precision, in this case, four.

Converting to a string and back is not a costly conversion, relative to virtually everything else. If that's actually a bottleneck in your application, that'd be surprising, but if it is based on a profile, and you have a faster technique, I'm more than happy to merge a pull request to limit significant digits in a faster way.

from simple-statistics.

TheDirigible avatar TheDirigible commented on June 14, 2024

from simple-statistics.

tmcw avatar tmcw commented on June 14, 2024

Okay, I'm happy to accept a PR if you want to write one, to improve performance here. I will say - I'm specifically talking about identifying toFixed as a bottleneck in a real-world program. Microbenchmarks can identify differences between Math.round versus the ~~ bit-twiddling hack, but 98% of those who care about that, including me in a previous iteration, are wrong to focus on it - the difference never rises to 1% of total-application performance budget, and usually gets optimized away once some V8 engineer decides to make Math.random or toFixed or etc faster.

I will say, I'm really happy to discuss this stuff, but in the future - this discussion started with an incorrect assumption based on a misreading of the code, led to a question answered by a comment in the code, and now is about microbenchmarking. It would be certainly nicer and more productive to have fewer assumptions and a generally more collaborative intent.

from simple-statistics.

TheDirigible avatar TheDirigible commented on June 14, 2024

from simple-statistics.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.