Boost logo

Boost :

Subject: [boost] Safe Float design question
From: Damian Vicino (damian_at_[hidden])
Date: 2018-07-17 02:18:24

I looking for some design advice.

The context:
- I'm revisiting SafeFloat after some time and, while doing so, I'm
rethinking all the decisions from the past. Idea is to sent for review in
the following months.
- The goal of the library is being a drop-in replacement for float, that
adds checks to floating point operations and reports when a check failed.

Without SafeFloat, someone could write something like this:

#include <iostream>
#include <limits>
#include <cfenv>

using namespace std;

int main(){
    float a = 1.0f;
    float b = numeric_limits<float>::max();
    float c = a/b;
    if(fetestexcept(FE_UNDERFLOW)) { cout << "underflow result\n"; }

What I expect when using safe_float is to declare upfront "what checks" I
care about, "what operations" to check, and "what to do when a check fails".

Some intention examples:
- I would like to check there was no "overflow_to_infinite" in "addition
operations", otherwise I "throw exception"..
- I would like to check there was no "division by zero" in "division",
otherwise I will "log to cerr and ignore it".
- I would like to check there was no "inexact" "addition", otherwise I
return an boost::unexpected.

I have at least 5 things to check (there is 5 flags in c++11::fenv). I have
at least 4 places to check (+/-/*//) and I want to keep what to do about it

In addition, sometimes I want to check multiple things "overflow and
underflow", etc...

So, the question is how the user can pass all that information to the type
and it doesn't look as horrible nonsense.

My original option was:
int main(){
using CHECK = compose_policy<check_addition_overflow,
check_division_by_zero, check_division_underflow>::type;
using REPORT = report_throw_on_failure;
using sf = safe_float<float, CHECK, REPORT>;

   sf a = 1.0_sf;
   sf b = numeric_limits<sf>::max();
   auto c = a/b;
} catch (safe_float_exception e){
   cout << e.message(); //this outputs there was a underflow


Please comment in what you think about this way to use. Is there a better
way to specify the policies to apply that I should try?

Best regards,

I expect when using safe float to write some code like this:

int main(){



Boost list run by bdawes at, gregod at, cpdaniel at, john at