TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

How do computers calculate sine?

227 点作者 vegesm大约 1 年前

25 条评论

staplung大约 1 年前
I recently learned how Doom was ported to the SNES. It&#x27;s quite impressive. The SNES hardware was nowhere near fast enough to do all the trig calculations needed for the game but cartridge based games had a trick up their sleeve: they could include actual hardware <i>inside the cart</i> that the game code could make use of. It was more expensive but if you expected to sell a boatload of copies, it could be worth it. However, even using extra hardware wasn&#x27;t enough in this case. So they pre-calculated lookup tables for sine, cosine, tangent etc. for every angle at the necessary precision. They were helped by the fact that the game resolution in this case was fairly low.<p>If you&#x27;re interested, you can peruse the C code that was used to generate the tables. Here&#x27;s the file for sine&#x2F;cosine:<p><a href="https:&#x2F;&#x2F;github.com&#x2F;RandalLinden&#x2F;DOOM-FX&#x2F;blob&#x2F;master&#x2F;source&#x2F;mksin.c">https:&#x2F;&#x2F;github.com&#x2F;RandalLinden&#x2F;DOOM-FX&#x2F;blob&#x2F;master&#x2F;source&#x2F;m...</a>
评论 #39637064 未加载
评论 #39637980 未加载
评论 #39637326 未加载
评论 #39638035 未加载
评论 #39639061 未加载
评论 #39638783 未加载
评论 #39641976 未加载
warpech大约 1 年前
This made me realize that trigonometric functions are not deterministic across different CPU architectures, OS, and programming languages (floating point precision aside).<p>E.g. I would assume that Math.sin(x) returns the same thing in NodeJS on Windows and Mac&#x2F;M1, but it turns out it is necessarily so. <a href="https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;74074312&#x2F;standard-math-functions-reproducibility-on-different-cpus" rel="nofollow">https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;74074312&#x2F;standard-math-f...</a>
评论 #39634680 未加载
评论 #39636927 未加载
评论 #39633877 未加载
评论 #39637227 未加载
评论 #39634117 未加载
评论 #39633730 未加载
评论 #39634002 未加载
评论 #39633683 未加载
评论 #39636223 未加载
jxy大约 1 年前
It&#x27;s much clearer if you read one of the source code of the libm.<p>Plan 9: <a href="https:&#x2F;&#x2F;9p.io&#x2F;sources&#x2F;plan9&#x2F;sys&#x2F;src&#x2F;libc&#x2F;port&#x2F;sin.c" rel="nofollow">https:&#x2F;&#x2F;9p.io&#x2F;sources&#x2F;plan9&#x2F;sys&#x2F;src&#x2F;libc&#x2F;port&#x2F;sin.c</a><p>Freebsd: <a href="https:&#x2F;&#x2F;cgit.freebsd.org&#x2F;src&#x2F;tree&#x2F;lib&#x2F;msun&#x2F;src&#x2F;k_sin.c" rel="nofollow">https:&#x2F;&#x2F;cgit.freebsd.org&#x2F;src&#x2F;tree&#x2F;lib&#x2F;msun&#x2F;src&#x2F;k_sin.c</a>
评论 #39636185 未加载
评论 #39639569 未加载
评论 #39639641 未加载
microtherion大约 1 年前
P.J. Plauger&#x27;s _The Standard C Library_ provides an implementation for all functions in the (then) C standard: <a href="https:&#x2F;&#x2F;www.amazon.com&#x2F;Standard-Library-P-J-Plauger&#x2F;dp&#x2F;0138380120?ref_=ast_author_dp&amp;dib=eyJ2IjoiMSJ9.J2FZMVfhMnUjy3nORwaNfJ39GKhZvMa1t-YBXfQeaEgAuQf63AYkxWWCauQjjBeo9Z3_OrNF4PZDHa-l_tdvzR3ooYzJBGyfigUwXLxNHiszIfSYtPsgIzjEKpUmRBVOSMlrnXBoG26XMFM6WGfX6gSXoSJCch-ZygQNZo-OuqAfqvHlSY4NZBnBxp3ORQySq32fkwuWqv516zBqZMCmp6fPUdbJG1rJgG9Z5yJxIc8.Kz9qxgL-6ZhzUqEeAARs4sTCGFg8uFGlXxRP21iGrRc&amp;dib_tag=AUTHOR" rel="nofollow">https:&#x2F;&#x2F;www.amazon.com&#x2F;Standard-Library-P-J-Plauger&#x2F;dp&#x2F;01383...</a>
toolslive大约 1 年前
After reducing the interval, you don&#x27;t want to use the Taylor series as you&#x27;re building an approximation that&#x27;s really good in 0 but not so good moving away from 0. It&#x27;s better to use an interpolating polynomial (Chebychev comes to mind) over the whole target interval.
评论 #39635858 未加载
tails4e大约 1 年前
I&#x27;ve seen the third order Taylor series used, but with the coefficients calculated at various offsets for a quarter wave. So you lookuo where you are in the quarter wave, then look up the 3 or 4 cofficients. This keeps the error somewhat bounded as the size of X is a small so the series does not diverge too much.
eska大约 1 年前
You might also find this video interesting:<p>&quot;Finding the BEST sine function for Nintendo 64&quot;<p><a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=xFKFoGiGlXQ" rel="nofollow">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=xFKFoGiGlXQ</a>
eh_why_not大约 1 年前
Anyone experienced with the Remez algorithm mentioned at the end of the article?<p>The degree-9 polynomial, said to be a thousand times better than the original Taylor approximation in maximum error, also appears to be very close to the Taylor series in the first place.<p>Rounding the Taylor coefficients to 6 digits after the decimal:<p>1&#x2F;3! = 0.166667<p>1&#x2F;5! = 0.008333<p>1&#x2F;7! = 0.000198<p>1&#x2F;9! = 0.000027(56)<p>The first 2 are exact, the third is 5 digits only (so 0.000190), and the fourth is more different starting from the 6th digit (0.000026019).<p>The delta in the 9-th order is expected if you were to truncate the Taylor series starting from the 11th order to infinity (+ x^11 &#x2F; 11! - x^13&#x2F;13! ...).
评论 #39643029 未加载
评论 #39636721 未加载
评论 #39651403 未加载
azhenley大约 1 年前
I blogged my adventure of implementing cosine from scratch and how others have done it:<p><a href="https:&#x2F;&#x2F;austinhenley.com&#x2F;blog&#x2F;cosine.html" rel="nofollow">https:&#x2F;&#x2F;austinhenley.com&#x2F;blog&#x2F;cosine.html</a>
dboreham大约 1 年前
This was my first use of open source, around 1978. I wondered how calculators and computers did trig functions, and was also using Unix V7. We had a large disk and kept the source on line. So I was able to find this: <a href="https:&#x2F;&#x2F;www.tuhs.org&#x2F;cgi-bin&#x2F;utree.pl?file=V7&#x2F;usr&#x2F;src&#x2F;libm&#x2F;sin.c" rel="nofollow">https:&#x2F;&#x2F;www.tuhs.org&#x2F;cgi-bin&#x2F;utree.pl?file=V7&#x2F;usr&#x2F;src&#x2F;libm&#x2F;s...</a> and from there this book: <a href="https:&#x2F;&#x2F;www.biblio.com&#x2F;book&#x2F;computer-approximations-john-f-hart-e&#x2F;d&#x2F;1504828236" rel="nofollow">https:&#x2F;&#x2F;www.biblio.com&#x2F;book&#x2F;computer-approximations-john-f-h...</a>
duped大约 1 年前
1 - cos^2(x), obviously
评论 #39633802 未加载
评论 #39634553 未加载
convolvatron大约 1 年前
<a href="http:&#x2F;&#x2F;steve.hollasch.net&#x2F;cgindex&#x2F;math&#x2F;inccos.html" rel="nofollow">http:&#x2F;&#x2F;steve.hollasch.net&#x2F;cgindex&#x2F;math&#x2F;inccos.html</a> is a great technique if you need a fast integer approximation for some some arbitrary sampling interval (i.e. motor control)
评论 #39636407 未加载
aidenn0大约 1 年前
Just using the 32 entry LUT and the small-angle approximations (sin(x) = x, cos(x) = 1-x^2&#x2F;2) lets you calculate sin(x) within +&#x2F;- 0.00015, which is rather impressive for something that can be done quickly by hand.<p>If you use cos(x) = 1 then you are still accurate to within 1%<p>[edit]<p>I think I also found an error in TFA? It seems like picking the &quot;best&quot; N would allow r to be in the range -pi&#x2F;32 &lt; r &lt; pi&#x2F;32, which makes the 3rd order Taylor series have an error of 4e-12, significantly better than the error range for 0 &lt; r &lt; pi&#x2F;16
vardump大约 1 年前
CORDIC is how it&#x27;s usually done in hardware (and FPGAs).<p><a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;CORDIC" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;CORDIC</a>
评论 #39633819 未加载
评论 #39633701 未加载
评论 #39634345 未加载
Tade0大约 1 年前
I was truly amazed when my high school computer science teacher expanded the sine function into a Taylor series for us to implement in class.<p>Couldn&#x27;t wrap my head around the concept until the topic was revisited in college, but idea was there and helped me understand the tradeoffs brought by &quot;fast math&quot; libraries I was using during high school.
t-3大约 1 年前
Link doesn&#x27;t appear to be valid, but aren&#x27;t these usually precalculated and stored in a lookup table?
评论 #39643161 未加载
评论 #39655284 未加载
mettamage大约 1 年前
What’s the best way to calculate it by hand?<p>I’m brushing up my math basics (I graduated CS while dodging the math requirements) and it frustrates me that in trig I need to remember values at all. The values such as sqrt(2)&#x2F;2 make sense but how hard is it to calculate sin(5 degrees) by hand?
评论 #39637138 未加载
评论 #39637397 未加载
评论 #39640104 未加载
sema4hacker大约 1 年前
I remember seeing the source code for a version of SpaceWar! running on an Adage Graphics Terminal (a one&#x27;s complement machine) around 1970 that used a precomputed sine table.<p>I wonder what the first program was that ever used a precomputed trig table.
simonblack大约 1 年前
The same way you can eat an elephant: one byte at a time.<p>Any calculating job can be undertaken by a proper Turing machine. You just have to keep in mind the old triangle of usage: Cost, capability and speed.<p>If a human can calculate a sine, so can any full-blown computer.
demondemidi大约 1 年前
I thought modern CPUs since the late 1980&#x27;s used a lookup table for trig&#x2F;transcendental functions. Is the LUT just an expansion of the polynomial? I never really understood how FPUs worked...
评论 #39636684 未加载
评论 #39636694 未加载
评论 #39638509 未加载
perihelions大约 1 年前
Is this still current? The paper has a publication year of 1999.
deepthaw大约 1 年前
Ignorant question:<p>Given the ridiculous number of transistors and so on we can use in CPUs and GPUs nowadays how feasible is a relatively huge trig lookup table burned into rom?
评论 #39634428 未加载
评论 #39638496 未加载
评论 #39634362 未加载
Solvency大约 1 年前
Why isn&#x27;t this just done with an industry standard lookup table these days?
paulpauper大约 1 年前
sine is easy because the series is globally convergent and fast converging
评论 #39637099 未加载
评论 #39633394 未加载
amelius大约 1 年前
And arcsin?
评论 #39635861 未加载