Namespaces
Variants

std::log10(std::valarray)

来自 zh.cppreference.net
 
 
 
 
定义于头文件 <valarray>
template< class T >
valarray<T> log10( const valarray<T>& va );

对于 va 中的每个元素,计算其值的常用对数(以 10 为底)。

参数

va - 要应用操作的值数组

返回值

包含 va 中数值的常用对数的值数组。

注解

使用非限定函数(log10)执行计算。如果该函数不可用,则因std::log10实参依赖查找而使用。

该函数的返回类型可以不同于std::valarray。此时,替代类型具有以下属性:

  • 提供const的所有std::valarray成员函数。
  • std::valarray、std::slice_array、std::gslice_array、std::mask_array及std::indirect_array可由替代类型构造。
  • 对于每个接受const std::valarray<T>& 参数的函数(begin()和end()(C++11起)除外),应添加接受替代类型的相同函数;
  • 对于每个接受两个const std::valarray<T>&参数的函数,应添加接受const std::valarray<T>&与替代类型所有组合的相同函数。
  • 返回类型不增加超过最深层嵌套参数类型两层以上的模板嵌套层级。

可能的实现

template<class T>
valarray<T> log10(const valarray<T>& va)
{
    valarray<T> other = va;
    for (T& i : other)
        i = log10(i);
    return other; // 可能返回代理对象
}

示例

#include <cmath>
#include <iomanip>
#include <iostream>
#include <valarray>
void show(char const* title, const std::valarray<float>& va)
{
    std::cout << title << " : " << std::right;
    for (float x : va)
        std::cout << std::setw(6) << x;
    std::cout << '\n';
}
int main()
{
    const std::valarray<float> n{-2.f, -1.f, 0.f, 1.f, 2.f, 3.f, INFINITY};
    const std::valarray<float> pow10{std::pow(10.f, n)};
    const std::valarray<float> log10_pow10{std::log10(pow10)};
    show("n      ", n);
    show("10ⁿ    ", pow10);
    show("lg(10ⁿ)", log10_pow10);
}

输出:

n       :     -2    -1     0     1     2     3   inf
10ⁿ     :   0.01   0.1     1    10   100  1000   inf
lg(10ⁿ) :     -2    -1     0     1     2     3   inf

参见

对 valarray 的每个元素应用函数 std::log
(函数模板)
(C++11) (C++11)
计算常用(以 10 为底)对数( log 10 (x) )
(函数)
沿负实轴分割的复常用对数
(函数模板)