Skip to content

mpc_complex_backend : Basic math operators + - * / are too greedy for real types. #363

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

Closed
cosurgi opened this issue Aug 30, 2021 · 3 comments · Fixed by #365
Closed

mpc_complex_backend : Basic math operators + - * / are too greedy for real types. #363

cosurgi opened this issue Aug 30, 2021 · 3 comments · Fixed by #365

Comments

@cosurgi
Copy link

cosurgi commented Aug 30, 2021

Observe following example code where I lose 400 digits of precision without even a single warning from compiler:

#include <iostream>
#include <boost/core/demangle.hpp>
#include <boost/multiprecision/mpc.hpp>
#include <boost/multiprecision/mpfr.hpp>

template<typename T>
void print_name(T) {
	std::cout << boost::core::demangle(typeid(T).name()) << std::endl;
}

int main() {
	boost::multiprecision::number<boost::multiprecision::backends::mpfr_float_backend<500u>,  boost::multiprecision::et_off> a(1);
	boost::multiprecision::number<boost::multiprecision::backends::mpc_complex_backend<100u>, boost::multiprecision::et_off> b(1);

	// ERROR: 400 digits of precision are lost without even a warning from the compiler:
	auto c1 = a + b;
	auto c2 = b + a;

	auto c3 = a - b;
	auto c4 = b - a;

	auto c5 = a * b;
	auto c6 = b * a;

	auto c7 = a / b;
	auto c8 = b / a;

	print_name(c1);
	print_name(c2);
	print_name(c3);
	print_name(c4);
	print_name(c5);
	print_name(c6);
	print_name(c7);
	print_name(c8);
}

I suppose that such conversion could be allowed, but only explicitly when I really want to do it. Not by mistake like in such an arithmetic operation.

I encountered this problem when implementing a helper for upconverting precisions [1,2] which provides these four basic operations. My helper operators would create in example above an mpc_complex_500 type here. But they cannot, because the operators + - * / are already provided for this combination of types.

  1. https://gitlab.com/yade-dev/trunk/-/blob/improveComplex/lib/high-precision/UpconversionOfBasicOperatorsHP.hpp

  2. https://gitlab.com/yade-dev/trunk/-/blob/master/lib/high-precision/UpconversionOfBasicOperatorsHP.hpp - when 1. gets merged see 2.

@cosurgi
Copy link
Author

cosurgi commented Aug 30, 2021

BTW: it feels similar to boostorg/math#350

@jzmaddock
Copy link
Collaborator

That's a bug - all the machinery is in place to ensure that lossy conversions are always explicit, but somehow that one has escaped!

I'll look into it.

@cosurgi
Copy link
Author

cosurgi commented Aug 31, 2021

Thank you! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants