大整数的Math.Cos()的精度 [英] Precision of Math.Cos() for a large integer

查看:128
本文介绍了大整数的Math.Cos()的精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在C#中计算4203708359弧度的余弦值:

I'm trying to compute the cosine of 4203708359 radians in C#:

var x = (double)4203708359;
var c = Math.Cos(x);

(4203708359可以用双精度精确表示.)

(4203708359 can be exactly represented in double precision.)

我要

c = -0.57977754519440394

Windows计算器提供的

Windows' calculator gives

c = -0.579777545198813380788467070278

在Linux上,PHP的cos(double)函数(内部仅使用C标准库中的cos(double))提供:

PHP's cos(double) function (which internally just uses cos(double) from the C standard library) on Linux gives:

c = -0.57977754519881

使用Visual Studio 2017编译的简单C程序中的

C的cos(double)函数给出

C's cos(double) function in a simple C program compiled with Visual Studio 2017 gives

c = -0.57977754519881342

这是C#中Math.cos()的定义:它似乎是一个内置功能.我尚未在C#编译器中进行挖掘(尚未)来检查它可以有效地进行编译,但这可能是下一步.

It appears to be a built-in function. I didn't dig (yet) in the C# compiler to check what this effectively compiles to but this is probably the next step.

与此同时:

为什么在我的C#示例中精度如此之差,我该怎么办?

C#编译器中的余弦实现是否简单地无法处理大整数输入?

Is it simply that the cosine implementation in the C# compiler deals poorly with large integer inputs?

Wolfram Mathematica 11.0:

Edit 1: Wolfram Mathematica 11.0:

In[1] := N[Cos[4203708359], 50]
Out[1] := -0.57977754519881338078846707027800171954257546099993

我确实需要该级别的精度,并且我已经准备好走得很远以获得它.如果存在一个支持余弦的好库,我很乐意使用任意精度库(到目前为止,我的努力还不止于此).

Edit 2: I do need that level precision, and I'm ready to go pretty far in order to obtain it. I'd be happy to use an arbitrary precision library if there exists a good one that supports cosine (my efforts haven't let to one so far).

我将问题发布到coreclr的问题跟踪器上: https://github.com /dotnet/coreclr/issues/12737

Edit 3: I posted the question on coreclr's issue tracker: https://github.com/dotnet/coreclr/issues/12737

推荐答案

大概是这些盐与每个密码一起存储.您可以使用PHP代码计算该余弦,并将其与密码一起存储.然后,我还将添加一个密码版本号,并将所有这些较早的密码默认为版本1.然后,在您的C#代码中,对于任何新密码,您将实现一个新的哈希算法,并将这些密码哈希存储为密码版本2.对于任何版本1的密码都可以进行身份​​验证,而不必计算余弦,只需使用与密码哈希值和盐值一起存储的余弦值即可.

那个PHP代码的程序员可能想做一个聪明的Pepper版本.通过将余弦或胡椒与盐和密码哈希值一起存储,您基本上可以将胡椒变成盐2.因此,执行此操作的另一种无版本方式是在C#哈希代码中使用两种盐.对于新密码,您可以将第二个盐保留为空白或以其他方式分配.对于旧密码,它将是该余弦,但已经计算出了.

Presumably, the salts are stored along with each password. You could use the PHP code to calculate that cosine, and store that also with the password. I would then also add a password version number and default all those older passwords to be version 1. Then, in your C# code, for any new passwords, you implement a new hashing algorithm, and store those password hashes as passwords version 2. For any version 1 passwords, to authenticate, you do not have to calculate the cosine, you simply use the one stored along with the password hash and the salt.

The programmer of that PHP code was probably wanting to do a clever version of pepper. By storing that cosine, or pepper along with the salt and the password hashes, you basically change that pepper into a salt2. So, another versionless way of doing this would be to use two salts in your C# hashing code. For new passwords you could leave the second salt blank or assign it some other way. For old passwords, it would be that cosine, but it is already calculated.

这篇关于大整数的Math.Cos()的精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆