简体   繁体   English

设置操作的浮点精度

[英]Set floating point precision for operations

I'm looking for a way to force the computer to calculate a floating-point operation with a set number of significant digits. 我正在寻找一种方法来强制计算机计算具有一定数量的有效数字的浮点运算。 This is for pure learning reasons, so I don't care about the loss of accuracy in the result. 这纯粹是出于学习原因,所以我不在乎结果准确性的下降。

For example, if I have: 例如,如果我有:

float a = 1.67;
float b = 10.0;
float c = 0.01

float d = a * b + c;

And I want every number represented with 3 significant digits, I'd like to see: 我希望每个数字都用3个有效数字表示,我希望看到:

d = 16.7;

Not: 不:

d = 16.71;

So far, I got this as a possible answer: Limit floating point precision? 到目前为止,我得到了一个可能的答案: 限制浮点精度?

But it would bloat my code to turn every floating-point variable into one with the precision I want using that strategy. 但这会使我的代码肿,以我想要使用该策略的精度将每个浮点变量变成一个。 And then doing to the same with the result. 然后对结果进行相同的处理。

Is there an automatic way to fix the precision? 有自动的方法来固定精度吗?

The floating point data types are binary floating points, ie, they have precision in terms of binary digits and it is actually impossible to represent the decimal values exactly in general. 浮点数据类型是二进制浮点,即,它们在二进制数字方面具有精度,实际上通常不可能精确地表示十进制值。 As a result, you will have some problems truncating the operations to the correct number of decimal places in the first place. 如此一来,您会遇到一些将运算符截断为小数位数的问题。 What could work is to format a floating point value after each operation with a precision of n digits (eg with n == 3 ) and convert this back into a floating value. 可行的是在每次操作后以n位精度(例如n == 3 )格式化浮点值,然后将其转换回浮点值。 This won't be particularly efficient but would work. 这不是特别有效,但可以。 To avoid littering the code with the corresponding truncation logic, you would encapsulate the operations you need into a class which does the operation an appropriately truncates the result. 为了避免用相应的截断逻辑乱码,您可以将所需的操作封装到一个类中,该类对该操作进行适当的截断结果。

Alternatively, you could implement the necessary logic using a significand and a suitable base 10 exponent. 另外,您可以使用有效数字和合适的10指数来实现必要的逻辑。 The significant would be restricted to values between -999 and 999. It is probably more work to implement a class like this but the result is likely to be more efficient. 有效位将限制在-999到999之间的值。实现这样的类可能需要更多的工作,但结果可能会更有效。

So far, I got this as a possible answer: Limit floating point precision? 到目前为止,我得到了一个可能的答案: 限制浮点精度?

Read the second answer, which received ten votes, rather than the accepted one, which only received four votes. 阅读第二个答案,该答案获得了十票,而不是被接受的答案,该答案仅获得了四票。 Don't do it. 不要这样

You don't want to do this when you do calculations on paper , let alone on a computer. 在纸上进行计算时,您不想这样做,更不用说在计算机上进行计算了。 Those intermediate calculations are best done to at least one extra significant digit, and preferably two or more, than the underlying data indicate. 这些中间计算最好是对基础数据所指示的至少一个额外的有效数字,最好是两个或更多。 You truncate to the precision indicated by the data at the very end. 您将截断到最后数据所指示的精度。 The only reason we do this on paper is because people aren't that good at dealing with a lot of digits. 我们在纸上进行此操作的唯一原因是因为人们不擅长处理很多数字。 It's a short circuit operation that is tuned to how people calculate (or miscalculate). 这是一种短路操作,可根据人们的计算方式(或计算错误)进行调整。

All that you are doing by rounding intermediate calculations accomplishes is to create an opening for errors to creep in and slowing the computer down, oftentimes by a quite a bit. 通过四舍五入的中间计算所完成的所有工作,是为错误的蔓延开辟了机会,使计算机蠕动并降低计算机的运行速度,通常这要花很多时间。 Don't worry about the extra precision in those intermediate results. 不用担心这些中间结果的额外精度。 Simply use display the results to the desired precision on output. 只需使用显示结果即可达到所需的输出精度。

The opposite problem sometimes does apply. 有时确实存在相反的问题。 You may need to worry about loss of precision in your intermediate results. 您可能需要担心中间结果的精度损失。 Sometimes that loss of precision will mean changing from floats to doubles, or from doubles to variable precision arithmetic (which is slow ). 有时,精度损失意味着从浮点数变为双精度,或者从双精度变为可变精度算术(这很 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM