0

Arithmetic Exception

//m*n行列Aを用いてy = A*x +b を計算する
void fc(int m, int n, const float *x, const float *A, const float *b, float *y){
    int i, j;

    for (i = 0; i < m; i++){
        y[i] = b[i];
        for (j = 0; j < n; j++){
            y[i] += A[i * n + j] * x[j];
        }
    }
}

This is a code that does AX+b calculation of matrixes. But as in the photo, an arithmetic exception is occurred. Why is this happening? Even though it is multiplication and there is nothing divided by 0. How can I solve this error?

Sorry that I cannot add the values, or else I will have to add the whole file here. These are the parameters of the Neural Network and I will have to add .dat files here then I will also need other codes that can load those files. Also, I do not know how to bring only numbers from the .dat files, they are kind of weirdly encoded, so.

I will provide all the other information otherwise, so please don't close this question and I really want to know why this happens and how to solve it.

This is also another example of the exception. Example

What I want to know is how can this happen even where there is nothing divided by 0 in this example. How I can interpret this situation.

MBH
  • 11
  • 3
  • 1
    You can most likely inspect the values of all variables when the exception happens and include those to make a [mcve]. Without any information of the values there’s not much we can say. We don’t even know the sizes of the arrays or values of m and n etc. – Sami Kuhmonen Jul 13 '21 at 04:55
  • I tried to print out the values of the exception through print();. But even in that case, an arithmetic exception occurred when printing the value in the array. How can I interpret this? – MBH Jul 13 '21 at 05:03
  • Use the debugger to get the data. It will show you the data, it already shows the first values in the screenshots – Sami Kuhmonen Jul 13 '21 at 05:04
  • I have added the picture so can you take a look at it? However, I am not sure the values in the window actually shows the number that is causing the error. – MBH Jul 13 '21 at 05:10
  • I have added another picture of trying to printing the value when the error occur, but I cannot even print the value. What is happening? – MBH Jul 13 '21 at 05:16
  • In the shown code, your loop changes i (row index) from 0 to m-1, and j (column index) from 0 to n-1, then accessing the element with i*n+j, which looks correct. On the other hand, your calculation in the attached image access the array A in a different way. The index `A[j * m + i]` is incorrect and may exceed the range of the array. – tshiono Jul 13 '21 at 05:48
  • Your two last examples seems completely unrelated to the code posted. – Support Ukraine Jul 13 '21 at 06:46
  • Yeah I think I should review the code all over, problems are happening everywhere,, – MBH Jul 13 '21 at 06:49
  • @MBH The index calculation seems wrong in the last two examples. Also notice that they are **seg fault** which is another thing. Please edit the question and remove those examples. – Support Ukraine Jul 13 '21 at 06:50
  • Put a signal handler on SIGFPE. Then you can see exatly what happens – Support Ukraine Jul 13 '21 at 07:00
  • Sorry for triggering confusion and yes I will add a signal handler. – MBH Jul 13 '21 at 07:21
  • My conclusion is that there parameters from NN is wrong. When I tried out the standard parameter, it came out correctly. – MBH Jul 13 '21 at 07:22
  • Related? https://stackoverflow.com/questions/8180752/how-to-convert-signalling-nan-to-quiet-nan – pmg Jul 13 '21 at 07:57

2 Answers2

0
#include <stdio.h>
void fc(int m, int n, const float *x, const float *A, const float *b, float *y){
    int i, j;

    for (i = 0; i < m; i++){
        y[i] = b[i];
        for (j = 0; j < n; j++){
            y[i] += A[i * n + j] * x[j];
        }
    }
}

int main()
{
    const float x[3]={1,1,1};  
    const float *xp=x;
    const float A[3][3]={{1,1,1},{1,1,1},{1,1,1}};
    const float b[3]={1,1,1};   
    const float *bp=b;
    float y[3]; float *yp=y;
    fc (3,3,xp,*A,bp,yp);
    printf("%f %f %f ",y[0],y[1],y[2]);

    return 0;
}

I've tested the program with imaginary values of 1 for all variables and the matrices size of 3x3 and 3x1. the result was correct with no error. ther result was

4.0000000 4.0000000 4.0000000 

So the problem does not arise from the structure of your code. it is definitely comes from a special arithmetic problem.

  • Is there a way to ignore those kind of arithmetic problem or change the result to 0 in c? – MBH Jul 13 '21 at 04:44
  • ignoing is not best choice, there might be a problem in the computation. I think the problem must be related to overflow of float number. On the other hand, consider floating limits, for esample adding a very large number of float (eg. 999+e50) and a small number (eg. 0.00000001) cannot be done and might result in error in some compilers. – Amin Khorsandi Jul 13 '21 at 04:55
  • I am really desperate for the solution, do you mind if you can look this problem a little deeper? – MBH Jul 13 '21 at 05:01
  • you must find the exact number that error occurs during it's calculation. which number has been multiplied by which number that the error occurs? – Amin Khorsandi Jul 13 '21 at 05:05
  • I have added the picture but I am not sure the values in the window actually shows the number that is causing the error. – MBH Jul 13 '21 at 05:11
  • I have added another picture of trying to printing the value when the error occur, but I cannot even print the value. What is happening? – MBH Jul 13 '21 at 05:16
0

according to you image, your matrices size is 100x50 (m,n) that means 5000 items. but you entered A[j*m+i] where 'j' is equal with 55 and 'i' is equal with 0. that means accessing the 5500 item of array which is not allowed.