48

Math in V8 is Broken and How We Can Fix It - Athan Reines, Fourier

Embed Size (px)

Citation preview

Page 1: Math in V8 is Broken and How We Can Fix It - Athan Reines, Fourier
Page 2: Math in V8 is Broken and How We Can Fix It - Athan Reines, Fourier

Math in V8 is Broken / Athan Reines @kgryte

Page 3: Math in V8 is Broken and How We Can Fix It - Athan Reines, Fourier

STANDARD LIBRARY

Page 4: Math in V8 is Broken and How We Can Fix It - Athan Reines, Fourier

ES5acosasinatanatan2cossintan

absexplog (ln)powsqrt

ceilfloorround

maxminrandom

Page 5: Math in V8 is Broken and How We Can Fix It - Athan Reines, Fourier

ES2015/ES6acoshasinhatanhcoshsinhtanh

signcbrtexpm1log10log1plog2

froundtrunc

hypotclz32imul

Page 6: Math in V8 is Broken and How We Can Fix It - Athan Reines, Fourier

COMPARISON

Page 7: Math in V8 is Broken and How We Can Fix It - Athan Reines, Fourier

GOLANGAbsAcoshAsinAsinhAtanAtan2AtanhCbrtCeilCopysign

CosCoshDimErfErfcExpExp2Expm1Float32bitsFloat64bits

FloorFrexpGammaHypotIlogbJ0J1JnLdexpLgamma

LogLog10Log1pLog2LogbMaxMinModModfNexta�er

Page 8: Math in V8 is Broken and How We Can Fix It - Athan Reines, Fourier

GOLANGNextA�er32PowPow10SignbitSinSincosSinhSqrtTanTanh

TruncY0Y1YnExpFloat64Float64IntInt63NormFloat64Perm

Uint32Zipf(Complex)(Big)

Page 9: Math in V8 is Broken and How We Can Fix It - Athan Reines, Fourier

SO WHAT?

Page 10: Math in V8 is Broken and How We Can Fix It - Athan Reines, Fourier

BUGS

Page 11: Math in V8 is Broken and How We Can Fix It - Athan Reines, Fourier

V8 AND NODENode EOL V8 Release

0.10.44 10-2016 3.14.5.9 05-2013

0.12.17 01-2017 3.28.71.19 11-2014

4.6.2 04-2018 4.5.103.42 08-2015

5.12.0 07-2016 4.6.85.32 11-2015

6.9.1 04-2019 5.1.281.84 07-2016

7.2.0 07-2017 5.4.500.43 11-2016

Page 12: Math in V8 is Broken and How We Can Fix It - Athan Reines, Fourier

Math.sin/Math.cosvar x = Math.pow( 2, 120 ); // returns 1.329227995784916e+36

var y = Math.sin( x ); // returns: -0.8783788442551665 // expected: 0.377820109360752

y = Math.cos( x ); // returns: 0.47796506772457525 // expected: -0.9258790228548378

Node v0.10

Page 13: Math in V8 is Broken and How We Can Fix It - Athan Reines, Fourier

NaNvar nan1 = 0.0 / 0.0; var nan2 = Number.POSITIVE_INFINITY / Number.POSITIVE_INFINITY;

var arr = [ nan1, nan2 ]; var FLOAT64_VIEW = new Float64Array( arr ); var INT32_VIEW = new Int32Array( FLOAT64_VIEW.buffer );

var isnan1 = ( FLOAT64_VIEW[0] !== FLOAT64_VIEW[0] ); // returns true

var isnan2 = ( FLOAT64_VIEW[1] !== FLOAT64_VIEW[1] ); // returns true

var bool = ( nan1 !== nan2 ); // returns true

Node v0.12 Node v4 Node v6 Node v7Node v0.10*

Page 14: Math in V8 is Broken and How We Can Fix It - Athan Reines, Fourier

Math.powvar x = Math.pow( 10, 308 ); // returns: 1.0000000000000006e+308 // expected: 1.0e+308

Node v0.10+

Page 15: Math in V8 is Broken and How We Can Fix It - Athan Reines, Fourier

Algorithmfunction pow( x, y ) { var m = x; var n = y; var p = 1; while ( n !== 0 ) { if ( ( n & 1 ) !== 0 ) { p *= m; } m *= m; if ( ( n & 2 ) !== 0 ) { p *= m; } m *= m; n >>= 2; } return p;

Page 16: Math in V8 is Broken and How We Can Fix It - Athan Reines, Fourier

Math.atanhvar y = Math.atanh( 1.0e-10 ); // returns: 1.000000082640371e-10 // expected: 1.0e-10

Node v0.12 Node v4 Node v6

Page 17: Math in V8 is Broken and How We Can Fix It - Athan Reines, Fourier

Math.acoshvar y = Math.acosh( 1.0 + 1.0e-10 ); // returns: 0.000014142136208733941 // expected: 0.000014142136208675862

Node v0.12 Node v4 Node v6

Page 18: Math in V8 is Broken and How We Can Fix It - Athan Reines, Fourier

Math.asinhvar y = Math.asinh( 1.0e-50 ); // returns: 0.0 // expected: 1.0e-50

y = Math.asinh( 1.0e200 ); // returns +infinity // expected: 461.2101657793691

Node v0.12 Node v4 Node v6

Page 19: Math in V8 is Broken and How We Can Fix It - Athan Reines, Fourier

Algorithmfunction asinh( x ) { if ( x === 0 || !isFinite( x ) ) { return x; } if ( x > 0 ) { return Math.log( x + Math.sqrt( x*x + 1 ) ); } return -Math.log( -x + Math.sqrt( x*x + 1 ) ); }

Page 20: Math in V8 is Broken and How We Can Fix It - Athan Reines, Fourier

Algorithmfunction asinh( x ) { var sgn; var xx; var t; if ( isnan( x ) || isinfinite( x ) ) { return x; } if ( x < 0.0 ) { x = -x; sgn = true; } // Case: |x| < 2**-28 if ( x < NEAR_ZERO ) { t = x; } // Case: |x| > 2**28

Page 21: Math in V8 is Broken and How We Can Fix It - Athan Reines, Fourier

Math.expvar y = Math.exp( 100.0 ); // returns: 2.6881171418161485e+43 // expected: 2.6881171418161356e+43

Node v0.12 Node v4 Node v6

Page 22: Math in V8 is Broken and How We Can Fix It - Athan Reines, Fourier

Math.randomuint32_t state0 = 1; uint32_t state1 = 2; uint32_t mwc1616() { state0 = 18030 * (state0 & 0xffff) + (state0 >> 16); state1 = 30903 * (state1 & 0xffff) + (state1 >> 16); return state0 << 16 + (state1 & 0xffff); }

Node v0.10 Node v0.12 Node v4

Page 23: Math in V8 is Broken and How We Can Fix It - Athan Reines, Fourier

Examplevar ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_'

function randomString( length ) { var randint; var str; var i;

str = ''; for ( i = 0; i < length; i++ ) { randint = Math.floor( Math.random() * ALPHABET.length ); str += ALPHABET.substring( randint, randint+1 ); } return str; }

  Betable

Page 25: Math in V8 is Broken and How We Can Fix It - Athan Reines, Fourier

JavaScript

Page 26: Math in V8 is Broken and How We Can Fix It - Athan Reines, Fourier

UnderspecificationCross-browser variabilityNo single codebaseVersioningRequired shims

GlobalsTestingNo golden algorithmsTimescaleTrust

Page 27: Math in V8 is Broken and How We Can Fix It - Athan Reines, Fourier

Community

Page 28: Math in V8 is Broken and How We Can Fix It - Athan Reines, Fourier

Polyfillsvar isFinite = require( 'is-finite' );

module.exports = function asinh( x ) { if ( x === 0 || !isFinite( x ) ) { return x; } return x < 0 ? -asinh( -x ) : Math.log( x + Math.sqrt( x*x + 1 ) );}

Page 29: Math in V8 is Broken and How We Can Fix It - Athan Reines, Fourier

Librariesfunction asinh( x ) { return Math.log( Math.sqrt( x*x + 1 ) + x ); }

Page 30: Math in V8 is Broken and How We Can Fix It - Athan Reines, Fourier

:(

Page 33: Math in V8 is Broken and How We Can Fix It - Athan Reines, Fourier

Opportunities

Page 34: Math in V8 is Broken and How We Can Fix It - Athan Reines, Fourier

Get Involved!

Page 35: Math in V8 is Broken and How We Can Fix It - Athan Reines, Fourier

AdvocateInt64/Uint64Int128/Uint128Typed Objects (complex)SIMD (long)

ParallelismGPGPUBigFloat/BigInt

Page 36: Math in V8 is Broken and How We Can Fix It - Athan Reines, Fourier

Thank you! / Athan Reines @kgryte

https://github.com/stdlib-js/stdlib

Page 37: Math in V8 is Broken and How We Can Fix It - Athan Reines, Fourier
Page 38: Math in V8 is Broken and How We Can Fix It - Athan Reines, Fourier

APPENDIX

Page 39: Math in V8 is Broken and How We Can Fix It - Athan Reines, Fourier
Page 40: Math in V8 is Broken and How We Can Fix It - Athan Reines, Fourier

WHAT CAN BE DONE AT THESPECIFICATION LEVEL?

Page 48: Math in V8 is Broken and How We Can Fix It - Athan Reines, Fourier

THE END