LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-28-2013, 05:04 PM   #1
PeterUK
Member
 
Registered: May 2009
Posts: 277

Rep: Reputation: 16
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?
 
Old 08-28-2013, 09:45 PM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: CentOS 6 (pre-systemd)
Posts: 1,769

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
integer division will never give a floating point exception.
Most likely it is a problem in the division in: nbytes/seconds3/1024/1024
 
Old 08-28-2013, 10:10 PM   #3
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,540

Rep: Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878
Quote:
Originally Posted by smallpond View Post
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.

Last edited by ntubski; 08-28-2013 at 10:11 PM. Reason: add attribution for quote
 
Old 08-29-2013, 02:28 AM   #4
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
I would use assert to make sure denominators are not zero before any division.
 
Old 08-29-2013, 10:09 AM   #5
PeterUK
Member
 
Registered: May 2009
Posts: 277

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by H_TeXMeX_H View Post
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
 
Old 08-29-2013, 10:18 AM   #6
PeterUK
Member
 
Registered: May 2009
Posts: 277

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by ntubski View Post
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?
 
Old 08-29-2013, 05:58 PM   #7
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,540

Rep: Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878
Quote:
Originally Posted by PeterUK View Post
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).
 
Old 08-29-2013, 07:13 PM   #8
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,523

Rep: Reputation: 332Reputation: 332Reputation: 332Reputation: 332
Quote:
Originally Posted by PeterUK View Post
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;
}
 
Old 08-30-2013, 09:52 AM   #9
PeterUK
Member
 
Registered: May 2009
Posts: 277

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by ntubski View Post
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?
 
Old 08-30-2013, 09:59 AM   #10
PeterUK
Member
 
Registered: May 2009
Posts: 277

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by dwhitney67 View Post
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.
 
Old 08-30-2013, 01:21 PM   #11
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,540

Rep: Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878
Quote:
Originally Posted by PeterUK View Post
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)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] NTPD Terminated with "Floating Point Exception" ksprabha Linux - Software 3 05-23-2013 06:43 AM
[SOLVED] How to invoke the calculator "bc" in bash script for floating point arithmetic? bluesmodular Programming 2 05-16-2010 12:58 AM
Yum Upgrade from FC5 "Floating point exception" TheJosh Fedora 0 01-05-2007 11:03 PM
KDE "floating point exception" JRR883 Linux - Desktop 2 12-18-2006 04:45 PM
Floating point exception problem with realplayer jasmeet93 Linux - Software 0 06-30-2005 08:44 AM


All times are GMT -5. The time now is 04:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration