为什么带有十进制值的指数表示法会失败? [英] Why does exponential notation with decimal values fail?

查看:103
本文介绍了为什么带有十进制值的指数表示法会失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

常规上1e3表示10**3.

>>> 1e3
1000.0
>>> 10**3
1000

e**3相似的情况是exp(3).

>>> exp(3)
20.085536923187668
>>> e**3
20.085536923187664

但是现在请注意指数是否为float值:

However now notice if the exponent is a float value:

>>> exp(3.1)
22.197951281441636
>>> e**3.1
22.197951281441632

这很好.现在来看第一个示例:

which is fine. Now for the first example:

>>> 1e3.1
  File "<stdin>", line 1
    1e3.1
        ^
SyntaxError: invalid syntax
>>> 10**3.1
1258.9254117941675

显示Python

也不太喜欢1e3.1Fortran. 不管它为什么是标准的(!),为什么会这样?

which shows Python does not like 1e3.1, Fortran too. Regardless it could be a standard (!) why it is like that?

推荐答案

使用e的表示法是数字文字,它是许多编程语言的词法语法的一部分,基于标准形式/科学表示法.

The notation with the e is a numeric literal, part of the lexical syntax of many programming languages, based on standard form/scientific notation.

此符号的目的是允许您通过移动点位置来指定非常大/很小的数字.它并非旨在允许您将乘以10的任意幂的乘法编码为数字文字.因此,该点和随后的数字甚至都不会被识别为数字文字标记的一部分.

The purpose of this notation is to allow you to specify very large/small numbers by shifting the point position. It's not intended to allow you to encode multiplication by some arbitrary power of 10 into numeric literals. Therefore, that point and the following digits aren't even recognised as part of the numeric literal token.

如果您想要任意的幂,如发现的那样,可以使用数学函数和运算符来完成这项工作.与数字文字不同,您甚至可以在运行时确定参数值.

If you want arbitrary powers, as you've found, there are math functions and operators that do the job. Unlike a numeric literal, you even get to determine the parameter values at run-time.

这篇关于为什么带有十进制值的指数表示法会失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆