tcl_ precision 的状态如何?

发布于 2024-08-22 04:08:14 字数 412 浏览 11 评论 0原文

我在日常工作中不使用Tcl。然而,我的一位同事偶尔会与一位客户互动,他希望我们的工具的扩展语言更像 Tcl(!)。他提出的一个主题是 Tcl 如何让他通过全局变量 tcl_ precision 设置双精度值存储的精度。

我做了一些网络搜索,我发现的文档似乎表明情况确实如此(而不仅仅是设置打印精度)。然而,看起来 tcl_ precision 的历史似乎是曲折的。我的印象是,它在一两个版本中被完全删除,然后又放回去,但有关于覆盖默认值 0 的警告和啧啧称赞,这实际上意味着 17(手册承诺足以代表任何 IEEE第754章

那么谁能告诉我更多关于 tcl_ precision 实际承诺做什么,以及它对幕后的双打有什么影响?它只是打印数字的全局设置,还是实际上截断存储的数字的精度(这对我来说似乎很危险)?

I don't use Tcl in my daily work. However, I have a colleague who occasionally interacts with a customer who wishes our tool's extension language worked more like Tcl (!). One topic he brought up was how Tcl let him set how much precision was stored in a double, via a global variable, tcl_precision.

I did some web searches, and the documentation I found certainly seems to suggest that this is the case (rather than just setting the print precision). However, it looks as if tcl_precision has had a checkered history. I get the impression that it was removed entirely for a version or two, and then put back, but with warnings and tut-tuts about overriding the default value, 0, which really means 17 (which the manual promises is enough to represent any IEEE 754 double).

So can anyone tell me more about what tcl_precision actually promises to do, and what effect it has on doubles under-the-covers? Is it just a global setting for printing numbers, or does it in fact truncate the precision of numbers as stored (which seems dangerous to me)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

演多会厌 2024-08-29 04:08:14

我认为你的客户的理解不正确。

我相信 tcl_ precision 变量允许您设置 Tcl 在将实数转换为字符串时显示的有效位数,正如您所建议的那样。

默认设置为 6:

expr 1.11111111 + 1.11111111
=> 2.22222

如果将其设置为 10,您将得到:

set tcl_precision 10
expr 1.11111111 + 1.11111111
=> 2.22222222

它不会影响 Tcl 在内部用于表示实数的实际方法,该方法被记录为 C 类型 double,提供大约 15 位十进制数字的精度。

I don't think your customer is correct in his understanding.

I believe that the tcl_precision variable allows you to set the number of significant digits Tcl will display when it converts a real to a string, as you suggest.

The default setting is six:

expr 1.11111111 + 1.11111111
=> 2.22222

If you set it to 10 you would get:

set tcl_precision 10
expr 1.11111111 + 1.11111111
=> 2.22222222

It doesn't affect the actual method Tcl uses to represent the real number internally, which is documented as being a C type double, providing about 15 decimal digits of precision.

苦妄 2024-08-29 04:08:14

在 Tcl 8.5 中,tcl_ precision 全局变量应该(可能)单独保留,因为默认设置使用最小位数来准确表示相关的 IEEE 双精度。这与使用固定位数不同,而是人们通常想要的。如果他们想要其他东西,很可能他们正在谈论格式化输出值;那是您想要使用:

format %6f $doubleValue

或类似的东西来生成他们想要看到的实际格式的值。

In Tcl 8.5, the tcl_precision global variable should (probably) be left well alone, as the default setting uses the minimum number of digits to represent the relevant IEEE double exactly. That's not the same as using a fixed number of digits, but instead does what people usually want. If they want something else, chance is they're talking about formatting the value for output; that's when you want to use:

format %6f $doubleValue

or something like that to produce a value in the actual format they want to see.

著墨染雨君画夕 2024-08-29 04:08:14

其他答案是正确的,tcl_ precision 控制 float ->字符串转换。

然而真正的问题(也是你不应该搞乱 tcl_ precision 的原因)是 TCL 的 EIAS 理念使得这种转换可能不仅仅在显示时完成,即您可以在字符串中构建一个表达式,然后对其进行 expr 或在字符串中构建一个脚本进行评估。单独保留 tcl_precicion 意味着到字符串的转换是可逆的,这在这些情况下显然很重要。

话虽如此,如果您的客户依靠更改 tcl_presicion 来更改计算,他们可能并不真正知道自己在做什么。您可能想看看是否可以找到他们真正想做的事情的更多细节。

The other answers are correct that tcl_precision controls float -> string conversion.

however the real problem (and the reason you shouldn't mess with tcl_precision) is that TCL's EIAS philosphy makes it possible that this conversion isn't just done at display i.e. you might build an expression in a string and then expr it or build a script in a string to eval. leaving tcl_precicion alone means that conversion to string is reversable, which is obviously important in these cases.

Having said that if your customer is relying on changing tcl_presicion to change computation they probably don't really know what they are doing. You may want to see if you can find out more details of what they actually want to do.

千と千尋 2024-08-29 04:08:14

tcl_ precision 有点有趣,因为它听起来有误导性。

它主要影响它如何打印变量,但不影响它内部如何计算它。

tcl 在内部使用 C double 类型,因此无论 tcl_ precision 值如何,都使用相同的 C double 类型完成计算。

因此,如果您这样做,

set x 1.123456

set tcl_preceision 2

它将显示 1.12,但在内部它仍然是最初设置的完整双精度值。

如果你将 tcl_ precision 改回“10”,

它将返回到 1.123456

所以你实际上并没有控制计算精度,只是控制它的显示方式。

The tcl_precision is a bit funny as it sounds misleading.

It mainly affects how it prints the variable but not how it calculates it internally.

Internally tcl uses the C double type so the calculations are done using the same C double types regardeless of the tcl_precision value.

So if you do

set x 1.123456

set tcl_preceision 2

It will display 1.12 but internally it is still the full double value it was originally set.

If you change the tcl_precision back to say "10"

It will go back to 1.123456

So your not actually controlling the calculation precision just how it is displayed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文