Git Product home page Git Product logo

Comments (2)

pratahmesh avatar pratahmesh commented on June 1, 2024

The issue you're facing is due to integer promotion in C++. When you perform operations on values of different types, C++ promotes them to a common type before performing the operation. In your case, the values you're passing to the average function include both positive and negative integers, which leads to integer promotion.

The problem is that the sizeof...(t) part of your code is evaluated as an unsigned integer (size_t), which causes all the values to be promoted to unsigned integers for the division operation. This is why you're getting an unexpected result.

To fix this issue, you can explicitly cast the result of the division to the desired type (e.g., double) to ensure that the division is performed with the correct type. Here's an updated version of your code:

#include <iostream>

template<typename ... T>
auto average(T ... t) {
    return static_cast<double>((t + ...)) / sizeof...(t);
}

int main() {
    std::cout << average(1, 2, 3, 4, 5, -6, -7, -8, -9, -10) << std::endl;
}

By casting the result of (t + ...), you ensure that the division is performed using a floating-point type, which can handle both positive and negative values correctly. This will give you the expected average, including negative values.

from modern-cpp-tutorial.

Delta456 avatar Delta456 commented on June 1, 2024

I have made a PR for fixing this bug. See #268

from modern-cpp-tutorial.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.