js-hexfloat
Advanced tools
Comparing version 0.3.1 to 0.4.1
/* | ||
* $Id: hexfloat.js,v 0.3 2016/01/04 17:53:08 dankogai Exp dankogai $ | ||
* $Id: hexfloat.js,v 0.4 2016/01/05 07:13:22 dankogai Exp dankogai $ | ||
* | ||
@@ -12,7 +12,11 @@ * Licensed under the MIT license. | ||
} | ||
var RE_HEXFLOAT = | ||
/([\+\-]?)0x([0-9A-F]+)\.?([0-9A-F]*)p([\+\-]?[0-9]*)/i; | ||
var pat_hexfloat = | ||
'([\+\-]?)0x([0-9A-F]+)\.?([0-9A-F]*)p([\+\-]?[0-9]*)'; | ||
// 1 2 3 4 | ||
var parseHexFloat = function(s) { | ||
var m = RE_HEXFLOAT.exec(s); | ||
var RE_HEXFLOAT = new RegExp(pat_hexfloat, 'i'); | ||
var RE_HEXFLOAT_G = new RegExp(pat_hexfloat, 'gi'); | ||
var parseHexFloat = function() { | ||
var m = arguments.length < 2 | ||
? RE_HEXFLOAT.exec(arguments[0]) | ||
: arguments; | ||
if (!m) return NaN; | ||
@@ -37,5 +41,6 @@ var mantissa = parseInt(m[1] + m[2] + m[3], 16); | ||
[ | ||
[ global, 'parseHexFloat', parseHexFloat], | ||
[ Number, 'parseHexFloat', parseHexFloat], | ||
[ global, 'RE_HEXFLOAT', RE_HEXFLOAT], | ||
[ global, 'parseHexFloat', parseHexFloat ], | ||
[ Number, 'parseHexFloat', parseHexFloat ], | ||
[ global, 'RE_HEXFLOAT', RE_HEXFLOAT ], | ||
[ global, 'RE_HEXFLOAT_G', RE_HEXFLOAT_G ], | ||
[ Number.prototype, 'toHexString', toHexString ] | ||
@@ -42,0 +47,0 @@ ].forEach(function(a){ |
{ | ||
"name": "js-hexfloat", | ||
"version": "0.3.1", | ||
"version": "0.4.1", | ||
"description": "Rudimentary C99 Hexadecimal Floating Point Support in JS", | ||
@@ -5,0 +5,0 @@ "main": "hexfloat.js", |
@@ -27,2 +27,15 @@ [![build status](https://secure.travis-ci.org/dankogai/js-hexfloat.png)](http://travis-ci.org/dankogai/js-hexfloat) | ||
#### `parseHexFloat` as a callback | ||
From version 0.4.0, you can use `parseHexFloat` with `String.prototype.replace`. | ||
````javascript | ||
var text = 'e=0x1.5bf0a8b145769p+1, pi=0x1.921fb54442d18p+1'; | ||
text.replace(RE_HEXFLOAT_G, parseHexFloat) // 'e=2.718281828459045, pi=3.141592653589793'; | ||
```` | ||
In the case above, parseHexFloat does not apply regexp by itself. Instead it just takes its `arguments` as the matched result. | ||
Unfortunately `text.replace(RE_HEXFLOAT, parseHexFloat, 'g')` is not standard so `RE_HEXFLOAT_G` is added. | ||
### `RE_HEXFLOAT` | ||
@@ -36,2 +49,6 @@ | ||
### `RE_HEXFLOAT_G` | ||
`RE_HEXFLOAT` with a `g` (global) flag. See above for usage. | ||
### `Number.prototype.toHexString()` | ||
@@ -50,3 +67,3 @@ | ||
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. | ||
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 bits in some occasions. | ||
@@ -69,4 +86,27 @@ ```` | ||
* http://en.cppreference.com/w/c/language/floating_constant | ||
* https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html | ||
* http://search.cpan.org/dist/perl-5.22.0/pod/perldelta.pod#Floating_point_parsing_has_been_improved | ||
* http://www.exploringbinary.com/hexadecimal-floating-point-constants/ | ||
## Appendix: HexFloat in Other Languages | ||
* C | ||
* Standard since C99 | ||
* Literals, output via `%a` in `printf` format, input via `strtod()`, ... | ||
* https://gcc.gnu.org/onlinedocs/gcc/Hex-Floats.html | ||
* C++ | ||
* Standard since C++11 | ||
* C features plus `std::hexfloat`, ... | ||
* http://www.cplusplus.com/reference/ios/hexfloat/ | ||
* Perl 5 | ||
* Supported since 5.22 | ||
* Pretty much like C99: literals, the `%a` format, `POSIX::strtod()`, ... | ||
* http://perldoc.perl.org/perl5220delta.html | ||
* http://www.effectiveperlprogramming.com/2015/06/perl-v5-22-adds-hexadecimal-floating-point-literals/ | ||
* Ruby | ||
* Supported since 1.9? | ||
* http://docs.ruby-lang.org/ja/1.9.3/doc/print_format.html | ||
* http://docs.ruby-lang.org/ja/1.8.7/doc/print_format.html | ||
* No literals, output via `%a` for `sprintf`, input via `Float()` | ||
* Swift | ||
* Supported from the beginning | ||
* Literals, output via `String(format:"%a", ...)`, input via `Double()`, ... | ||
* https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ |
/* | ||
* $Id: test.js,v 0.2 2016/01/04 17:52:54 dankogai Exp dankogai $ | ||
* $Id: test.js,v 0.3 2016/01/05 07:13:40 dankogai Exp dankogai $ | ||
* | ||
@@ -9,3 +9,6 @@ * use mocha to test me | ||
assert = require("assert"); | ||
parseHexFloat = require('../hexfloat.js').parseHexFloat; | ||
G = require('../hexfloat.js'); | ||
parseHexFloat = G.parseHexFloat; | ||
RE_HEXFLOAT = G.RE_HEXFLOAT; | ||
RE_HEXFLOAT_G = G.RE_HEXFLOAT_G; | ||
} | ||
@@ -19,2 +22,4 @@ var is = function (a, e, m) { | ||
var dead_beef = 57005.7458343505859375; | ||
var src = 'e=0x1.5bf0a8b145769p+1, pi=0x1.921fb54442d18p+1'; | ||
var dst = 'e=2.718281828459045, pi=3.141592653589793'; | ||
@@ -54,2 +59,4 @@ describe('Hexadecimal Float', function () { | ||
is(Number.MAX_VALUE.toHexString(), '0x1.fffffffffffffp+1023')); | ||
it('str.replace(RE_HEXFLOAT_G, parseHexFloat)', | ||
is(src.replace(RE_HEXFLOAT_G, parseHexFloat), dst)); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
19783
111
109