round-float32.js (1918B)
1 // Bug 1073910 2 (function() { 3 function roundf(y) { 4 return Math.round(Math.fround(y)); 5 } 6 7 var x = -1; 8 assertEq(roundf(x), x); 9 assertEq(roundf(x), x); 10 11 var x = -2; 12 assertEq(roundf(x), x); 13 assertEq(roundf(x), x); 14 15 var x = -1024; 16 assertEq(roundf(x), x); 17 18 var x = -14680050; 19 assertEq(roundf(x), Math.fround(x)); 20 21 var x = -8388610; 22 assertEq(roundf(x), Math.fround(x)); 23 })(); 24 25 // Bug 1000606 26 (function() { 27 function f() { 28 var d = Math.fround(0.4999999701976776); 29 return Math.round(d); 30 } 31 assertEq(f(), f()); 32 33 function g() { 34 var c = Math.fround(8886111); 35 return Math.round(c); 36 } 37 assertEq(g(), g()); 38 })(); 39 40 // Bug 1124485 41 (function() { 42 function h(x) { 43 var y = Math.fround(x); 44 assertEq(y, Math.pow(y, 1)); 45 } 46 h(0); 47 h(2147483647); 48 })(); 49 50 // Bug 1122344 51 (function() { 52 function f() { 53 return Math.round(Math.fround(-13527757)); 54 }; 55 assertEq(f(), f()); 56 })(); 57 58 (function() { 59 // Test values around -0.5 and +0.5 60 var f32 = new Float32Array(1); 61 var i32 = new Int32Array(f32.buffer); 62 63 function round(x) { return Math.round(x); } 64 function roundf(x) { return Math.round(Math.fround(x)); } 65 66 // Warm up 67 round(2.5); 68 round(3.5); 69 roundf(2.5); 70 roundf(3.5); 71 72 f32[0] = 0.5; 73 i32[0] += 1; 74 print('0.5+e =', f32[0]); 75 76 var x = f32[0]; 77 assertEq(round(x), 1); 78 assertEq(roundf(x), 1); 79 80 f32[0] = 0.5; 81 i32[0] -= 1; 82 print('0.5-e =', f32[0]); 83 84 var x = f32[0]; 85 assertEq(round(x), 0); 86 assertEq(roundf(x), 0); 87 88 f32[0] = -0.5; 89 i32[0] += 1; 90 print('-0.5-e =', f32[0]); 91 92 var x = f32[0]; 93 assertEq(round(x), -1); 94 assertEq(roundf(x), -1); 95 96 f32[0] = -0.5; 97 i32[0] -= 1; 98 print('-0.5+e =', f32[0]); 99 100 var x = f32[0]; 101 assertEq(round(x), -0); 102 assertEq(roundf(x), -0); 103 })();