LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C and C++ Problem with "Floating point exception" (https://www.linuxquestions.org/questions/programming-9/c-and-c-problem-with-floating-point-exception-4175475068/)

PeterUK 08-28-2013 04:04 PM

C and C++ Problem with "Floating point exception"
 
I am having problem with an error "Floating point exception"

I think I understand all of it but I am not sure if I am missing something, I know the code works as a while as I have tested it the benchmark routine before and it works.

From where it came: This is a benchmark to test the number of data coming from the USB port.

What am I trying to do: I am breaking the code and testing the bit which suppose to measure the number of data coming from the USB, I did test it initially in C++ with g++,

and I get "make: *** [run] Floating point exception"

I though was something to do with g++ so I did test it on gcc and I get the same error.

I think I know the reason nbytes/ncalls which is 0, I think It cannot be divided by zero, but I am not sure, maybe ncalls never is 0 as should make n number the call to usb buss? that is my guess.

Now the code:

Code:

#include <stdio.h>
#include <sys/time.h>
//-------
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <ctype.h>
int main()
{
 
size_t nbytes=16777216;
size_t chunk_size=nbytes;
int ncalls=0;
int count=0; 
// Start benchmark:
struct timeval start_tv,end_tv;
gettimeofday(&start_tv,NULL);

 printf ("Test_bench\n");
for (count=0; count<=15000; count++)

// End benchmark:
gettimeofday(&end_tv,NULL);
double seconds3;
seconds3 = (end_tv.tv_sec-start_tv.tv_sec)+ (end_tv.tv_usec-start_tv.tv_usec)/1000000.0;

        printf("Read %zu bytes in %5d msec (chunk size %6zu): "
                        "%6.3f Mb/sec (%5d calls, %6zu bytes/call)\n",
                        nbytes,(int)(seconds3*1000+0.5),
                        chunk_size,nbytes/seconds3/1024/1024,
                        ncalls,nbytes/ncalls);
return (0);
}

So some page I found on the way bechmark

Also there is something I still dont know why I needed to do the code work well if you do "timeval start_tv,end_tv;" but for this test I had the word "struct" as in here to make the compile error go away. Do you know why?

smallpond 08-28-2013 08:45 PM

integer division will never give a floating point exception.
Most likely it is a problem in the division in: nbytes/seconds3/1024/1024

ntubski 08-28-2013 09:10 PM

Quote:

Originally Posted by smallpond (Post 5017885)
integer division will never give a floating point exception.

I thought so too, but after running the program it turns out it does. Apparently there is no integer exception so divide by zero gets lumped under floating point exception even if it comes from an integer operation.

Note: you can run the program under a debugger to see exactly which line causes the crash. You'll want to move the divisions into separate statements: otherwise it just tells you it comes from the printf() line.

Quote:

Originally Posted by PeterUK
Also there is something I still dont know why I needed to do the code work well if you do "timeval start_tv,end_tv;" but for this test I had the word "struct" as in here to make the compile error go away. Do you know why?

In C you have to name structs with struct name, in C++ name suffices.

H_TeXMeX_H 08-29-2013 01:28 AM

I would use assert to make sure denominators are not zero before any division.

PeterUK 08-29-2013 09:09 AM

Quote:

Originally Posted by H_TeXMeX_H (Post 5017979)
I would use assert to make sure denominators are not zero before any division.

I did something like assert(ncalls && "ncalls was zero"); and it works fine, thanks

PeterUK 08-29-2013 09:18 AM

Quote:

Originally Posted by ntubski (Post 5017893)
Note: you can run the program under a debugger to see exactly which line causes the crash. You'll want to move the divisions into separate statements: otherwise it just tells you it comes from the printf() line.

What do you mean here?

I search on internet and I found it gdb so I read a bit and give it a goal and it works nice the output comes as:

Test_bench

Program received signal SIGFPE, Arithmetic exception.
0x08049111 in main () at Buffer.cpp:202
202 ncalls,nbytes/ncalls);

is this one you meant?

Also It said to compile with -ggdb as a flag but I test with and without and it seen to be working. Does this really need it?

ntubski 08-29-2013 04:58 PM

Quote:

Originally Posted by PeterUK (Post 5018260)
Program received signal SIGFPE, Arithmetic exception.
0x08049111 in main () at Buffer.cpp:202
202 ncalls,nbytes/ncalls);

is this one you meant?

Yes, although I get:
Code:

Program received signal SIGFPE, Arithmetic exception.
0x0000000000400647 in main () at bench.c:27
27                printf("Read %zu bytes in %5d msec (chunk size %6zu): "

The beginning of the statement instead of the end. Not sure why.

Quote:

Also It said to compile with -ggdb as a flag but I test with and without and it seen to be working. Does this really need it?
According to the manual:
Quote:

-g
Produce debugging information in the operating system's native format (stabs, COFF, XCOFF, or DWARF 2). GDB can work with this debugging information.

On most systems that use stabs format, -g enables use of extra debugging information that only GDB can use;
...
...
-ggdb
Produce debugging information for use by GDB. This means to use the most expressive format available (DWARF 2, stabs, or the native format if neither of those are supported), including GDB extensions if at all possible.

I would guess there is no difference between the 2 options on Linux systems (ie the native format is gdb's format).

dwhitney67 08-29-2013 06:13 PM

Quote:

Originally Posted by PeterUK (Post 5017750)
Code:

...

 printf ("Test_bench\n");
for (count=0; count<=15000; count++)

// End benchmark:
gettimeofday(&end_tv,NULL);
...
}


You do realize that you are executing gettimeofday() with each iteration of the for-loop, right?

If that was not your intent, then I would suggest that you insert a semi-colon at the end of the for-loop statement.

Also, as others have already deduced, division with a denominator of zero will produce the floating point exception.
Code:

int main(void)
{
    int denominator = 0;
    return 0/denominator;
}


PeterUK 08-30-2013 08:52 AM

Quote:

Originally Posted by ntubski (Post 5018523)
Yes, although I get:
Code:

Program received signal SIGFPE, Arithmetic exception.
0x0000000000400647 in main () at bench.c:27
27                printf("Read %zu bytes in %5d msec (chunk size %6zu): "

The beginning of the statement instead of the end. Not sure why.
.

I tried both and in all i get the same, the end bit of the statements, What steps did you do?

PeterUK 08-30-2013 08:59 AM

Quote:

Originally Posted by dwhitney67 (Post 5018562)
You do realize that you are executing gettimeofday() with each iteration of the for-loop, right?

If that was not your intent, then I would suggest that you insert a semi-colon at the end of the for-loop statement.

Thanks for that comments! I am going to be honest and say I missed the semi-colon :-/, I just want to make something that take some time but I just wanted like a none operation but if I leave as {} it take early nothing or the compile optimize and remove the loop? If I add {printf ("T");} it takes 3msec.

ntubski 08-30-2013 12:21 PM

Quote:

Originally Posted by PeterUK (Post 5018887)
I tried both and in all i get the same, the end bit of the statements, What steps did you do?

Put the code from your first post into bench.c, then ran
Code:

gcc -g -o bench bench.c
gdb -ex run bench
# same result for
gcc -ggdb -o bench bench.c
gdb -ex run bench

I suppose the difference of beginning vs end of statement depends on the gdb/gcc versions.

Code:

gcc (Debian 4.7.3-4) 4.7.3
GNU gdb (GDB) 7.6 (Debian 7.6-5)



All times are GMT -5. The time now is 01:20 PM.