Floating Point exception

Pages: 12
This program accepts as an input a txt file which I explain below and tries to find the lcm. everytime it excludes an i and tries to find the minimum lcm, for every i I exclude. I exclude an i each time and needs to find the minimum lcm.
Here is a c program I have to run using a terminal in linux. It takes as an input a txt file. for example if I name the c file example.c, i use the command
./example txt_file.txt to run it. the txt file has in the first line the number of integers the txt file is going to have. After the first line I list all the integers. If I create for example a txt file
10
1 2 3 4 5 6 7 8 9 10 or something familiar (with the command cat in linux) it runs fine. However, with a testcase of 50000 numbers in total
50000
744 3443 ..... 938 for example, when I run it I get an error.
Floating Point Exception. I tried everything for a couple of days and still cant find why this happends. I reckon no divison with 0 happends. I use int64_t cause I have to deal with very large numbers. Unfortunately, I cant include the txt file with 50000 numbers here. Can you please check Why this error happens? I tried everything and I cant get it on my own.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
    
    int64_t lcm, d=0, min;
    min=findlcm(array, N, i);// int64_t min=pow(10,18);
    while(i<N){
         lcm=findlcm(array, N, i);
        if(lcm<min){
           min=lcm;
           d=i+1;
           }
        i++;
    }

printf("% " PRId64  "% " PRId64 "\n", min, d);//}
fclose(fin);
free(array);
return 0;
}
Last edited on
You shouldn't use variable length arrays especially for large arrays.

Better change line 42:
int64_t *array = new int64_t[N]


Line 44 is wrong since k will be out of bounds at the end.
Change to: for(k=0; k<N; k++)

There are more out of bounds problem (like line 19). The index within an array must be always smaller than the array size.
I start my array from 1 not from zero. This is why I have for example i<=N. this isnt a problem, is it?

I will change the array as you cited. VLA array is maybe responsible for this error?

I am a beginner, excuse me.when I change the line as suggested I get an errror.
Last edited on
The first element is always at index 0.
If array has N elements it means that array[k] will be out of bounds when k=N.
this isnt a problem, is it?
Yes that's the problem. With fscanf(...) you write to memory that is not yours (when k==N). This causes undefined behavior.

VLA array is maybe responsible for this error?
This depends on the stack size. The more memory the more likely it is.
I changed it as you told me , not tha vla part though, and i still have this error
You seem to be under the misapprehension that C array indices start at 1 and go to the SIZE of the array, but they actually start at 0 and go to SIZE-1.

So this is a problem
1
2
    int64_t array[N];
    for (k = 1; k <= N; k++)

C array indices start at 0 and go up to N-1.
 
    for (k = 0; k < N; k++)


You're basically doing the same thing in findlcm. You pass N in as n and then start the second loop at n, but the highest index is n - 1. And don't forget to start at 0!

Get rid of the VLA. malloc the array instead so it is allocated on the heap instead of the stack (which is much more limited). Don't use new! This is obviously a C program so you need to use malloc. You should also free() the array at the end of the program.

inttypes.h includes stdint.h, so you only need to include inttypes.h.

You should use SCNd64 in the scanf (not that it will make a difference, but it's the proper thing to do).
Last edited on
Is is possible to tell me how to write the malloc for this program.
I am new and kind of ignorant.
I did everything that you told me and still have the same error
Can you show us the current status of your code?
I am unable to replicate the error with random numbers in place of your file.
The code runs fine and generates output etc.

Conclusion: there is data in your file that makes it crash sometimes.
Solution: print the current data on the screen with a flush until it crashes. Then debug LCM in a stub program using that input.

Ι did. I updated my post
i know it runs fine for small input.
for big input 50000 i am unable to find the error
@maryt,
Do you have any idea how large the lcm of 50000 numbers can be?
If I take just 3, 5, 7, 11 the lowest common multiple is already 1155. Do you really have large enough integers?
I totally agree, but that would not trigger a FP exception. Integer overflow, yes, but FP? And overflow is default a warning, I think, and usually runs happily to give the wrong answer...

However, with a testcase of 50000 numbers in total

Just how big do you expect that number to be? The LCM of the numbers from 1 to 43 is too big to fit in a 64 bit integer. The LCM of the numbers 1-300 is 431 digits long.

What is the actual problem you're trying to solve? There is probably an easier way.
To get a more informative error message you might want to compile your program with -fsanitize=undefined
Last edited on
Do I get this error because my algorithm isnt the best afterall?
I tried to divide the findlcm into 3 smaller functions and i still get the same error.

Lets say I have 10 integers
1 2 3 4 5 6 7 8 9 10 which i store in an array. i traverse the array both ways (because i want my program to be fast otherwise it runs extremely slow for big numbers). Each time for some i i exclude an element. lets say if i exclude 7 i get the minimum lcm than any other number i exclude

If i send u via mail the testcase with 50000 numbers can u find the bug for me?
If i send u via mail the testcase with 50000 numbers can u find the bug for me?
Read my previous post. I found the bug for you.
@dhayden
i think the problem is that my algorithm is unefficient for 50000 input
.for 1 to 43 it is to big but can be calculated perfectly same for 100.

So i need to find a better algorithm. I assume i need again both 2 traversal for the matrix of elements (one from the first and one from the last element ----> 2 loops) and maybe store the lcm values in another element to find the minimum lcm.
Pages: 12