1

5th digit after decimal actual output is not same as the 5th digit after decimal of the expected output.

output: 558.29498 expected: 558.29496

double findMaxAverage(int* nums, int numsSize, int k){
float maxSum = -2147483648;
float currentSum = 0;

for(int i=0; i < numsSize; i++){
    currentSum += nums[i];
    if(i >= k - 1){
        if(currentSum > maxSum) maxSum = currentSum;
        currentSum -= nums[i - (k - 1)];
    }
}
return maxSum/k;
}

I tried doing this-

return (double)maxSum/k;

Please help me out with this.

Filburt
  • 17,626
  • 12
  • 64
  • 115
  • 6
    Use `double` all the way instead of using `float` and then casting to `double`. – interjay Jul 04 '23 at 19:00
  • 3
    Alternatively, you could declare `maxSum` and `currentSum` as `long long` and convert to `double` before the division. I'd also separete the `if(currentSum > maxSum) maxSum = currentSum;` from the `if(i >= k - 1)` check. – Bob__ Jul 04 '23 at 19:06
  • @Bob__ In practice you'll probably be able to get away with that. In theory, `int` and `long long` could be the same size. – user3386109 Jul 04 '23 at 19:08
  • 1
    @user3386109 True, if you are thinking of possible overflows, but the OP haven't specified the range of input data (nor if negative values are expected). – Bob__ Jul 04 '23 at 19:10
  • 1
    @ AryantKumar What is the code supposed to do if the array has less than `k` elements in it? – user3386109 Jul 04 '23 at 19:13
  • 1
    Please see [Is floating point math broken?](http://stackoverflow.com/questions/588004/is-floating-point-math-broken) and [Why Are Floating Point Numbers Inaccurate?](https://stackoverflow.com/questions/21895756/why-are-floating-point-numbers-inaccurate) – Weather Vane Jul 04 '23 at 21:24
  • Side note: Instead of `-2147483648`, I suggest that you use [`-FLT_MAX`](https://en.cppreference.com/w/c/types/limits). – Andreas Wenzel Jul 05 '23 at 01:11
  • @AndreasWenzel Another good choice is `-HUGE_VALF` which is -infinity, if `float` supports it, or `-FLT_MAX`. – chux - Reinstate Monica Jul 05 '23 at 04:50
  • 1
    If `k` is large, you'll be losing precision by maintaining `currentSum` orders of magnitude larger than the input values - see Tony Finch's [Incremental calculation of weighted mean and variance](https://fanf2.user.srcf.net/hermes/doc/antiforgery/stats.pdf) for some guidance on numerically-stable techniques for these. – Toby Speight Jul 05 '23 at 07:31

1 Answers1

3

Don't throw away precision by using float and then casting it to double. Start out using double precision floating point numbers.

double findMaxAverage(int* nums, int numsSize, int k) {
    double maxSum = -2147483648;
    double currentSum = 0;

    for (int i = 0; i < numsSize; i++) {
        currentSum += nums[i];

        if (i >= k - 1) {
            if (currentSum > maxSum) 
                maxSum = currentSum;

            currentSum -= nums[i - (k - 1)];
        }
    }

    return maxSum/k;
}
Chris
  • 26,361
  • 5
  • 21
  • 42