0

My question is, how do I increment an array of doubles without it being 0.98999999? I'm currently doing a question on leetcode and I'm trying to this:

array[*level]++;
printf("This is array %lf", array[*level]);

where *level is a pointer to integer.

I kept trying array[*level]++; or array[*level] += 1.00;, but when I check the current double in the array using print, why does it always return 0.988889999 or something that's not 1.000000?

EDIT: I solved the leetcode question, but I'm still curious about this ^

2nd EDIT: I used a for loop to ensure every single index is exactly a 0 like this:

for (int j = 0; j < *returnSize; j++)
    {
        nodecount[j] = 0;
    }

3rd EDIT

This is what I'm seeing in the leetcode stdout : ACTUAL:

[5.00001, 14.00003,1.00001] 

EXPECTED:

[5.00000, 14.00000,1.00000] 

How I got my answer, basically I used 5.00000 divided by 0.9899999 and 28.00000 divided by 1.9999989. Thus, routing back to the original question of why the divider became 0.9899999 instead of 1.000000

FINAL EDIT: THANKS FOR ANSWERING EVERYONE! Basically it was because did not initialise it EXACTLY 0 before incrementing

  • 2
    If you are using floating-point to work on a leetcode problem and are getting issues because of floating-point rounding, you probably should not be using floating-point arithmetic for the problem. Floating-point arithmetic is largely intended for approximating real arithmetic, such as simulating physics, not for doing the sort of abstract computing that most leetcode problems are. – Eric Postpischil Mar 07 '22 at 13:09
  • 1
    In binary floating-point, it is **impossible** to represent .99, because it is not one of the numbers that is representable in a binary-based floating-point format. Methods for compensating for that are highly dependent on the situation, and there is usually an alternative that does not use floating-point arithmetic. – Eric Postpischil Mar 07 '22 at 13:09
  • Related: [Is floating point math broken?](https://stackoverflow.com/q/588004/3545273) – Serge Ballesta Mar 07 '22 at 13:20
  • I don't think there's enough information in the question yet to answer. Is it related to arrays, or is that just the context in your program? How do you initialize the value that is subsequently 0.9899999, and what operations do you perform on it? Just adding integers will not produce any floating point error unless you use very large integer values. – Paul Hankin Mar 07 '22 at 13:21
  • @EricPostpischil The question requires me to return an array of doubles, hence, i made every single calculation in doubles. My original intention is to have the array in like array[0] = 1.00, array[2] = 2.00, etc etc (used later again for doubles arithmetic) – sasaasasasha Mar 07 '22 at 13:24
  • @4386427 As in the post, i made everything to 0 before incrementing, tested and checked it was 0. – sasaasasasha Mar 07 '22 at 13:26
  • @4386427 oh you're right, for some reason its -0.000002 instead THANKSS – sasaasasasha Mar 07 '22 at 13:43

1 Answers1

1

I kept trying array[*level]++; or array[*level] += 1.00;, but when I check the current double in the array using print, why does it always return 0.988889999 or something that's not 1.000000?

Because the value before the increment was not exactly 0.0. You have not given us enough information to evaluate why that might be.

Assuming IEEE-754 floating-point arithmetic (which is a pretty safe assumption), floating-point additions where the operands and the mathematical result are all exactly representable in the floating point format produce a representation of the exact result.

If you expect the elements of your array always to have integer values, then you probably should use an integer element type. If you chose double for its range of representable values then you may have shot yourself in the foot, because the increased range relative to integer types comes at the cost of limited precision. If you need to represent exact integers larger than unsigned long long int can represent then you need an arbitrary-precision math library such as GMP.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
  • Hello John, I used a for loop to reinitiate the array like array[j] = 0 before I ran this to ensure everything started from 0. I made an edit in the original post to show the for loop used to make every index to 0 – sasaasasasha Mar 07 '22 at 13:21
  • 1
    @sasaasasasha please update the question with a minimal piece of code that encapsulates what you're seeing. Having your actual code scattered around comments doesn't help as much as a having a representative sample in the question. – Paul Hankin Mar 07 '22 at 13:23
  • @PaulHankin I made some edits, hope its clearer! – sasaasasasha Mar 07 '22 at 13:31
  • Hello John , are you referring to the part where I initialised it to 0 instead of 0.00? – sasaasasasha Mar 07 '22 at 13:41