-
Notifications
You must be signed in to change notification settings - Fork 4.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
Math.Cos() precision #8528
Comments
Ah, the joys of following FCall and Intrinsic method calls/implementations through the CoreCLR source, welcome to the club!! I think it's wired up via this code to the implementation here. Certainly this comment supports that:
However, this might only apply for |
When running using which runtime? The current .NET Core build produces -0.579777545198813 on x86 and -0.579777545194404 on x86.
and VC++ 2017's output value on x86 is -0.579777545194404. It all comes down to whether the generated code is using a "proper" cosine calculation function or the x87 |
Sorry I forgot to mention, I'm using .NET Framework 4.6.2.
Good point, I somehow missed that.
That was indeed my suspicion. Alright so I think the case is closed, I've got answers to all my questions, thanks for the quick replies! |
Note that .NET Framework "insists" in using |
As @mikedn said, netfx insists on using The math functions, in the vast majority of applications, prefer speed over precision and that was the default the If you want/need always more accurate results, you will currently need to write your own implementation. That being said, it might be worth logging a bug to see if a JIT configuration option can be added so users can opt-in-to precise vs fast mode for the Math intrinsics (CRT has this and it seems a reasonable request to have for managed code as well). However, because of the way FCALLs work, I don't think this type of feature will be easy to implement in any case. |
The following C# program computes the cosine of 4203708359 (which can be exactly represented in double precision):
Regardless of the platform target (Any CPU, x86, x64), in both Debug and Release, this program outputs the following value:
Here is the equivalent C program compiled with Visual Studio 2017:
and its output value (x64, Debug or Release):
Finally, here is the value computed by Mathematica to 50 decimals:
Questions:
Math.Cos()
actually implemented? All I could find so far is its declaration in mscorlib.The text was updated successfully, but these errors were encountered: