Arithmetic exception in C

624 views Asked by At

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.

2

There are 2 answers

0
Amin Khorsandi On BEST ANSWER

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.

6
Amin Khorsandi On
#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.