@@ -31,10 +31,6 @@ const {
3131 indexOfBuffer,
3232 indexOfNumber,
3333 indexOfString,
34- readDoubleBE : _readDoubleBE ,
35- readDoubleLE : _readDoubleLE ,
36- readFloatBE : _readFloatBE ,
37- readFloatLE : _readFloatLE ,
3834 swap16 : _swap16 ,
3935 swap32 : _swap32 ,
4036 swap64 : _swap64 ,
@@ -1197,35 +1193,80 @@ Buffer.prototype.readInt32BE = function readInt32BE(offset, noAssert) {
11971193} ;
11981194
11991195
1200- Buffer . prototype . readFloatLE = function readFloatLE ( offset , noAssert ) {
1201- offset = offset >>> 0 ;
1202- if ( ! noAssert )
1203- checkOffset ( offset , 4 , this . length ) ;
1204- return _readFloatLE ( this , offset ) ;
1196+ // For the casual reader who has not at the current time memorized the
1197+ // IEEE-754 standard in full detail: floating point numbers consist of
1198+ // a fraction, an exponent and a sign bit: 23+8+1 bits for single precision
1199+ // numbers and 52+11+1 bits for double precision numbers.
1200+ //
1201+ // A zero exponent is either a positive or negative zero, if the fraction
1202+ // is zero, or a denormalized number when it is non-zero. Multiplying the
1203+ // fraction by the smallest possible denormal yields the denormalized number.
1204+ //
1205+ // An all-bits-one exponent is either a positive or negative infinity, if
1206+ // the fraction is zero, or NaN when it is non-zero. The standard allows
1207+ // both quiet and signalling NaNs but since NaN is a canonical value in
1208+ // JavaScript, we cannot (and do not) distinguish between the two.
1209+ //
1210+ // Other exponents are regular numbers and are computed by subtracting the bias
1211+ // from the exponent (127 for single precision, 1023 for double precision),
1212+ // yielding an exponent in the ranges -126-127 and -1022-1024 respectively.
1213+ //
1214+ // Of interest is that the fraction of a normal number has an extra bit of
1215+ // precision that is not stored but is reconstructed by adding one after
1216+ // multiplying the fraction with the result of 2**-bits_in_fraction.
1217+
1218+
1219+ function toDouble ( x0 , x1 ) {
1220+ const frac = x0 + 0x100000000 * ( x1 & 0xFFFFF ) ;
1221+ const expt = ( x1 >>> 20 ) & 2047 ;
1222+ const sign = ( x1 >>> 31 ) ? - 1 : 1 ;
1223+ if ( expt === 0 ) {
1224+ if ( frac === 0 ) return sign * 0 ;
1225+ return sign * frac * 2 ** - 1074 ;
1226+ } else if ( expt === 2047 ) {
1227+ if ( frac === 0 ) return sign * Infinity ;
1228+ return NaN ;
1229+ }
1230+ return sign * 2 ** ( expt - 1023 ) * ( 1 + frac * 2 ** - 52 ) ;
1231+ }
1232+
1233+
1234+ function toFloat ( x ) {
1235+ const frac = x & 0x7FFFFF ;
1236+ const expt = ( x >>> 23 ) & 255 ;
1237+ const sign = ( x >>> 31 ) ? - 1 : 1 ;
1238+ if ( expt === 0 ) {
1239+ if ( frac === 0 ) return sign * 0 ;
1240+ return sign * frac * 2 ** - 149 ;
1241+ } else if ( expt === 255 ) {
1242+ if ( frac === 0 ) return sign * Infinity ;
1243+ return NaN ;
1244+ }
1245+ return sign * 2 ** ( expt - 127 ) * ( 1 + frac * 2 ** - 23 ) ;
1246+ }
1247+
1248+
1249+ Buffer . prototype . readDoubleBE = function ( offset , noAssert ) {
1250+ const x1 = this . readUInt32BE ( offset + 0 , noAssert ) ;
1251+ const x0 = this . readUInt32BE ( offset + 4 , noAssert ) ;
1252+ return toDouble ( x0 , x1 ) ;
12051253} ;
12061254
12071255
1208- Buffer . prototype . readFloatBE = function readFloatBE ( offset , noAssert ) {
1209- offset = offset >>> 0 ;
1210- if ( ! noAssert )
1211- checkOffset ( offset , 4 , this . length ) ;
1212- return _readFloatBE ( this , offset ) ;
1256+ Buffer . prototype . readDoubleLE = function ( offset , noAssert ) {
1257+ const x0 = this . readUInt32LE ( offset + 0 , noAssert ) ;
1258+ const x1 = this . readUInt32LE ( offset + 4 , noAssert ) ;
1259+ return toDouble ( x0 , x1 ) ;
12131260} ;
12141261
12151262
1216- Buffer . prototype . readDoubleLE = function readDoubleLE ( offset , noAssert ) {
1217- offset = offset >>> 0 ;
1218- if ( ! noAssert )
1219- checkOffset ( offset , 8 , this . length ) ;
1220- return _readDoubleLE ( this , offset ) ;
1263+ Buffer . prototype . readFloatBE = function ( offset , noAssert ) {
1264+ return toFloat ( this . readUInt32BE ( offset , noAssert ) ) ;
12211265} ;
12221266
12231267
1224- Buffer . prototype . readDoubleBE = function readDoubleBE ( offset , noAssert ) {
1225- offset = offset >>> 0 ;
1226- if ( ! noAssert )
1227- checkOffset ( offset , 8 , this . length ) ;
1228- return _readDoubleBE ( this , offset ) ;
1268+ Buffer . prototype . readFloatLE = function ( offset , noAssert ) {
1269+ return toFloat ( this . readUInt32LE ( offset , noAssert ) ) ;
12291270} ;
12301271
12311272
0 commit comments