美文网首页Julia语言
求直角三角形的斜边有何困难?

求直角三角形的斜边有何困难?

作者: Julia语言 | 来源:发表于2018-09-07 09:19 被阅读16次

微信公众号:Julia语言
每周一三五更新Julia语言
每周二四六更新Python进阶

What’s so hard about finding a hypotenuse?

https://www.johndcook.com/blog/2010/06/02/whats-so-hard-about-finding-a-hypotenuse/

Numerical libraries usually have a function for finding the hypotenuse of a right triangle. Isn’t this trivial? If the sides of a triangle are x and y, just write this:

sqrt(x*x + y*y)

That works in theory, but in practice it may fail. If x is so large that x*x overflows, the code will produce an infinite result.

We’d like to be able to say to the computer “Now xx and yy might be too big, but just wait a minute. I’m going to take a square root, and so the numbers will get smaller. I just need to borrow some extra range for a minute.” You can do something like that in environments that have extended precision, but that would be inefficient and unnecessary. And of course it would fail completely if you’re already using the largest numeric type available.

Here’s how to compute sqrt(xx + yy) without risking overflow.

  1. max = maximum(|x|, |y|)
  2. min = minimum(|x|, |y|)
  3. r = min / max
  4. return max*sqrt(1 + r^2)

Notice that the square root argument is between 1 and 2 and so there is no danger of overflow in taking the square root. The only way the algorithm could overflow is if the result itself is too large to represent. In that case the fault lies with the problem, not the algorithm: the algorithm was asked to compute a quantity larger than the largest representable number, and so it does the correct thing by overflowing and returning an infinite value. However, the algorithm will not unnecessarily return an infinite value due to an overflow in an intermediate computation.

To see how the algorithm above may succeed when the naive implementation would fail, let M be the largest representable number. For IEEE double precision, M is on the order of 10^308. All that matters for this example is that M is greater than 4. Let x and y equal 0.5 M. The algorithm above would return 0.707 M. But the naive algorithm would compute xx and overflow since 0.25 M² > M. Then xx + y*y would evaluate to infinity and the square root would evaluate to infinity.

备注

Julia语言中,可以直接使用hypot(x, y)函数求取斜边。

julia> hypot(3,4)
5.0

julia> hypot(5,12)
13.0
欢迎关注微信公众账号Julia语言.jpg

点击阅读原文可查看历史文章

相关文章

  • 求直角三角形的斜边有何困难?

    微信公众号:Julia语言每周一三五更新Julia语言;每周二四六更新Python进阶; What’s so ha...

  • 图形探究

    如图所示,由直角三角形两直角边上一点与斜边中点连线剪去两个三角形得到了一个直角梯形。求原直角三角形斜边的长。 下面...

  • 一个初中几何题

    问题:某直角三角形的斜边长为 10 ,斜边上的高为 6 ,求此三角形的面积。答案是简单的 吗? 这么想可就掉入陷阱...

  • R语言基础--函数构建

    函数的编写 参考文章 格式如下所示: 函数案例A 输入直角三角形的两个边长,求斜边长 运行结果如下所示: 函数案例...

  • 巧用直角三角形斜边中线定理解决问题

    直角三角形斜边中线定理是初中几何的重要内容,它的概念是:如果一个三角形是直角三角形,那么这个三角形斜边上的中线等于...

  • python如何开平方

    案例1:给出直角三角形的两条直角边,求第三条斜边的长度。 答案: 学习的地方:不会开平方,gg了一下,开平方是 *...

  • 上海中考冲刺 | 解答题23题

    Ø23题剖析 一、关于线段中点的联想及解题技巧 •直角三角形斜边中线等于斜边的一半(逆定理:如果三角形一边上的中线...

  • 勾股定理&统计数字问题

    勾股定理 题目:勾股定理,西方称为毕达哥拉斯定理,它所对应的三角形现在称为:直角三角形。已知直角三角形的斜边是某个...

  • 勾股定理的应用,如何找勾股数

    勾股定理:直角三角形两条直角边a、b的平方和等于斜边c的平方(a²+b²=c²) 勾股定理的证明方法有几百种,例如...

  • 勾股定理

    我国古代把直角三角形中,较短的直角边,称为“勾”;较长的直角边,称为“股”;斜边称为“弦”。 真是不要太美。勾股弦...

网友评论

    本文标题:求直角三角形的斜边有何困难?

    本文链接:https://www.haomeiwen.com/subject/ztdmwftx.html