Skip to content
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

Way of converting between types of different overflow tags #850

Open
hbe72 opened this issue Apr 8, 2021 · 1 comment
Open

Way of converting between types of different overflow tags #850

hbe72 opened this issue Apr 8, 2021 · 1 comment
Labels
question request Request for a new feature to be added to CNL

Comments

@hbe72
Copy link
Collaborator

hbe72 commented Apr 8, 2021

What is your question?

Is there a way of converting between datatypes with different overflow tags.

What are you trying to do?

One may need to change the overflow type within an algorithm.

TEST(math, convert_overflow_type)                                                                                            
{                                                                                                               
    auto a = cnl::static_number<16,-15, cnl::neg_inf_rounding_tag, cnl::native_overflow_tag, int16_t>{0.5};     
    auto b = cnl::static_number<16,-15, cnl::neg_inf_rounding_tag, cnl::native_overflow_tag, int16_t>{0.5};     
    auto c = a + b;                                                                                             
    cnl::static_number<16,-15, cnl::neg_inf_rounding_tag, cnl::saturated_overflow_tag, int16_t> d = c;          
    std::cout << "d = " << d << std::endl;                                                                      
}  

Going through rep works but is quite cumbersome:

static constexpr auto a = cnl::static_number<16,-15, cnl::neg_inf_rounding_tag, cnl::native_overflow_tag, int16_t>{0.5};
static constexpr cnl::static_number<16,-15, cnl::neg_inf_rounding_tag, cnl::saturated_overflow_tag, int16_t> b = cnl::from_rep<cnl::static_number<16,-15, cnl::neg_inf_rounding_tag, cnl::saturated_overflow_tag, int16_t>, int16_t> {}(static_cast<int16_t>(to_rep(a)));
static constexpr auto expected = cnl::static_number<16,-15, cnl::neg_inf_rounding_tag, cnl::saturated_overflow_tag, int16_t>{0.5};
static_assert(identical(expected, b));
@hbe72 hbe72 added the question label Apr 8, 2021
@johnmcfarlane
Copy link
Owner

There's no such API currently.

I took a look at this yesterday. There is a set_rounding_t alias which does a near-identical thing for rounding_integer. But a quick fix to copy/paste this as set_overflow_t isn't as quite simple as it sounds and merely highlights the fact that there is a more general-purpose solution fighting to get out.

I think that adapting set_tag_t so that it works recursively, is probably the way to go. This will replace set_rounding_t and be used thus:

constexpr auto b{set_tag_t<decltype(a), saturated_overflow_t>{a}};

@johnmcfarlane johnmcfarlane added the request Request for a new feature to be added to CNL label Apr 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question request Request for a new feature to be added to CNL
Projects
None yet
Development

No branches or pull requests

2 participants