Security News
The Risks of Misguided Research in Supply Chain Security
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.
js-hexfloat
Advanced tools
Rudimentary C99 Hexadecimal Float Support in JS
var pi = parseHexFloat('0x1.921fb54442d18p+1'); // 3.14159265358982
var piHex = Math.PI.toHexString(); // '0x3.243f6a8885a3p0'
parseHexFloat(piHex) == Math.PI; // true
This script adds the following:
parseHexFloat(theString)
Parses C99 hexadecimal floating point in theString
. Returns NaN
if fails.
Unlike parseInt
and parseFloat
, the number must be prepended with '0x' and ended with 'p' and exponent in decimal number.
also available as Number.parseHexFloat()
.
Number.prototype.toHexString(canonical)
Stringifies the number as a C99 hexadecimal notation. Analogous to "%a"
in C99 sprintf()
.
Unless canonical
is true
, the result is not canonical. Canonically the first digit of the number is always 1
and the sign of the exponent is never omitted.
printf("%a\n", -57005.7458343505859375); // prints -0x1.bd5b7ddep+15
On the other hand, this implementation takes advangage of the fact Number.prototype.toString(16)
works for floating point numbers. It just checks the number is negative and prepends '-' if so, then prepend '0x', and append 'p0'.
console.log((-57005.7458343505859375).toHexString()); // -0xdead.beefp0
console.log((-57005.7458343505859375).toHexString(true)); // -0x1.bd5b7ddep+15
Even when not canonical, you can use the result interchangeably with C99 and other platforms that support the notation (C++11, Ruby, Perl 5.22 ...).
FAQs
Rudimentary C99 Hexadecimal Floating Point Support in JS
The npm package js-hexfloat receives a total of 1 weekly downloads. As such, js-hexfloat popularity was classified as not popular.
We found that js-hexfloat demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.
Research
Security News
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.