generate_int_float_pair() and uint64_t engines

Open
#45 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
1/5
Estimated time
1-3 hours
Newbie friendliness
48/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
cpp
Domain
tooling

Research direction

Inspect random/detail/int_float_pair.hpp around lines 65-77, especially the assignments to bucket from generate_one_digit(). Reproduce the MSVC 2010 warning if available, then verify that the uint64_t engine path no longer produces the reported conversion warnings while preserving the existing calculations.

Written by the indexing model from the issue text.

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<int>((bucket << m) | u);
At line 77, bucket = static_cast<int>((bucket << (w%m)) | (mask & u));

Dominant language
C++
Stars
42
Forks
76
Avg merge
3d 22h
Merged PRs (30d)
6

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from boostorg/random

All issues in boostorg/random

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.