Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

js-hexfloat

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

js-hexfloat

Rudimentary C99 Hexadecimal Float Support in JS

  • 0.1.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
decreased by-83.33%
Maintainers
1
Weekly downloads
 
Created
Source

build status

js-hexfloat

Rudimentary C99 Hexadecimal Float Support in JS

SYNOPSIS

var pi = parseHexFloat('0x1.921fb54442d18p+1'); // 3.14159265358982
var piHex = Math.PI.toHexString();              // '0x3.243f6a8885a3p0'
parseHexFloat(piHex) == Math.PI;                // true

DESCRIPTION

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().

CAVEAT

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 ...).

SEE ALSO

Keywords

FAQs

Package last updated on 04 Jan 2016

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc