I am trying to do this:
const numerator = 268435456;
const denominator = 2 ** 64;
const decimalFraction = numerator / denominator;
To make this work, I have tried to use the code suggested here: https://stackoverflow.com/a/54409977/3846032:
const rawValue = 268435456n;
const exponent = 2n ** 64n;
const precision = 100_000_000;
const fraction = Number((rawValue * BigInt(precision)) / exponent) / precision;
When I run this in dev it works fine, but in prod I get this error:
Uncaught TypeError: can't convert BigInt to number
I believe it is coming from 2n ** 64n
.
I'm using create-react-app and some digging reveals this: https://github.com/facebook/create-react-app/issues/6907#issuecomment-778178677).
Unfortunately this suggestion makes it fail in both dev and prod with an OOM error.
Any suggestions on how to make this work would be appreciated.
CodePudding user response:
Use Math.pow(2, 64)
instead of 2 ** 64
:
const Example = () => {
const x = 268435456;
const y = Math.pow(2, 64);
return (x / y);
}
ReactDOM.render(<Example />, document.getElementById("react"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.1/umd/react-dom.production.min.js"></script>
<div id="react"></div>
CodePudding user response:
So it turns out the OOM I was getting was unrelated to this particular problem, so disregard that part.
As for this error:
Uncaught TypeError: can't convert BigInt to number
You can avoid it by doing Math.power(2, 64)
instead of 2n ** 64n
.
The issue in question: https://github.com/facebook/create-react-app/issues/11705.