Skip to content

Instantly share code, notes, and snippets.

@sverhoeven
Created October 2, 2020 08:20
Show Gist options
  • Save sverhoeven/70101b54593ffd3972290ea9d3617777 to your computer and use it in GitHub Desktop.
Save sverhoeven/70101b54593ffd3972290ea9d3617777 to your computer and use it in GitHub Desktop.
run-cpp-on-web: webassembly
<html>
<head>
<!-- Load WebAssembly module -->
<script type="text/javascript" src="newtonraphson.js"></script>
</head>
<body>
<div>
Function root is approximately at x =
<span id="answer"/>
</div>
<script>
// Wait for module to initialize,
createModule().then(({NewtonRaphson}) => {
// Hardcoded input values
const initial_guess = -4;
const tolerance = 0.001;
// Perform computation
const newtonraphson = new NewtonRaphson(tolerance);
const root = newtonraphson.solve(initial_guess);
// Write the value of 'root' to the tag whose 'id' is equal to "answer"
document.getElementById("answer").innerHTML = root.toFixed(2);
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment