作者pmove (热脸迎人满是伤)
看板MacDev
标题[问题] Codewars 6级数学题(可用Swift)
时间Tue Dec 13 19:23:39 2022
今天尝试用Swift解难度6级的Floating-point Approximation (I):
https://www.codewars.com/kata/58184387d14fc32f2b0012b2
结果只有测试资料Pass, 传送过去就Fail了。
想了一个下午,最後只好看解答,
看了解答,我还是想不出来…为什麽?有人能明讲,
这题解答的原理是什麽吗?
题目很短,我顺便Po在下面:
Consider the function
f: x -> sqrt(1 + x) - 1 at x = 1e-15.
We get: f(x) = 4.44089209850062616e-16
or something around that, depending on the language.
This function involves the subtraction of a pair of similar numbers when x is
near 0 and the results are significantly erroneous in this region. Using pow
instead of sqrt doesn't give better results.
A "good" answer is 4.99999999999999875... * 1e-16.
Can you modify f(x) to give a good approximation of f(x) in the neighborhood
of 0?
Note:
Don't round or truncate your results. See the testing function in Sample
Tests:.
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 1.161.9.149 (台湾)
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/MacDev/M.1670930622.A.315.html
1F:→ pmove: 我想出来了,只用到简单公式,(x+1) * (x- 1.161.9.149 12/13 20:49
2F:→ pmove: 1) = x^2 - 1 ,讨论区还有人说要用到泰勒 1.161.9.149 12/13 20:49
3F:→ pmove: 展开式…笑死 1.161.9.149 12/13 20:49
4F:→ pmove: 这题拿去问New Bing, 它可以解的出来喔,只 1.161.38.179 03/26 10:12
5F:→ pmove: 是程式码累赘了一点 1.161.38.179 03/26 10:12