Why am I getting 'Floating point exception: 8'

前端 未结 1 1640
清酒与你
清酒与你 2020-12-21 16:33

I\'m trying to calculate all the prime numbers from 0 - 100 and I\'m getting a floating point exception, could anyone tell me why? (If it helps I\'m using gcc)



        
相关标签:
1条回答
  • 2020-12-21 17:14

    Well, it's really hard to understand what your code is doing. But still

    for(i=1;i<100;i++)
        for(j=2;j<100;j++)
            if((nums[i] % nums[j]) == 0)
            {
                nums[j] = 0;
            }
    

    After this, many values of nums will be 0.(You can print and check)

    So, Later when you are doing

    for(z=0;z<100;z++)
    {  
      for(k=i;k<100;k++)
       for(l = (k+2);l < 100;l++)
         if((nums[k] % nums[l]) == 0) //Part where division by 0 occurs
           nums[k] = 0;
    }
    

    There will be a division by 0, which is giving the floating point exception

    Edited

    Infact, there will be a floating point exception in the first two for loops only.. When i=2 and j=2, nums[2] will get updated to value 0. Then later when for i=4 and j=2. There will be a division by 0, because num[2] is already 0, thus causing the floating point exception

    0 讨论(0)
提交回复
热议问题