boostorg / boostorg/random

generate_int_float_pair() and uint64_t engines

Open
#45 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
42
Forks
76
Avg merge
4d 8h
Merged PRs (30d)
5

Description

When a random engine with a 'result_type' of 'uint64_t' is fed to exponential_distribution::operator()(), MSVC 2010 generates lots of warnings.
The key warning complains that:
warning C4244: '=' : conversion from 'base_unsigned' to 'int', possible loss of data.

Cause of the issue:
In random/detail/int_float_pair.hpp, at line 65, a local variable 'bucket' is defined as an 'int'.
At line 68, 'generate_one_digit()' returns a value of type 'base_unsigned' (actually 'uint64_t').
At line 69, the value joins certain calculation, and the result value of type 'uint64_t' is assigned to 'bucket', which is a 64-bit to 32-bit conversion, and issues the warning.
At line 77, there is the same problem.

Suggestions:
At line 67, there is a 'for' loop. However, if 'base_unsigned' has 64 digits, 'm' is 64, and the loop will never run.
At line 69, it should be safe to cast the result of the calculation into 'int' before it is assigned to 'bucket'.
i.e., `bucket = static_cast((bucket << m) | u);`
At line 77, `bucket = static_cast((bucket << (w%m)) | (mask & u));`

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.