Skip to content

Commit cff02dd

Browse files
zakhenryzak-cloudnc
authored andcommitted
fix(splitFloat32): Fix incorrect exponent when converting double to float
Submitted on behalf of a third-party: Ihor Darkov See protocolbuffers/protobuf#6887 (comment) for original source
1 parent bb4c138 commit cff02dd

2 files changed

Lines changed: 10 additions & 1 deletion

File tree

binary/utils.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,11 @@ jspb.utils.splitFloat32 = function(value) {
202202

203203
exp = Math.floor(Math.log(value) / Math.LN2);
204204
mant = value * Math.pow(2, -exp);
205-
mant = Math.round(mant * jspb.BinaryConstants.TWO_TO_23) & 0x7FFFFF;
205+
mant = Math.round(mant * jspb.BinaryConstants.TWO_TO_23);
206+
if (mant >= 0x1000000) {
207+
++exp;
208+
}
209+
mant = mant & 0x7FFFFF;
206210

207211
jspb.utils.split64High = 0;
208212
jspb.utils.split64Low = ((sign << 31) | ((exp + 127) << 23) | mant) >>> 0;

binary/utils_test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,11 @@ describe('binaryUtilsTest', function() {
391391
// Pi.
392392
test(f32_pi, 0x40490fdb);
393393

394+
// corner cases
395+
test(0.9999999762949594, 0x3f800000);
396+
test(7.99999999999999, 0x41000000);
397+
test(Math.sin(30 * Math.PI / 180), 0x3f000000); // sin(30 degrees)
398+
394399
// Various positive values.
395400
var cursor = f32_eps * 10;
396401
while (cursor != Infinity) {

0 commit comments

Comments
 (0)