-
Notifications
You must be signed in to change notification settings - Fork 31
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
Optimize unsigned integer sqrt #11
Comments
New insight has been gained in the area of Karatsuba square root. This is currently under investigation in another project and should be applicable to this issue eventually. |
Karatsuby square root is being investigated more deeply in Boost.Multiprecision |
Assuming that it is a bug to pass a negative value to a |
In wide_integer, this has, indeed, become an open issue with recent supportof signed wide integers. At the moment, I decised to simply return 0 for square roots having argument of 0 or less than 0. This line is shown here. |
I would think twice about using this function because of that part of the contract. This raises three problems:
It's a design philosophy choice and not universally accepted -- or even consistently the right choice! But CNL identifies this kind of condition as a bug -- just like divide-by-zero. And bugs are treated with zero tolerance in CNL. |
Fair enough. It's always a toss-up, provide the wrong answer or throw. But wide-integer is designed to be on-the-metal, no-rtti, exceptions off. I wonder if there is a nice way to handle with these constraints. I do not have the generalized concept of exception, but that would be one option, with special cases for rtti/noexcept. Ideas? |
Firstly, it's important distinguish between errors and bugs.
Both of these are types of contract violation. The former is of the contract between the program user and the program author. The latter is of the contract between an API implementer and an API user and it is that contract which concerns us here. There is no single strategy for dealing with the discovery of a bug in a running program. In C++, the basic choices are:
These choices are mutually exclusive. You might want to pick-and-mix for different types of errors within different areas of the program... but you probably don't. And you cannot easily let the user make the choice at run-time without violating 2) for some users. The choice must be made at build time. The right choice depends on who is using the program and for what purpose:
Because of the plethora of scenarios and choices, I don't think it is wise to impose a single choice on the user. But in CNL I do provide the following:
My goodness, all of this makes me wonder if I shouldn't write a library just for contract checking! I'm currently involved in trying to standardise some of this so that it will be much easier to enact in other libraries. But the most important first step is to provide some kind of HTH and thanks for asking the question. I have been planning to write an article and maybe even present a talk on this subject and this has really helped gather my thoughts. |
It really did. Thank you @johnmcfarlane
That would be terrific in my opinion. I'm pretty honed into the areas of error-intolerant, performance-critical, no backup if you'd ever like to exchange ideas on those top-level requirements. |
Ah, that gets me thinking of numerical errors which are a special case of errors because they are incorrect by degree. I shall make sure to call those out and invite you to review! |
I'd like that. Thx. |
The first version of the article is available here. Please let me know what you think and whether it's helpful. |
Try to optimize unsigned integer square root, for instance, with the algorithm
SqrtRem
known from the MPFR author's literature.The text was updated successfully, but these errors were encountered: