isocpp / isocpp/CppCoreGuidelines
Guideline for dividing two named integers
Nobody has claimed this yet.
- Dominant language
- CSS
- Stars
- 45.3k
- Forks
- 5.6k
- PR merge metrics
- No merged PRs in 30d
Description
What is the recommended way of avoiding truncation of the decimal part of the result in the following example?
int a = 3;
int b = 2;
double c = a / b; // c == 1
Is any of those:
double c1 = static_cast<double>(a) / b;
double c2 = double(a) / b;
double c3 = (a + 0.0) / b;
preferred over the others ?
Having the official recommendation would be useful, as there are still many suggestions to use the C-style casting, i.e.: c=(double)a/b; available.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
The issue body is the only entry point provided and names no file or test. Start by locating existing guidance on integer division and casts, then compare the listed C++ alternatives with the project's current recommendations. Done means recording an agreed recommendation and updating the appropriate guideline with supporting examples.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- documentation
- Issue type
- Documentation
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100