Pra*_*har 4 floating-point perl decimal
所以我正在写一个 perl 程序来做一些计算,我把一个浮点数作为
$x = 00.05;
如果我打印
print $x * 100.0;
它返回 500.0
但如果我这样做
$x = 0.05;
print $x * 100.0;
它正确打印 5.0;
这是什么行为?有没有我必须遵守的约定,我失踪了?
Håk*_*and 10
前导零表示八进制常数,所以当你这样做时
my $x = 00.05;
Run Code Online (Sandbox Code Playgroud)
您实际上是将两个八进制数进行字符串连接:
my $x = 00 . 05; # The same as "0" . "5"
Run Code Online (Sandbox Code Playgroud)
它给你字符串"05",然后你做
print $x * 100.0; # prints 500
Run Code Online (Sandbox Code Playgroud)
因为perl解释为"05"数字5