stdlib-js / stdlib

✨ Standard library for JavaScript and Node.js. ✨
https://stdlib.io
Apache License 2.0
4.37k stars 447 forks source link

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

Open bvdmitri opened 5 days ago

bvdmitri commented 5 days ago

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

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

stdlib-bot commented 5 days ago

:wave: Hi there! :wave:

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

kgryte commented 5 days ago

@Planeshifter Any ideas here?

Planeshifter commented 5 days ago

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?

bvdmitri commented 4 days ago

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.

kgryte commented 4 days ago

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

bvdmitri commented 4 days ago

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 commented 4 days ago

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 commented 3 days ago

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
};