PHP:为什么乘以“1E3”会返回double而不是int?

问题描述 投票:0回答:2

我有以下代码:

$waitTimeInMs = random_int(500, 1000);
// 1E3 = 10 to the power of 3 = 1000
echo gettype($waitTimeInMs) .'|'.gettype($waitTimeInMs * 1E3) . '|' . gettype($waitTimeInMs * 1000) .'|' . $waitTimeInMs * 1E3;

这将返回integer|double|integer|759000

所以,实际上:

  • random_int()返回int;
  • 乘以1E3返回double;
  • 乘以1000返回int;

所以,如果1E3,为什么乘以int会返回一个双倍而不是1000 = 1E3

php int double exponent exponentiation
2个回答
1
投票

指数数字存储为double ..因为php不会更改正指数或负指数的数据类型

https://www.php.net/manual/en/language.types.float.php

数据类型是相同的

$c = 7E-10;

要么

$c = 7E+10;

但第一个通常是浮动


1
投票

你的假设是假的:

// 1E3 = 10 to the power of 3 = 1000

1E3+1.000E3的缩写形式。根据定义,科学计算机符号中的浮点数:

// 1.23E4   => 1.23 * 10^4
// computer => scientific notation of a real number

顺便说一句:0.123e5 === 1.23e4 === 12.3e3 === 123e2 === 12300e0 === 12300.0

© www.soinside.com 2019 - 2024. All rights reserved.