鱼C论坛

 找回密码
 立即注册
查看: 3680|回复: 0

[技术交流] pow与math.pow 以及关于math.pow得出来的数字会自动使用科学记数法的问题

[复制链接]
发表于 2017-6-21 23:22:12 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 Teagle 于 2017-6-21 23:22 编辑

pow与math.pow以及关于math.pow得出来的数字会自动使用科学记数法的问题



  • pow

    1. 官方文档的内容:
    2. pow(x, y[, z])
    3. Return x to the power y; if z is present, return x to the power y, modulo z (computed more efficiently than pow(x, y) % z). The two-argument form pow(x, y) is equivalent to using the power operator: x**y.

    4. The arguments must have numeric types. With mixed operand types, the coercion rules for binary arithmetic operators apply. For int operands, the result has the same type as the operands (after coercion) unless the second argument is negative; in that case, all arguments are converted to float and a float result is delivered. For example, 102 returns 100, but 10-2 returns 0.01. If the second argument is negative, the third argument must be omitted. If z is present, x and y must be of integer types, and y must be non-negative.
    复制代码


    如果参数z存在,相当于计算 pow(x,y)%z,但是相比之下计算的更有效率;而pow(x,y)则等价于 x**y。
    参数要求必须是可以计算的类型;对于int类型的操作数,结果便是相同的类型;如果第二个参数是负数的话,所有的参数会被强制转换为float类型,结果自然也会变成float类型。
    比如102 = 100, 10-2 = 0.0.1 相当于 10.0 ** -2.0。
    如果第二个参数是负数,那么比三个参数不能使用。
    如果想使用第三个参数,那么前两个参数必须是整型,而且第二个参数必须是正数。


  • math.pow


    1. 官方文档的内容:
    2. math.pow(x, y)
    3. Return x raised to the power y. Exceptional cases follow Annex ‘F’ of the C99 standard as far as possible. In particular, pow(1.0, x) and pow(x, 0.0) always return 1.0, even when x is a zero or a NaN. If both x and y are finite, x is negative, and y is not an integer then pow(x, y) is undefined, and raises ValueError.

    4. Unlike the built-in ** operator, math.pow() converts both its arguments to type float. Use ** or the built-in pow() function for computing exact integer powers.
    复制代码


    特点:
    math.pow(1.0,y) 与 pow(x,0.0)不管另外一个参数值是多少,结果总是 1.0。(内置函数pow同样如此)

    1. >>>pow(1.0,-2)
    2. 1.0
    3. >>>math.pow(1.0,-2)
    4. 1.0
    复制代码

    当x是负数,y不是整数,那么便会引发ValueErrot的错误。(内置函数pow不会)
    1. >>>pow(-2,0.5)
    2. (8.659560562354934e-17+1.4142135623730951j)
    3. >>>math.pow(-2,0.5)
    4.   File "<stdin>", line 1
    5.    pow(-2.0.5)
    6.              ^
    7. SyntaxError: invalid syntax
    复制代码

    不同于内建函数pow与**操作符,math.pow()会将两个参数先强制转换为float类型,在进行计算,结果,自然也是float类型
    1. >>> pow(2,2)
    2. 4
    3. >>> math.pow(2,2)
    4. 4.0
    复制代码




    通常使用**操作符或者内建函数pow来进行精确地整数型的计算。


  • 浮点数与科学技术法


在python中如果数据是浮点型并且位数很长便会自动使用科学计数法表示
也就是说如果结果整型并且很长的话,math.pow的返回值会自动使用科学记数法进行别是,并且会自动四舍五入
  1. >>> pow(2,2)
  2. 4
  3. >>> math.pow(2,2)
  4. 4.0
复制代码

如果结果是浮点型并且很长的话,math.pow与pow皆会用科学记数法进行表示
  1. >>> pow(2.0,342)
  2. 8.958978968711217e+102
  3. >>> math.pow(2.0,342)
  4. 8.958978968711217e+102
复制代码


感谢大佬' lh625243422 '的点播,以及大佬 ' jerryxjr1220 '的python挑战赛的题目

评分

参与人数 2鱼币 +8 收起 理由
康小泡 + 4
小甲鱼 + 4 支持楼主!

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-10-21 03:20

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表