美文网首页Julia语言
看起来不必要的数学函数

看起来不必要的数学函数

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

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

Math library functions that seem unnecessary

https://www.johndcook.com/blog/2010/06/07/math-library-functions-that-seem-unnecessary/

This post will give several examples of functions include in the standard C math library that seem unnecessary at first glance.

Function log1p(x) = log(1 + x)

The function log1p computes log(1 + x). How hard could this be to implement?

julia> log(1+9)
2.302585092994046

julia> log1p(9)
2.302585092994046

But wait. What if x is very small? If x is 10^-16, for example, then on a typical system 1 + x = 1 because machine precision does not contain enough significant bits to distinguish 1 + x from 1. (For details, see Anatomy of a floating point number.) That means that the code log(1 + x) would first compute 1 + x, obtain 1, then compute log(1), and return 0. But log(1 + 10^-16) ≈ 10^-16. This means the absolute error is about 10-16 and the relative error is 100%. For values of x larger than 10-16 but still fairly small, the code log(1 + x) may not be completely inaccurate, but the relative error may still be unacceptably large.

Fortunately, this is an easy problem to fix. For small x, log(1 + x) is approximately x. So for very small arguments, just return x. For larger arguments, compute log(1 + x) directly. See details here.

Why does this matter? The absolute error is small, even if the code returns a zero for a non-zero answer. Isn’t that OK? Well, it might be. It depends on what you do next. If you add the result to a large number, then the relative error in the answer doesn’t matter. But if you multiply the result by a large number, your large relative error becomes a large absolute error as well.

Function expm1(x) = exp(x) – 1

Another function that may seem unnecessary is expm1. This function computes e^x – 1. Why not just write this?

julia> exp(8)-1
2979.9579870417283

julia> expm1(8)
2979.9579870417283

That’s fine for moderately large x. For very small values of x, exp(x) is close to 1, maybe so close to 1 that it actually equals 1 to machine precision. In that case, the code exp(x) - 1 will return 0 and result in 100% relative error. As before, for slightly larger values of xthe naive code will not be entirely inaccurate, but it may be less accurate than needed. The solution is to compute exp(x) – 1 directly without first computing exp(x). The Taylor series for exp(x) is 1 + x + x2/2 … So for very small x, we could just return x for exp(x) – 1. Or for slightly larger x, we could return x + x2/2. (See details here.)

欢迎关注微信公众账号Julia语言.jpg

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

相关文章

  • 看起来不必要的数学函数

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

  • JS中的数学函数Math

    JS中的数学函数MathMath 称为数学函数,属于对象类型的函数

  • C语言009 第九节课-数学函数进行计算 2019-06-29

    使用数学函数,计算数学问题。 这小节主要学习了调用数学函数,使用数学函数进行计算。 源代码: #include "...

  • SQL-数学函数

    数学函数也是比较常用的函数,在SQL Server中提供了20多个用于处理整数与浮点值的数学函数。数学函数能够对数...

  • 三. PHP与MySQL关系大揭秘

    PHP内置MySQL函数学习(1) PHP内置MySQL函数学习(2) PHP内置MySQL函数学习(2)

  • Excel常用函数

    Excel常用的函数类型有:数学函数,日期函数文本函数,统计函数,查找与引用,逻辑等 数学函数: ABS:求绝对值...

  • 深入浅出MySQL(七)

    MySQL函数操作 数学函数的使用 常用的数学函数见表所示: 因为数学函数操作比较简单,这里面就不给予演示了。 字...

  • MySQL基本使用

    函数 常用函数 数学函数 字符串函数 日期函数

  • 数学函数 ~ mysql函数之一

    1. 基本概念 数学函数主要用来处理数值函数,主要的数学函数有绝对值函数,三角函数(正弦函数,余弦函数,正切函数,...

  • 数学函数

    数学运算函数 double MathAbs( double 输入值)返回数字的绝对值 :: 输入参数value ...

网友评论

    本文标题:看起来不必要的数学函数

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