Skip to content

bug in boost::math::statistics::mode (or its documentation) #1419

Description

Docs of boost::math::statistics::mode::mode() suggests that data will be sorted if random access iterators are passed:

Nota bene: The input data must be sorted in order to pass a forward iterator. If data is not sorted random access iterators are required for a call to std::sort.

Both examples in the docs have non-sorted data, but with modes adjacent to each other ( {1, 3, 2, 2, 5, 4}, {2, 2, 3, 1, 5, 4, 4}) .

In any case, for std::vector<int>{3,2,1,2}, mode returns the entire data as its result (in boost 1.90 on Ubuntu 26.04):

#include <boost/math/statistics/univariate_statistics.hpp>
#include <algorithm>
#include <print>

int main()
{       namespace bms = boost::math::statistics;

        std::vector<int> v = {3,2,1,2};
        auto mode = bms::mode(v);
        
        std::ranges::sort(v);
        auto mode2 = bms::mode(v);
        std::println("{}",mode2); //prints [2]
        std::println("{}",mode);  //prints [3, 2, 1, 2]

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions