std::log10(std::valarray)(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… NAME πŸ’…

ok so bestie, this page is literally about std::log10(std::valarray) β€” that's the whole name, no cap. It lives in the C++ Standard Library (I know, fancy), and it's the std::log10 overload that takes a std::valarray. Iconic behavior only πŸŽ€


✨ Synopsis ✨

First things first, it's defined in header <valarray> πŸ’–

template< class T >
valarray<T> log10( const valarray<T>& va );

So what does it do? For each element in va, it computes the common (base 10) logarithm of the value of the element. Every single element, one by one, no skipping. Periodt. 🫢


🌸 Parameters 🌸

  • va β€” the value array you're applying the operation to. That's it, that's the whole parameter list, she's low maintenance πŸ’…

πŸ¦‹ Return value πŸ¦‹

A value array containing the common logarithms of the values in va. So you hand her a valarray of numbers, she hands you back a valarray of their base-10 logs. Trading cards, basically. ✨


πŸ‘‘ Notes πŸ‘‘

ok this part is the real iykyk section so pay attention bestie πŸ™ˆ

An unqualified function (log10) is used to perform the computation. But if such a function isn't available, std::log10 gets used instead β€” and that's all thanks to argument-dependent lookup. Cute, right? πŸŽ€

Also, the function can be implemented with a return type different from std::valarray. In that case, the replacement type has the following properties (this is the fine print, but it's important fine print, fr fr):

  • All const member functions of std::valarray are provided. βœ…
  • std::valarray, std::slice_array, std::gslice_array, std::mask_array and std::indirect_array can be constructed from the replacement type. βœ…
  • For every function taking a const std::valarray<T>& β€” except begin() and end() (since C++11) β€” identical functions taking the replacement types shall be added. βœ…
  • For every function taking two const std::valarray<T>& arguments, identical functions taking every combination of const std::valarray<T>& and replacement types shall be added. βœ…
  • The return type does not add more than two levels of template nesting over the most deeply-nested argument type. βœ…

That's five properties, and I listed all five, because I would never do you dirty like that πŸ’œ


🧚 Possible implementation 🧚

Here's how she might be built under the hood β€” copy-paste energy, but make it ✨algorithmic✨:

template<class T>
valarray<T> log10(const valarray<T>& va)
{
    valarray<T> other = va;
    for (T& i : other)
        i = log10(i);

    return other; // proxy object may be returned
}

Note that little comment at the end β€” // proxy object may be returned β€” she's mysterious like that. 🌈


πŸ”₯ Example πŸ”₯

The page says // Run this code, and honestly? You should. Here it is in full:

#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);
}

⭐ Output ⭐

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

Look at that symmetry 😭 log base 10 and 10-to-the-power literally undoing each other, it's giving soulmate behavior πŸ’–


πŸ’€ See also πŸ’€

  • log(std::valarray) β€” applies the function std::log to each element of valarray (function template)
  • log10, log10f, log10l (C++11), (C++11) β€” computes common (base 10) logarithm ((\log_{10}{x}), aka log[10](x)) (function)
  • log10(std::complex) β€” complex common logarithm with the branch cuts along the negative real axis (function template)

Three whole entries, all present and accounted for, because I don't gatekeep 🫢


πŸ™ˆ Hidden category πŸ™ˆ

  • Pages with unreviewed LWG DR marker

(She's a little shy, that's why she's hidden 🌸)


ok that's the whole page bestie!! source/version is cppreference.com, 2024.06.10, and the page is std::log10(std::valarray)(3) β€” go forth and log your valarrays, you absolute main character πŸ‘‘πŸ’…βœ¨

πŸ“¦ where this page comes from