-
Notifications
You must be signed in to change notification settings - Fork 83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FeatReq: Overflow Detection #13
Comments
I know some other fixed-point libraries have overflow detection, but one of the concerns I have with this is performance: adding these checks slows down basic fast operations like addition and also (but to less, relatively), multiplication. The performance impact on compound operations like trigonometry would have to be investigated, and those operations already suffer from being "emulated". An argument against overflow detection is that in C++ signed integers and floats also have UB on overflow (although in practice some methods are available to handle fp overflow with exceptions). If I had to choose, I'd add this with a template argument, to let developers choose whether to have an overflow-safe type or not. @Klummel69 , is there a particular use case that you have run into that would require this feature? |
My background is in automation with microcontrollers. Mostly, performance is important, of course. |
Another option is to use
That's what I'm trying to avoid, so if this checking needs to happen in a release build as well, I'd favor it being a template argument, e.g. |
Of course. I have also often modified assert() so that assertions were also caught in the release. Not perfect, but easy to implement.
That would be the ideal solution, of course. |
An architecturally cleaner solution is to remove the template parameter constraint that the using fixed_16_16_checked = fpm::fixed<Saturating<std::int32_t>, std::int64_t, 16>; A simple saturating wrapper over a native integral type is rather trivial to implement but one might say that it would be out of the scope of this library. |
An interesting idea, @pavel-kirienko. This was considered for #8, but I was afraid I couldn't guarantee that the user wouldn't use weird types such as float or classes. As mentioned in that issue, ideally I'd want a integer duck-typing concept, i.e. accept types that look and feel like integers. Maybe that's simply too much and I should trust the programmer. Using |
A generic type is expected to generalize some behavior for any T irrespective of its identity as long as it meets specific requirements; the problem with the current approach is that it enforces irrelevant requirements (such as T being a native integer) which goes against the principles of metaprogramming. Being able to instantiate this template with a custom class is absolutely essential for it to be useful outside of the most basic scenarios. I see a few adequate solutions:
|
I do agree with your argument. The first solution is the one that I was not very happy about. Your second solution looks much nicer. I didn't realize I'll switch to that one instead. Many thanks for pointing this out! |
Unlike floatingspoint, overflow can occur more quickly when fixed-point operations are used. In applications such as filters or control algorithms, overflow detection would be useful.
Example
An optional switchable overflow detection would be good. At least for the basic arithmetic operations + - * /.
In the case of multiplication it might be easy to implement:
Is there any interest in this? Maybe I will deal with it in the near future.
The text was updated successfully, but these errors were encountered: