189 lines
No EOL
5 KiB
HTML
189 lines
No EOL
5 KiB
HTML
---
|
|
layout: default
|
|
title: "Cancellation error"
|
|
redirect_from:
|
|
- /01331134959
|
|
---
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
|
<html>
|
|
<head>
|
|
<title>Cancellation error</title>
|
|
<link rel="stylesheet" href="https://media.unpythonic.net/css/bootstrap.min.css">
|
|
<style>
|
|
|
|
|
|
#mask {
|
|
position: absolute;
|
|
left: 0;
|
|
top: 0;
|
|
background-color: #000;
|
|
opacity: 0.7;
|
|
display: none;
|
|
}
|
|
|
|
#loading {
|
|
position: fixed;
|
|
left: 50%;
|
|
top: 50%;
|
|
width: 58px;
|
|
height: 60px;
|
|
background-color: black;
|
|
background-image: url('https://media.unpythonic.net/img/loading.gif');
|
|
opacity: 0.9;
|
|
z-index: 20;
|
|
display: none;
|
|
border-radius: 4px;
|
|
}
|
|
|
|
#bigimg {
|
|
position: fixed;
|
|
top: 0;
|
|
left: 0;
|
|
width: 100%;
|
|
height: 100%;
|
|
display: none;
|
|
}
|
|
|
|
#maskouter {
|
|
height: 100%;
|
|
display: table;
|
|
margin: 0 auto;
|
|
}
|
|
|
|
#maskinner {
|
|
vertical-align: middle;
|
|
display: table-cell;
|
|
}
|
|
|
|
#imgleft, #imgright {
|
|
cursor: pointer;
|
|
position: fixed;
|
|
top: 50%;
|
|
width: 64px;
|
|
height: 64px;
|
|
border-radius: 4px;
|
|
background-color: black;
|
|
z-index: 22;
|
|
opacity: 0.7;
|
|
}
|
|
|
|
#imgleft {
|
|
left: 0;
|
|
background-image: url('https://media.unpythonic.net/img/prevbtn.png');
|
|
}
|
|
|
|
#imgleft.disabled, #imgright.disabled { display: none; }
|
|
|
|
#imgright {
|
|
right: 0;
|
|
background-image: url('https://media.unpythonic.net/img/nextbtn.png');
|
|
}
|
|
|
|
#imgdesc {
|
|
position: fixed;
|
|
left: 64px;
|
|
bottom: 0;
|
|
background-color: #333;
|
|
width: 100%;
|
|
padding-top: 1em;
|
|
padding-bottom: 1em;
|
|
padding-left: 2em;
|
|
border: 1px solid #555;
|
|
border-bottom: 0;
|
|
}
|
|
|
|
#imgdesc .filename {
|
|
color: #fff;
|
|
font-weight: bold;
|
|
}
|
|
|
|
#imgdesc .description {
|
|
color: #fff;
|
|
padding-left: 1em;
|
|
}
|
|
|
|
#mask { z-index: 10990; }
|
|
#loading { z-index: 10991; }
|
|
#bigimg { z-index: 10992; }
|
|
|
|
</style>
|
|
<script type="text/javascript" src="https://media.unpythonic.net/js/jquery-1.6.4.min.js"></script>
|
|
<script type="text/javascript" src="https://media.unpythonic.net/js/bootstrap-dropdown.js"></script>
|
|
<script type="text/javascript" src="https://media.unpythonic.net/js/jquery.mousewheel.js"></script>
|
|
<script type="text/javascript" src="https://media.unpythonic.net/js/jquery.ba-hashchange.min.js"></script>
|
|
<script type="text/javascript" src="https://media.unpythonic.net/js/jquery.mobile.custom.min.js"></script>
|
|
<script type="text/javascript" src="https://media.unpythonic.net/js/gallery.js"></script>
|
|
<style><!--
|
|
div.floatimageleft, div.floatimageright {
|
|
border: 1px solid black;
|
|
padding: 2px;
|
|
margin: 1em;
|
|
}
|
|
div.floatimageleft { clear: left; float: left; }
|
|
div.floatimageright { clear: right; float: right; }
|
|
div.floatimageleft img, div.floatimageright img { border: 0px; }
|
|
|
|
div.floatimageleft img.zoom, div.floatimageright img.zoom {
|
|
padding: 4px;
|
|
}
|
|
--></style>
|
|
<style><!--
|
|
div.albumouter {
|
|
margin-right: 1ex;
|
|
margin-top: 1ex;
|
|
vertical-align: baseline;
|
|
display: -moz-inline-box;
|
|
display: inline-block;
|
|
text-decoration: none !important;
|
|
}
|
|
|
|
.album { display: block; clear: both; }
|
|
|
|
.album a:link, .album a:hover, .album a:visited
|
|
{ text-decoration: none; }
|
|
.album a:hover { color: red; }
|
|
|
|
.albumimage {
|
|
display: block;
|
|
padding-left: 2px; padding-right: 2px;
|
|
padding-top: 3px; padding-bottom: 3px;
|
|
background: #ececec;
|
|
}
|
|
|
|
.albumimage img.zoom {
|
|
margin-left: 0px; margin-right: 0px;
|
|
padding-top: 4px; padding-right: 4px; }
|
|
.albumimage img { border: 0px;
|
|
display: block; margin-left: auto; margin-right:auto
|
|
}
|
|
--></style>
|
|
|
|
<meta name="description" content="I was recently reminded of the importance of choosing numeric algorithms that
|
|
don't behave catastrophically for certain inputs. One example is the
|
|
calculation of 1-cos(θ) for small θ. In this case…">
|
|
</head>
|
|
<body>
|
|
|
|
|
|
I was recently reminded of the importance of choosing numeric algorithms that
|
|
don't behave catastrophically for certain inputs. One example is the
|
|
calculation of 1-cos(θ) for small θ. In this case, cos(θ) is very close to 1,
|
|
leading to a <a href="http://en.wikipedia.org/wiki/Loss_of_significance">large
|
|
cancellation error</a> in the subtraction step.
|
|
|
|
<p>What is to be done about this? Seek out an equivalent expression that does
|
|
not suffer from cancellation error. In this case, employ the double-angle
|
|
formula cos(2u) = 1-2sin²(u). This yields the equivalent expression
|
|
2sin(θ/2)², which gives good numeric results for θ close to zero.
|
|
|
|
<p>This plot shows how the accuracy of the original formula starts to visibly
|
|
degrade at around 1e-7 and gets steadily worse until, at about 1e-8, it
|
|
actually drops to 0.
|
|
|
|
|
|
<p><img src="https://media.unpythonic.net/emergent-files/01331134959/cancelerr.png">
|
|
|
|
<p>I wonder whether it would be useful to have a valgrind-like tool warn when
|
|
disastrous cancellation occurs, or whether typical programs would have way
|
|
too many such errors to ever contemplate fixing them all…
|
|
<br><br><font size=-2>Entry first conceived on 7 March 2012, 15:42 UTC, last modified on 16 October 2013, 14:35 UTC</font>
|
|
</body>
|
|
</html> |