1

Background

I have a program which sometimes throws floating point exceptions due (primarily) to divide by zero despite having checks in place for dividing by zero values. This could be related to floating point speculation but I'm not sure.

I have implemented a handler for FPEs using feenableexcept/SIGFPE but this merely stops my program dead, which is better than propagating NaNs throughout the data.

I found this old page which talks about floating point traps, and seems to suggest that such a trap is responsible for the default behavior of returning NaN,Inf or -Inf on dividing by zero. This got me thinking.

Question

Is it possible to somehow have a division by zero always return the value 0.0? (One might wonder about the validity of this but in my case this is desirable)

Details

I am using intel compilers version 13.0.0 (gcc version 4.4.7 compatibility) on linux.

Community
  • 1
  • 1
Dan
  • 12,857
  • 7
  • 40
  • 57
  • _'despite having checks in place for dividing by zero values'_ How these are done exactly, can you show a sample? – πάντα ῥεῖ Feb 14 '14 at 10:48
  • The checks are as follows, `if(r – Dan Feb 14 '14 at 10:50
  • 1
    `SIGFPE` is also signaled for integer division. You should catch the faulting instruction in a debugger and determine what happened. You should also reduce the problem to a [short, self-contained, compilable example](http://sscce.org). – Eric Postpischil Feb 14 '14 at 11:50
  • @EricPostpischil is a SSCCE really applicable for this question? I'm not specifically trying to fix the SIGFPE being thrown rather asking a question about whether something is possible. Good to know it can be thrown on integer arithmetic as well, though it makes the name somewhat misleading ;) – Dan Feb 14 '14 at 12:10
  • Arranging for a value to be supplied upon an exception is called [pre-substitution](http://en.wikipedia.org/wiki/Exception_handling). Pre-substitution of custom values (such as zero for division by zero) is not widely supported very well. If that all you wish to ask, then the answer is that C++ does not fully support it. There may be mechanisms for it in Linux (which I will leave others to answer), and they are generally too slow to be practical if exceptions occur with any frequency. – Eric Postpischil Feb 14 '14 at 12:42
  • However, I recommended an SSCCE because the introductory part of your question shows a lack of control/isolation: It asserts exceptions are occurring in spite of efforts to avoid them. This suggests the exceptions that are occurring are in places you are not aware of, and therefore have not studied and would not know whether pre-substitution is appropriate. – Eric Postpischil Feb 14 '14 at 12:43
  • (Incidentally, pre-substitution is recommended in IEEE 754-2008 8.2, but I have not seen good support for it. At best, it requires providing one’s own exception handler, typically with platform-specific code to interpret a faulting instruction and to alter the program counter to continue after the faulting instruction.) – Eric Postpischil Feb 14 '14 at 14:45
  • Yeah. I have no problem writing a platform specific handler but unfortunately Google is not so forthcoming here, seems weird that something which is recommended is so poorly documented and/or supported. – Dan Feb 14 '14 at 14:52
  • @Dan I edited your question to make it a bit more concise, I feel that it was too verbose and that's why it was flagged as unclear what you're asking. Do you agree with my edit? It's rather substantial, so maybe I edited too much? – sashoalm Apr 05 '14 at 06:39
  • @Dan I removed the linux tag and sigfpe to make the question more generic, it's interesting to see if a solution exists for whatever platform. – sashoalm Apr 05 '14 at 06:54
  • Write your own class, say Float and overload the / operator. Also overload cast for easy casting. – rockoder Apr 05 '14 at 06:56
  • @rockoder That might have performance implications. I think structs and classes have padding and alignment requirements, an array of this Float class might take more memory than an array of ordinary floats. – sashoalm Apr 05 '14 at 07:15
  • @sashoalm I appreciate the edits to make it clearer. Seems to have reinvigorated the question somewhat but I was really specifically asking about pre-substitution so the answers don't really match this. They are possibilities but as you point out would have performance implications. – Dan Apr 07 '14 at 09:33

2 Answers2

2

How about you implement your own function to satisfy your unusual division requirement?

template <typename T> T mydiv(T lhs, T rhs)
{
   return (0 == rhs ? lhs : lhs/rhs);
} 
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • The requirement might not be the norm but calling it 'crazy' is a bit inflammatory. Anyway, thanks for the suggestion but the question was originally asking about a method called 'pre-substituion' to avoid any performance issues. – Dan Apr 07 '14 at 09:37
  • @Dan I used that word that in jocular way not in an ugly way. I'll be glad to fix it. – R Sahu Apr 07 '14 at 20:20
0

Apart from messing up the rules of the universe uses

float b = a == 0 ? 3 : 3 + (5 / a);
Ed Heal
  • 59,252
  • 17
  • 87
  • 127