stdlib icon indicating copy to clipboard operation
stdlib copied to clipboard

quantile function of Beta distribution is broken on production build with Next.js

Open bvdmitri opened this issue 1 year ago • 8 comments

Description

I have an application written in Next.js and some code that uses beta distribution from '@stdlib/stats-base-dists/dist'. I noticed that the quantile function is completely broken while building the application in production mode (next build && next start). For example the output in Chrome console:

> dist = new window.beta_test.Beta(5, 5) // I did an ugly hack window.beta_test = beta in the code
w {}
> dist.quantile(0.1)
0.9999999996744877
> dist.quantile(0.5)
4.6663115970492885e-302

The quantile function works as expected if I run the application in the development mode (next dev)

> dist = new window.beta_test.Beta(5, 5)
t {}
> dist.quantile(0.1)
0.30096876359321467
> dist.quantile(0.5)
0.5

I'm not sure if this is the case with other distributions

Related Issues

Related issues # , # , and # .

Questions

No.

Demo

No response

Reproduction

  • a
  • b
  • c

Expected Results

No response

Actual Results

No response

Version

0.2.2

Environments

Chrome

Browser Version

129.0.6668.91

Node.js / npm Version

No response

Platform

MacOS

Checklist

  • [x] Read and understood the Code of Conduct.
  • [x] Searched for existing issues and pull requests.

bvdmitri avatar Oct 15 '24 14:10 bvdmitri

:wave: Hi there! :wave:

And thank you for opening your first issue! We will get back to you shortly. :runner: :dash:

stdlib-bot avatar Oct 15 '24 14:10 stdlib-bot

@Planeshifter Any ideas here?

kgryte avatar Oct 15 '24 22:10 kgryte

Currently am unable to reproduce the issue in a Next.js app; similarly attached beta distribution namespace to beta_test and ran the above commands. I am seeing the expected results.

@bvdmitri Does the same happen when loading @stdlib/stats-base-dists instead of the optimized distribution bundles from @stdlib/stats-base-dists/dist? If so, would you be able to upload a repository with a reproduction of the bug?

Planeshifter avatar Oct 16 '24 01:10 Planeshifter

Hey @Planeshifter , thanks for checking! Yes, the issue also happens when I change the import statement to @stdlib/stats-base-dists.

console.log(new beta.Beta(5,5).quantile(.5))

outputs 4.6663115970492885e-302. This is on Next.js 14.2.15 (last release) and this is only happens if I use production build with pnpm build && pnpm start. This is not happening in the development mode with pnpm dev. I, unfortunately, cannot share the codebase.

However, I also tried Next.js 15.0 (still in beta afaiu) and the issue is not present there and everything works as expected. That makes me think that some configuration options have changed for the build process. Do you know what in principle can cause the mathematical operations to be malformed during bundling in js? I'm not an expert in js/ts bundlers build processes so this is as far as I can go.

bvdmitri avatar Oct 16 '24 08:10 bvdmitri

I suppose it could be possible that minification is reordering operations, but one would need to confirm by examining the bundled output.

kgryte avatar Oct 16 '24 08:10 kgryte

Here is the link to the reproducer https://github.com/bvdmitri/nextjs-stdlib-bug

import { beta } from '@stdlib/stats-base-dists';

export default function Home() {
  const result = new beta.Beta(5, 5).quantile(0.5);
  return <div>{result}</div>;
}

Image

bvdmitri avatar Oct 16 '24 08:10 bvdmitri

I also wanted to file this issue within Next.js repository, but since it is fixed on the next beta version its not considered as a bug. But updating our codebase to the latest unreleased beta of Next.js is somewhat disruptive, so any help here is appreciated.

bvdmitri avatar Oct 16 '24 08:10 bvdmitri

For other people who also encounter the same issue I was able to hot-fix it with the following option in next.config.mjs

const nextConfig = {
    swcMinify: false
};

bvdmitri avatar Oct 17 '24 08:10 bvdmitri