Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8998319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:00:36+00:00 2026-06-16T00:00:36+00:00

I have a function that generates some large numbers, puts them in a vector,

  • 0

I have a function that generates some large numbers, puts them in a vector, sums them, an returns the sum. I have been using std::accumulate to calculate the sum, however, after some testing, I realized it was not returning the expected sum.

typedef unsigned long long ull;
ull sum(ull kLimit)
{
    ull testSum = 0;
    vector<ull> numbers;
    for (ull n = 0; n < kLimit; ++n) {
        if (/* number I want */) {
            numbers.push_back(n);
            // directly sum for testing
            testSum += n;
        }
    }
    ull sum = accumulate(begin(numbers), end(numbers), 0);
    return sum;
}

I would expect the value of sum and testSum to be equal. However, sum is equal to 470064632 and test sum is equal to 82074443256, which is the expected value.

I tried reducing the limit to a much smaller number (500), and the values of sum and testSum were equal. This makes me think the error is overflow with accumulate, I’m not sure what the problem could be though. I am compiling with VS2012 for a x64 platform.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:00:37+00:00Added an answer on June 16, 2026 at 12:00 am

    It does look like an overflow error: the numbers are 0x131C049DF8 and 0x1C049DF8.

    Try casting your final parameter 0 to type ull, because the type returned by accumulate is the type of that final parameter:

    T accumulate(InputIt first, InputIt last, T value) { ... }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function that generates normal random number matrix having normal distribution using
I have some issues with C++ implementation of a function that generates random integers
I have a function that generates a key of 4 characters that has to
I'll start off with a solid example: I have a function that generates hashes
I have a function that creates a 2D vector void generate(int n) { vector<
Suppose I have a function that returns a closure: sub generator { my $resource
I have a function that returns two values in a list. Both values need
I have a program that generates some standard SQL like SELECT * FROM TEST
I am trying to create a function that generates an rectangle with some text
I am interested in a function rand(x, y, seed) that returns (pseudo) random numbers

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.