C# - 可以从文本输入中恢复双精度吗?
C# - Possible to restore double precision from a text input?
我有一个 class 可以根据票据上的高度计算长度。它已经存在多年并且运行良好......直到我们获得了独特的门票尺寸。
它们由销售人员以英寸为单位输入,通常是很好的数字,例如 3、4 或 3.5,并存储在数据库中 - 然而,这个是 3.66666 循环(或 11/3)但它输入为 3.666 和由于精度丢失导致计算失败。
我想到了一些技巧来恢复某些数字的精度,但我想也许有人知道更好的方法让 3.666 或 93.1333 恢复到它的数字 + 三分之二状态?
谢谢,
米克
很难得到 double 的准确值,因为 double is floating point.
MSDN 说:
Remember that a floating-point number
can only approximate a decimal number,
and that the precision of a
floating-point number determines how
accurately that number approximates a
decimal number. By default, a Double
value contains 15 decimal digits of
precision, although a maximum of 17
digits is maintained internally. The
precision of a floating-point number
has several consequences:
Two floating-point numbers that appear equal for a particular
precision might not compare equal
because their least significant digits
are different.
A mathematical or comparison operation that uses a floating-point
number might not yield the same result
if a decimal number is used because
the floating-point number might not
exactly approximate the decimal
number.
正如您在评论中所解释的那样,我现在明白您的意思了。我检查了数字:
168000 / 3.666 = 45826.5139
168000 / 3.666666 = 45818.1901488
168000 * 3 / 11 = 45818.1818182
相差8张票。我觉得您的问题可以通过多种方式解决。例如,在用户输入方面。或者在数据库方面。但回到你的问题:
How do I convert 3.666 or a 93.1333 back to it's number + two thirds
status?
您正在寻找将小数(或双精度)转换为分数的方法。
SO 上已经有一个问题:Algorithm for simplifying decimal to fractions 有很多答案。我测试了其中的一些,其中 none 令人满意。其中一些甚至不会复发。可能我没说对,你自己看
无论如何,我相信您不需要完全实现从 1.666 到 3/2 的转换,因为这并不容易,而且您有真实世界的尺寸。你说过,大多数时候数字都是 3、3.5、4 等。所以我建议你看看我上面链接的问题并搜索检测重复次数的算法。这里也讨论了 How to know the repeating decimal in a fraction?
在将 1.666 转换为 1.666666 之后,因为 1/1000000 英寸不会扰乱您的计算,如上数字所示。
我有一个 class 可以根据票据上的高度计算长度。它已经存在多年并且运行良好......直到我们获得了独特的门票尺寸。
它们由销售人员以英寸为单位输入,通常是很好的数字,例如 3、4 或 3.5,并存储在数据库中 - 然而,这个是 3.66666 循环(或 11/3)但它输入为 3.666 和由于精度丢失导致计算失败。
我想到了一些技巧来恢复某些数字的精度,但我想也许有人知道更好的方法让 3.666 或 93.1333 恢复到它的数字 + 三分之二状态?
谢谢, 米克
很难得到 double 的准确值,因为 double is floating point.
MSDN 说:
Remember that a floating-point number can only approximate a decimal number, and that the precision of a floating-point number determines how accurately that number approximates a decimal number. By default, a Double value contains 15 decimal digits of precision, although a maximum of 17 digits is maintained internally. The precision of a floating-point number has several consequences:
Two floating-point numbers that appear equal for a particular precision might not compare equal because their least significant digits are different.
A mathematical or comparison operation that uses a floating-point number might not yield the same result if a decimal number is used because the floating-point number might not exactly approximate the decimal number.
正如您在评论中所解释的那样,我现在明白您的意思了。我检查了数字:
168000 / 3.666 = 45826.5139
168000 / 3.666666 = 45818.1901488
168000 * 3 / 11 = 45818.1818182
相差8张票。我觉得您的问题可以通过多种方式解决。例如,在用户输入方面。或者在数据库方面。但回到你的问题:
How do I convert 3.666 or a 93.1333 back to it's number + two thirds status?
您正在寻找将小数(或双精度)转换为分数的方法。 SO 上已经有一个问题:Algorithm for simplifying decimal to fractions 有很多答案。我测试了其中的一些,其中 none 令人满意。其中一些甚至不会复发。可能我没说对,你自己看
无论如何,我相信您不需要完全实现从 1.666 到 3/2 的转换,因为这并不容易,而且您有真实世界的尺寸。你说过,大多数时候数字都是 3、3.5、4 等。所以我建议你看看我上面链接的问题并搜索检测重复次数的算法。这里也讨论了 How to know the repeating decimal in a fraction? 在将 1.666 转换为 1.666666 之后,因为 1/1000000 英寸不会扰乱您的计算,如上数字所示。