0, 0e0, 0.0, -0, +0, 000 对 Perl 来说都意味着同样的事情,为什么?

发布于 2024-10-27 05:46:50 字数 169 浏览 1 评论 0原文

只是让我感到困惑。

相关但不同的问题: Perl 中的“0 but true”是什么意思?

Just puzzling to me.

Related, but different question:
What does “0 but true” mean in Perl?

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

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

发布评论

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

评论(5

生死何惧 2024-11-03 05:46:50

Perl 不区分数字的种类。从非 CS/程序员的角度来看,它们对我来说也意味着同样的事情:零。 (这是 Perl 的基础之一:它试图像人一样工作,而不是像计算机一样工作。“如果它看起来像一只鸭子......”)

所以,如果你将它们用作数字,它们都是相同的东西。如果将它们用作字符串,它们会有所不同。这确实会导致您可能需要强制采用一种解释的情况(“0 但正确”;另请参阅"nancy键入”)。但总的来说,它会自动“做正确的事”。

Perl doesn't distinguish kinds of numbers. Looking at all of those with a non-CS/programmer eye, they all mean the same thing to me as well: zero. (This is one of the foundations of Perl: it tries to work like people, not like computers. "If it looks like a duck....")

So, if you use them as numbers, they're all the same thing. If you use them as strings, they differ. This does lead to situations where you may need to force one interpretation ("0 but true"; see also "nancy typing"). but by and large it "does the right thing" automatically.

捂风挽笑 2024-11-03 05:46:50

我不明白,他们还应该是什么意思?

您可以给出整数、科学数、浮点数、有符号整数和零的八进制表示法。为什么它们应该不同?

I don't understand, what else should they mean?

You give integer, scientific, floating point, signed integers and octal notations of zero. Why should they differ?

虚拟世界 2024-11-03 05:46:50

0==0 众所周知,包括拉里·沃尔在内。

0==0 as everyone, including Larry Wall, knows.

幻想少年梦 2024-11-03 05:46:50

Perl 将每个标量值解释为字符串和(可能)数字。根据 perl 的转换规则,所有这些零的字符串表示形式都可以转换为整数值 0 :

"0", "0.0", "-0", "+0", "000" =>直接字符串到数字转换的最简单情况。
“0e0”=>在数字上下文中,仅转换前导有效数字字符,因此仅使用前导“0”。例如,“1984abcdef2112”在数字上将被解释为 1984。perl

中的“0 but true”意味着像“0e0”这样的字符串将在数字上计算为 0,但在布尔上下文中将为“true”,因为转换为布尔值如下与严格的数字转换不同的规则。

Perl interprets every scalar value as both a string and (potentially) a number. All of those string representations of zero can convert to the integer value 0 , according to perl's conversion rules:

"0", "0.0", "-0", "+0", "000" => Simplest case of straight string to numeric conversion.
"0e0" => In a numeric context, only the leading valid numeric characters are converted, so only the leading "0" is used. For example, "1984abcdef2112" would be interpreted numerically as 1984.

"0 but true" in perl means that a string like "0e0" will evalutate numerically to 0, but in a boolean context will be "true" because the conversion to boolean follows different rules than the strict numeric conversion.

不寐倦长更 2024-11-03 05:46:50

Perl 在上下文中工作。在字符串上下文中,它们都是不同的。在数字上下文中,它们都为零。

print "same string\n" if '0' eq '0.0';
print "same number\n" if 0 == 0.0;

布尔上下文中的 '0 but true' 为 true:

print "boolean context\n" if '0 but true';
print "string context\n" if '0 but true' eq '0';
print "numeric context\n" if '0 but true' == 0;

Perl works in contexts. In string context, they are all different. In numeric context, they are all zero.

print "same string\n" if '0' eq '0.0';
print "same number\n" if 0 == 0.0;

'0 but true' in boolean context is true:

print "boolean context\n" if '0 but true';
print "string context\n" if '0 but true' eq '0';
print "numeric context\n" if '0 but true' == 0;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文