Boost logo

Boost :

Subject: [boost] Boost.Random subtract_with_carry doesn't like m == numeric_limits<result_type>::digits
From: Billy O'Neal (VC LIBS) (bion_at_[hidden])
Date: 2016-05-10 15:43:28

Hello, Boost folks.

I found a bug in subtract_with_carry_engine while fixing a bug in MSVC++'s implementation of the same, where m == numeric_limits<result_type>::digits. Consider the following:

#include <stdlib.h>
#include <stdio.h>

#ifdef USE_BOOST
#include <boost/random/subtract_with_carry.hpp>
using namespace boost::random;
#include <random>
using namespace std;

template <typename Engine> typename Engine::result_type run_10k(Engine engine) {
    for (int i = 0; i < 9999; ++i) {

    return engine();

int main() {
    subtract_with_carry_engine<unsigned long long, 64, 10, 24> ull_swc;
    printf("0x%016llX\n", run_10k(ull_swc));

MSVC++ said: 0xFCF0A4776BA0A6A8
Libstdc++ says: 0x01316AEA3646F686
Libc++ says: 0x01316AEA3646F686
Boost 1.60.0/libc++ + Clang says: 0x69E18848FE1BD36D
Boost 1.60.0/libstdc++ + g++ says: 0x1DD6C263C41EEED0

MSVC++'s bug was that we did the underflow check incorrectly resulting in the carry bit always being set if m == numeric_limits<result_type>::digits. I fixed that and MSVC++ will agree with libstdc++ and libc++ in the next update; but boost's behavior here is still strange.

Hope that helps,

Billy Robert O'Neal III
SDE II - Visual C++ Libraries

Boost list run by bdawes at, gregod at, cpdaniel at, john at