-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Implement <numbers> Math Constants #261
Conversation
I am no expert on the rules for implicit type conversions but for the 1 / sqrt2 assert, could that be an issue? Does the fact you’ve got integer 1, an M_ constant which I assume is double, and whatever sqrt resolves cause any concern? |
Changing it to Is #251 still an issue given that's how the standard defines it https://eel.is/c++draft/concepts.arithmetic? |
I'm torn between wanting hexfloats and wanting shortest round-trip doubles. Probably the latter. Thanks for the test program; it increases my confidence in these changes.
That doesn't seem surprising to me; the sqrt2 constant has been quantized to binary, so inverting it doesn't necessarily produce the same result as inverting the infinitely precise real number and then quantizing that. If anything, it's more surprising when this results in equality.
Those constants are non-Standard, I believe.
Good question; the answer is no. There are three major kinds of identifiers: non-
That's not an issue; please go ahead and define the |
/azp run |
Today the Azure Pipelines bot isn't paying attention to my comments for unknown reasons (it's happening in another PR too). We'll investigate. |
Your docs say
Which doesn't seem to work. Though I guess it should now point to the docs for this header now (or have them on that page), so guess this is moot. I totally forgot about the specialisation aspect, so sorry about that. Not sure about the Also should the non-concepts version be simplified with a macro (or TMP which I don't have the skills for)? |
Regardless of what the docs say, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is looking good; I think after one more revision we'll be able to port this to MSVC-internal git and enable some basic tests. Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! I'm going to port this to our MSVC-internal repo now and begin adding setup/test changes.
I verified that this is by design, caused by the quantized nature of binary floating-point. Here is a full analysis: #define _USE_MATH_DEFINES
#include <cmath>
#include <numbers>
#include <cstdio>
using namespace std;
using namespace std::numbers;
int main() {
printf(" sqrt2: %a %.1000g\n\n", sqrt2, sqrt2);
printf("1.0 / sqrt2: %a %.1000g\n", 1.0 / sqrt2, 1.0 / sqrt2);
puts("Exact inverse of quantized sqrt2: 0.7071067811865474760643777948402871575373580514923651957447...");
puts(" 0x1.6a09e667f3bcc8p-1 midpoint: 0.707106781186547517226159698111587204039096832275390625");
puts(" Exact inverse of exact sqrt(2): 0.7071067811865475244008443621048490392848359376884740365883...");
printf(" M_SQRT1_2: %a %.1000g\n", M_SQRT1_2, M_SQRT1_2);
printf("sqrt2 / 2.0: %a %.1000g\n", sqrt2 / 2.0, sqrt2 / 2.0);
}
Note the difference between |
Thanks again, and congratulations for your second C++20 feature! |
Thanks! Though I don't think moving something you already implemented counts 😛 |
@StephanTLavavej That you used |
Description
Resolves #29. As partially discussed there I just pulled the values from Wolfram Alpha to the same significant figures as
M_PI
and co.Not sure if it was wanted but I provided both a concepts and non-concepts verion. Concepts version depends on #251
I created this test program as requested and it all appears to check out (P.S. I have no idea what I'm doing so I could be wrong, also praise be to fmt)
One thing to note is that
static_assert(1 / sqrt2 == M_SQRT1_2);
fails withSo unlike the paper suggests you probably can't redefine that one in terms of
std::numbers
Sidenote: why does
<cmath>
not work for the constants? Are the docs incorrect there?And if I understand #140 correctly I should add
_STD
forfloating_point
,enable_if_t
, andis_floating_point
?Checklist
Be sure you've read README.md and understand the scope of this repo.
If you're unsure about a box, leave it unchecked. A maintainer will help you.
_Ugly
as perhttps://eel.is/c++draft/lex.name#3.1 or there are no product code changes.
verified by an STL maintainer before automated testing is enabled on GitHub,
leave this unchecked for initial submission).
members, adding virtual functions, changing whether a type is an aggregate
or trivially copyable, etc.).
the C++ Working Draft as a reference (and any other cited standards).
If they were derived from a project that's already listed in NOTICE.txt,
that's fine, but please mention it. If they were derived from any other
project (including Boost and libc++, which are not yet listed in
NOTICE.txt), you must mention it here, so we can determine whether the
license is compatible and what else needs to be done.