SlideShare a Scribd company logo
Math in V8 is Broken and How We Can Fix It - Athan Reines, Fourier
Math in V8 is Broken
/ Athan Reines  @kgryte
STANDARD LIBRARY
ES5
acos
asin
atan
atan2
cos
sin
tan
abs
exp
log (ln)
pow
sqrt
ceil
floor
round
max
min
random
ES2015/ES6
acosh
asinh
atanh
cosh
sinh
tanh
sign
cbrt
expm1
log10
log1p
log2
fround
trunc
hypot
clz32
imul
COMPARISON
GOLANG
Abs
Acosh
Asin
Asinh
Atan
Atan2
Atanh
Cbrt
Ceil
Copysign
Cos
Cosh
Dim
Erf
Erfc
Exp
Exp2
Expm1
Float32bits
Float64bits
Floor
Frexp
Gamma
Hypot
Ilogb
J0
J1
Jn
Ldexp
Lgamma
Log
Log10
Log1p
Log2
Logb
Max
Min
Mod
Modf
Nexta er
GOLANG
NextA er32
Pow
Pow10
Signbit
Sin
Sincos
Sinh
Sqrt
Tan
Tanh
Trunc
Y0
Y1
Yn
ExpFloat64
Float64
Int
Int63
NormFloat64
Perm
Uint32
Zipf
(Complex)
(Big)
SO WHAT?
BUGS
V8 AND NODE
Node 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
Math.sin/Math.cos
var 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
NaN
var 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 v7
Node v0.10*
Math.pow
var x = Math.pow( 10, 308 );
// returns: 1.0000000000000006e+308
// expected: 1.0e+308
Node v0.10+
Algorithm
function 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;
Math.atanh
var y = Math.atanh( 1.0e-10 );
// returns: 1.000000082640371e-10
// expected: 1.0e-10
Node v0.12 Node v4 Node v6
Math.acosh
var y = Math.acosh( 1.0 + 1.0e-10 );
// returns: 0.000014142136208733941
// expected: 0.000014142136208675862
Node v0.12 Node v4 Node v6
Math.asinh
var 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
Algorithm
function 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 ) );
}
Algorithm
function 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
Math.exp
var y = Math.exp( 100.0 );
// returns: 2.6881171418161485e+43
// expected: 2.6881171418161356e+43
Node v0.12 Node v4 Node v6
Math.random
uint32_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
Example
var ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345678
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
  V8 blog
JavaScript
Underspecification
Cross-browser variability
No single codebase
Versioning
Required shims
Globals
Testing
No golden algorithms
Timescale
Trust
Community
Polyfills
var 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 ) );
}
Libraries
function asinh( x ) {
return Math.log( Math.sqrt( x*x + 1 ) + x );
}
:(
Math in V8 is Broken and How We Can Fix It - Athan Reines, Fourier
github.com/stdlib-js/stdlib
Opportunities
Get Involved!
Advocate
Int64/Uint64
Int128/Uint128
Typed Objects (complex)
SIMD (long)
Parallelism
GPGPU
BigFloat/BigInt
Thank you!
/ Athan Reines  @kgryte
https://github.com/stdlib-js/stdlib
Math in V8 is Broken and How We Can Fix It - Athan Reines, Fourier
APPENDIX
Math in V8 is Broken and How We Can Fix It - Athan Reines, Fourier
WHAT CAN BE DONE AT THE
SPECIFICATION LEVEL?
(and bitwise ops)Int64
Typed Objects
WebCL
SIMD (long)
Parallel Computing
Operator Overloading
Web Assembly
INTEGER SUPPORT
discussion
gist
Int64 in R
TYPED OBJECTS
spec
explainer
typed data structures in Go
WEBCL
node-opencl
SIMD
polyfill
Intel announcement
MDN
presentation
PARALLEL COMPUTING
Data parallelism
Task parallelism
Scheduler
Lock-free programming
Shared Memory Web Workers
OPERATOR OVERLOADING
operator-overloading-js
paper.js
paper.js source
THE END

More Related Content

PDF
Coroutines in Kotlin. UA Mobile 2017.
DOCX
Microcontroller (8051) general and simple alp n cprograms
PDF
Cilk Plus Parallel Reduction
PPTX
Protecting C++
PDF
PPT
PDF
Java, Up to Date Sources
PDF
DESTRUCTOR EXAMPLES
Coroutines in Kotlin. UA Mobile 2017.
Microcontroller (8051) general and simple alp n cprograms
Cilk Plus Parallel Reduction
Protecting C++
Java, Up to Date Sources
DESTRUCTOR EXAMPLES

What's hot (20)

PDF
One definition rule - что это такое, и как с этим жить
PPTX
Go Concurrency Basics
PDF
Алексей Кутумов, Coroutines everywhere
PDF
The Ring programming language version 1.5.2 book - Part 19 of 181
PDF
The Ring programming language version 1.5.2 book - Part 75 of 181
PDF
Golang Channels
PDF
Hacklu11 Writeup
PPT
Windows debugging sisimon
PPTX
C10k and beyond - Uri Shamay, Akamai
PDF
Goroutines and Channels in practice
PDF
Ethereum virtual machine for Developers Part 1
PDF
Advanced QUnit - Front-End JavaScript Unit Testing
PDF
Crash Fast & Furious
PDF
Facts about multithreading that'll keep you up at night - Guy Bar on, Vonage
PDF
PPS
Ecma script 5
PDF
Kirk Shoop, Reactive programming in C++
TXT
Sine Wave Generator with controllable frequency displayed on a seven segment ...
DOCX
Primer Punto
PPTX
.NET Core Summer event 2019 in Brno, CZ - Async demystified -- Karel Zikmund
One definition rule - что это такое, и как с этим жить
Go Concurrency Basics
Алексей Кутумов, Coroutines everywhere
The Ring programming language version 1.5.2 book - Part 19 of 181
The Ring programming language version 1.5.2 book - Part 75 of 181
Golang Channels
Hacklu11 Writeup
Windows debugging sisimon
C10k and beyond - Uri Shamay, Akamai
Goroutines and Channels in practice
Ethereum virtual machine for Developers Part 1
Advanced QUnit - Front-End JavaScript Unit Testing
Crash Fast & Furious
Facts about multithreading that'll keep you up at night - Guy Bar on, Vonage
Ecma script 5
Kirk Shoop, Reactive programming in C++
Sine Wave Generator with controllable frequency displayed on a seven segment ...
Primer Punto
.NET Core Summer event 2019 in Brno, CZ - Async demystified -- Karel Zikmund
Ad

Viewers also liked (20)

PPTX
The Morality of Code - Glen Goodwin, SAS Institute, inc.
PDF
Hitchhiker's Guide to"'Serverless" Javascript - Steven Faulkner, Bustle
PDF
State of the CLI- Kat Marchan
PDF
Node.js Core State of the Union- James Snell
PDF
Take Data Validation Seriously - Paul Milham, WildWorks
PDF
From Pterodactyls and Cactus to Artificial Intelligence - Ivan Seidel Gomes, ...
PDF
Real-Life Node.js Troubleshooting - Damian Schenkelman, Auth0
PDF
Multimodal Interactions & JS: The What, The Why and The How - Diego Paez, Des...
PDF
Developing Nirvana - Corey A. Butler, Author.io
PDF
Node's Event Loop From the Inside Out - Sam Roberts, IBM
PDF
Are your v8 garbage collection logs speaking to you?Joyee Cheung -Alibaba Clo...
PDF
Real-Time Machine Learning with Node.js - Philipp Burckhardt, Carnegie Mellon...
PDF
Web MIDI API - the paster, the present, and the future -
PDF
Comet with node.js and V8
ODP
IBM MQ v8 and JMS 2.0
PDF
Nodifying the Enterprise - Prince Soni, TO THE NEW
PDF
Text Mining with Node.js - Philipp Burckhardt, Carnegie Mellon University
PDF
Building Scalable Web Applications Using Microservices Architecture and NodeJ...
PDF
Node.js Event Loop & EventEmitter
PPTX
Express State of the Union at Nodejs Interactive EU- Doug Wilson
The Morality of Code - Glen Goodwin, SAS Institute, inc.
Hitchhiker's Guide to"'Serverless" Javascript - Steven Faulkner, Bustle
State of the CLI- Kat Marchan
Node.js Core State of the Union- James Snell
Take Data Validation Seriously - Paul Milham, WildWorks
From Pterodactyls and Cactus to Artificial Intelligence - Ivan Seidel Gomes, ...
Real-Life Node.js Troubleshooting - Damian Schenkelman, Auth0
Multimodal Interactions & JS: The What, The Why and The How - Diego Paez, Des...
Developing Nirvana - Corey A. Butler, Author.io
Node's Event Loop From the Inside Out - Sam Roberts, IBM
Are your v8 garbage collection logs speaking to you?Joyee Cheung -Alibaba Clo...
Real-Time Machine Learning with Node.js - Philipp Burckhardt, Carnegie Mellon...
Web MIDI API - the paster, the present, and the future -
Comet with node.js and V8
IBM MQ v8 and JMS 2.0
Nodifying the Enterprise - Prince Soni, TO THE NEW
Text Mining with Node.js - Philipp Burckhardt, Carnegie Mellon University
Building Scalable Web Applications Using Microservices Architecture and NodeJ...
Node.js Event Loop & EventEmitter
Express State of the Union at Nodejs Interactive EU- Doug Wilson
Ad

Similar to Math in V8 is Broken and How We Can Fix It - Athan Reines, Fourier (20)

PDF
Beyond Floating Point – Next Generation Computer Arithmetic
PDF
Web futures
PDF
PDF
Value objects in JS - an ES7 work in progress
PDF
Unum Computing: An Energy Efficient and Massively Parallel Approach to Valid ...
ODP
Learn JavaScript by modeling Rubik Cube
PDF
Extensible Operators and Literals for JavaScript
PDF
Fluent14
PDF
The Present and Future of the Web Platform
PDF
20151224-games
PDF
Beating Floating Point at its Own Game: Posit Arithmetic
PDF
Value Objects, Full Throttle (to be updated for spring TC39 meetings)
PDF
Catastrophic Cancellation
PPTX
Evgeniy Muralev, Mark Vince, Working with the compiler, not against it
PDF
Scilab is not naive
PDF
Scilabisnotnaive
 
PPS
CS101- Introduction to Computing- Lecture 35
PDF
2022-ranger-update-Cauldron for gcc versions
PDF
JS Responsibilities
PDF
Design of 32-bit Floating Point Unit for Advanced Processors
Beyond Floating Point – Next Generation Computer Arithmetic
Web futures
Value objects in JS - an ES7 work in progress
Unum Computing: An Energy Efficient and Massively Parallel Approach to Valid ...
Learn JavaScript by modeling Rubik Cube
Extensible Operators and Literals for JavaScript
Fluent14
The Present and Future of the Web Platform
20151224-games
Beating Floating Point at its Own Game: Posit Arithmetic
Value Objects, Full Throttle (to be updated for spring TC39 meetings)
Catastrophic Cancellation
Evgeniy Muralev, Mark Vince, Working with the compiler, not against it
Scilab is not naive
Scilabisnotnaive
 
CS101- Introduction to Computing- Lecture 35
2022-ranger-update-Cauldron for gcc versions
JS Responsibilities
Design of 32-bit Floating Point Unit for Advanced Processors

More from NodejsFoundation (6)

PDF
Workshop: Science Meets Industry: Online Behavioral Experiments with nodeGame...
PDF
Take Data Validation Seriously - Paul Milham, WildWorks
PDF
From Pterodactyls and Cactus to Artificial Intelligence - Ivan Seidel Gomes, ...
PDF
Breaking Down the Monolith - Peter Marton, RisingStack
PDF
The Enterprise Case for Node.js
PDF
Node Foundation Membership Overview 20160907
Workshop: Science Meets Industry: Online Behavioral Experiments with nodeGame...
Take Data Validation Seriously - Paul Milham, WildWorks
From Pterodactyls and Cactus to Artificial Intelligence - Ivan Seidel Gomes, ...
Breaking Down the Monolith - Peter Marton, RisingStack
The Enterprise Case for Node.js
Node Foundation Membership Overview 20160907

Recently uploaded (20)

PPTX
Machine Learning_overview_presentation.pptx
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PPTX
1. Introduction to Computer Programming.pptx
PPTX
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Getting Started with Data Integration: FME Form 101
PPTX
Tartificialntelligence_presentation.pptx
PPTX
cloud_computing_Infrastucture_as_cloud_p
PDF
Mushroom cultivation and it's methods.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
Spectroscopy.pptx food analysis technology
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Machine learning based COVID-19 study performance prediction
PDF
Univ-Connecticut-ChatGPT-Presentaion.pdf
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Machine Learning_overview_presentation.pptx
gpt5_lecture_notes_comprehensive_20250812015547.pdf
1. Introduction to Computer Programming.pptx
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Getting Started with Data Integration: FME Form 101
Tartificialntelligence_presentation.pptx
cloud_computing_Infrastucture_as_cloud_p
Mushroom cultivation and it's methods.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
MIND Revenue Release Quarter 2 2025 Press Release
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Spectroscopy.pptx food analysis technology
Diabetes mellitus diagnosis method based random forest with bat algorithm
Machine learning based COVID-19 study performance prediction
Univ-Connecticut-ChatGPT-Presentaion.pdf
A comparative analysis of optical character recognition models for extracting...
Unlocking AI with Model Context Protocol (MCP)
Profit Center Accounting in SAP S/4HANA, S4F28 Col11

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