python - 从科学记数法中提取指数
问题描述
我有一堆科学计数法的数字,我想找到它们的指数。例如:
>>> find_exp(3.7e-13)
13
>>> find_exp(-7.2e-11)
11
因此,我只需要他们的指数,而忽略包括符号在内的所有其他内容。
我在 python 中寻找过这种方式,但类似的问题仅用于格式化目的。
解决方案
常用对数是您在这里需要的,您可以使用log10
+ floor
:
from math import log10, floor
def find_exp(number) -> int:
base10 = log10(abs(number))
return abs(floor(base10))
find_exp(3.7e-13)
# 13
find_exp(-7.2e-11)
# 11
推荐阅读
- javascript - 有没有办法在拖动光标并按下鼠标时将 CSS 类添加到 DOM 元素?
- google-app-engine - `gcloud app logs read --version=[myversion]` 调用时挂起,从不执行
- flutter - Flutter:从另一个小部件中按键隐藏/显示一个小部件
- javascript - Typegoose 中的数字数组与对象类型
- php - 使用 PHP 将单个整数变量转换为数字列表(例如 +/-3)
- php - DirectoryIterator 按字母顺序列出
- typescript - Typescript - 如何从可能值的数组中动态定义类型?
- python - 如何在 Keras 中使用 fit_generator() 平衡数据集?
- html - HTMLWebViewSource 视频全屏选项已禁用
- node.js - 在 handleFormSubmit 之后 React Checkbox 总是返回 false