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(); // '0x1.921fb54442d18p+1'
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()
.
RE_HEXFLOAT
RegExp
object used in parseHexFloat
:
/([\+\-]?)0x([0-9A-F]+).?([0-9A-F]*)p([\+\-]?[0-9]*)/i
Number.prototype.toHexString()
Stringifies the number as a C99 hexadecimal notation like "%a"
in C99 sprintf()
.
From version 0.3.0, .toHexString()
always returns the canonical notation.
Math.PI.toString(16); // 3.243f6a8885a3 -> 0x3.243f6a8885a3p0 is valid yet uncanonical
Math.PI.toHexString(); // 0x1.921fb54442d18p+1 is valid and canonical
It seems ok to just prepend '0x' and append 'p0' to .toString(16)
to make a hex float. Turns out it isn't. It sometimes drops the last bit in some platforms.
Math.log(2).toString(16) // 0.b17217f7d1cf78
Math.log(2).toHexString() // 0x1.62e42fefa39efp-1
% perl -E 'say 0x0.b17217f7d1cf78p0 - 0x1.62e42fefa39efp-1'
-1.11022302462516e-16
% perl -E 'say sprintf "%a", 0x0.b17217f7d1cf78p0 - 0x1.62e42fefa39efp-1'
-0x1p-53
And abs(-0x1p-53)
is DBL_EPSILON
.
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.