|
|
Home » Extra libraries, Code snippets, applications etc. » C++ language problems and code snippets » FP exception vs NaN
|
Re: Capture division by zero [message #55275 is a reply to message #55273] |
Tue, 27 October 2020 09:23   |
 |
mirek
Messages: 14256 Registered: November 2005
|
Ultimate Member |
|
|
koldo wrote on Fri, 16 October 2020 09:15I would like to capture floating point errors.
For now in case of error the program execution follows, but you can find in the doubles, things like INF or NAN.
This behaviour is very conservative for my apps, and I'd prefer in some situations to capture these situations stopping the execution.
I have tried it unsuccessfully using:
- signal(SIGFPE, ...)
- _controlfp_s()
- _set_se_translator()
Any help will be acknowledged!
PD. Somebody could say "just check that all data is adequate before using it in division, sqrt, ...". That's true, but difficult and cumbersome in some situations using uncontrolled data sources where sometimes even doing lots of "if" to check the consistency of data, that's not enough.
Ah, that explains it. So you have like 2 weeks of experience with this issue 
I was all hurray 3 years ago into catching all FP exceptions and avoiding all NaNs. Not so sure today....
Just consider this: Historically, FP exceptions were default and always active, divide by zero or negative sqr always stopped the code just like dereferencing NULL. Then IEEE basically replaced this behaviour with NaNs as default. Maybe they knew something? 
If you want a very simple contraexample where NaN is definitely better, look at this:
https://www.codeproject.com/Articles/1074135/Evaluating-expr ession-with-descend-parser-and-Uplu
Mirek
|
|
|
|
|
Re: Capture division by zero [message #55280 is a reply to message #55273] |
Tue, 27 October 2020 11:36   |
 |
koldo
Messages: 3432 Registered: August 2008
|
Senior Veteran |
|
|
Quote:Ah, that explains it. So you have like 2 weeks of experience with this issue After thirty five years programming, I think it's better late than never.
If I'd be an expert in this issue, I wouldn't ask for advice.
Best regards
Iñaki
[Updated on: Tue, 27 October 2020 11:39] Report message to a moderator
|
|
|
|
Re: Capture division by zero [message #55282 is a reply to message #55281] |
Tue, 27 October 2020 11:40   |
 |
mirek
Messages: 14256 Registered: November 2005
|
Ultimate Member |
|
|
koldo wrote on Tue, 27 October 2020 11:38mirek wrote on Tue, 27 October 2020 11:16Yet another example, how are you going to fix this:
double SomeFunction(double x)
{
return ln(x*x*x - 15*x*x + x + 20);
}
Now this function is used throughout your code on 50 places and you have got an exception because the argument is negative.
How are you going to fix it?
This is the main problem I have encounterd with "activate FP exception" approach and I still do not know the correct solution....
Mirek In my code I would put an ASSERT to capture the error in DEBUG. Just like Vector checks if index is negative.
Yeah, so your program now does not work in debug for particular input data. What is the next step?
Mirek
[Updated on: Tue, 27 October 2020 11:41] Report message to a moderator
|
|
|
|
|
|
|
Re: Capture division by zero [message #55299 is a reply to message #55298] |
Tue, 27 October 2020 20:35   |
 |
Klugier
Messages: 1099 Registered: September 2012 Location: Poland, Kraków
|
Senior Contributor |
|
|
Hello,
I slowly stop understanding what this thread is about? I think it is perfectly fine to validate input each time it can be wrong - this is the secure coding principles. So, in example bring by kold when i (-1) "v(2); v[i] = 2;", we should always check and handling appropriate. The same is true for divided by zero problem.
The only problem I have is that the CrashHandler is forced by using SysInfo package on Debug mode. For me the class should not be there in nay cases. If you would like to write such debug code just do it in your application not in the library. This is my postulate. If this code will help you find errors in your application that's fine, but use it only there.
EDIT: I saw that crash handler was removed from SysInfo. Thanks!
Klugier
U++ - one framework to rule them all.
[Updated on: Tue, 27 October 2020 20:43] Report message to a moderator
|
|
|
|
|
Re: Capture division by zero [message #55304 is a reply to message #55301] |
Wed, 28 October 2020 09:24   |
 |
koldo
Messages: 3432 Registered: August 2008
|
Senior Veteran |
|
|
mirek wrote on Tue, 27 October 2020 20:50koldo wrote on Tue, 27 October 2020 19:25
What you say is not conceivable for me. In my world a negative square root is the same as a Vector v(2); v[-1] = 2;
Cool, then please fix example/FnGraph.
Mirek
In this case my old Casio calculator prints an error message.
Edited: Just checked with my daughter's CASIO Graph 35+E. 1/0 prints "Math ERROR. Press EXIT". Really updated, this version even comes with Python.
Best regards
Iñaki
[Updated on: Wed, 28 October 2020 10:10] Report message to a moderator
|
|
|
Re: Capture division by zero [message #55305 is a reply to message #55299] |
Wed, 28 October 2020 09:29   |
 |
koldo
Messages: 3432 Registered: August 2008
|
Senior Veteran |
|
|
Klugier wrote on Tue, 27 October 2020 20:35Hello,
I slowly stop understanding what this thread is about? I think it is perfectly fine to validate input each time it can be wrong - this is the secure coding principles. So, in example bring by kold when i (-1) "v(2); v[i] = 2;", we should always check and handling appropriate. The same is true for divided by zero problem.
The only problem I have is that the CrashHandler is forced by using SysInfo package on Debug mode. For me the class should not be there in nay cases. If you would like to write such debug code just do it in your application not in the library. This is my postulate. If this code will help you find errors in your application that's fine, but use it only there.
EDIT: I saw that crash handler was removed from SysInfo. Thanks!
Klugier
CrashHandler capture of FP errors was activated only in DEBUG mode, like U++ Vector bounds calculation: no overhead added to RELEASE.
U++ Vector bounds check is forced by default in DEBUG. It seemed fair to do the same with other errors.
Best regards
Iñaki
[Updated on: Wed, 28 October 2020 09:30] Report message to a moderator
|
|
|
Re: Capture division by zero [message #55306 is a reply to message #55304] |
Wed, 28 October 2020 13:51   |
 |
mirek
Messages: 14256 Registered: November 2005
|
Ultimate Member |
|
|
koldo wrote on Wed, 28 October 2020 09:24mirek wrote on Tue, 27 October 2020 20:50koldo wrote on Tue, 27 October 2020 19:25
What you say is not conceivable for me. In my world a negative square root is the same as a Vector v(2); v[-1] = 2;
Cool, then please fix example/FnGraph.
Mirek
In this case my old Casio calculator prints an error message.
But so does FnGraph, that is the point (well, actually, it does not print the error, just does not render the graph in areas where the function is undefined). But if you activate FP exception, it will crash on it.
Quote:
Edited: Just checked with my daughter's CASIO Graph 35+E. 1/0 prints "Math ERROR. Press EXIT". Really updated, this version even comes with Python.
Sure, because it gets NaN and interprets correctly as error...
Mirek
|
|
|
Re: Capture division by zero [message #55307 is a reply to message #55306] |
Wed, 28 October 2020 14:04   |
 |
mirek
Messages: 14256 Registered: November 2005
|
Ultimate Member |
|
|
I am also pretty interested what will you do when you get exception in eigen. I am not that advanced in math, but I am pretty sure that e.g. matrix inversion can produce a lot of divide by zero for degenerate matrix and I am also pretty sure that eigen follows NaN model like every other mature library (if for nothing else, then for performance reasons).
Are you going to compute determinant before each call to inversion?
Mirek
[Updated on: Wed, 28 October 2020 14:57] Report message to a moderator
|
|
|
Re: Capture division by zero [message #55316 is a reply to message #55307] |
Thu, 29 October 2020 14:19   |
 |
koldo
Messages: 3432 Registered: August 2008
|
Senior Veteran |
|
|
mirek wrote on Wed, 28 October 2020 14:04I am also pretty interested what will you do when you get exception in eigen. I am not that advanced in math, but I am pretty sure that e.g. matrix inversion can produce a lot of divide by zero for degenerate matrix and I am also pretty sure that eigen follows NaN model like every other mature library (if for nothing else, then for performance reasons).
Are you going to compute determinant before each call to inversion?
Mirek
I am well aware of operations that could be bad conditioned . I use a lot matrix inversion.
In fact STEM4U includes some tools to handle infinite precision numbers (well, but as expected, very inefficiently)
If you ask me if I prefer to search for NaNs after every matrix algebra operation, or catching an exception, I will choose the second.
And remember that U++ is not for people who are content to following STL or BOOST, but is for people who wants more performance and simplicity of use. There are other libraries more compliant but, unfortunately for them, they are worse than U++. And you are the biggest culprit of it .
Best regards
Iñaki
|
|
|
Re: Capture division by zero [message #55357 is a reply to message #55316] |
Mon, 02 November 2020 10:31   |
 |
mirek
Messages: 14256 Registered: November 2005
|
Ultimate Member |
|
|
koldo wrote on Thu, 29 October 2020 14:19mirek wrote on Wed, 28 October 2020 14:04I am also pretty interested what will you do when you get exception in eigen. I am not that advanced in math, but I am pretty sure that e.g. matrix inversion can produce a lot of divide by zero for degenerate matrix and I am also pretty sure that eigen follows NaN model like every other mature library (if for nothing else, then for performance reasons).
Are you going to compute determinant before each call to inversion?
Mirek
I am well aware of operations that could be bad conditioned . I use a lot matrix inversion.
In fact STEM4U includes some tools to handle infinite precision numbers (well, but as expected, very inefficiently)
If you ask me if I prefer to search for NaNs after every matrix algebra operation, or catching an exception, I will choose the second.
Now actually catching the exception as part of program logic in release mode (as oppposed to just stopping on NaN in debug), that is completely different thing! I could agree that would be a good solution, except for one nasty tiny detail: you cannot officially catch FP exceptions in C++ (see e.g. https://stackoverflow.com/questions/38221471/how-to-convert- sigfpe-into-a-c-exception). It might be possible on specific platforms / compilers, but I would think twice before making your code reliant on it
All you can realistically do in the handler is to set some per-thread and/or global "fp-error" flag. That frankly does sound quite similar to NaN...
All that said, activating FP exception might have its uses, e.g. when you are getting NaN out of calculation for unknown reasons and you want to track it down.
I just do not think that it is the only valid way. And in my humble experience, insisting that your code never produces NaNs creates much more mess than it solves.
Mirek
[Updated on: Mon, 02 November 2020 10:45] Report message to a moderator
|
|
|
Goto Forum:
Current Time: Wed Apr 30 00:29:24 CEST 2025
Total time taken to generate the page: 0.01126 seconds
|
|
|