当'a`和`b`都是整数时,为什么`a ^ b`返回一个数字? [英] Why does `a ^ b` return a numeric when both `a` and `b` are integers?

查看:145
本文介绍了当'a`和`b`都是整数时,为什么`a ^ b`返回一个数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出两个整数:

a <- 1L
b <- 1L

正如我所料,加,减或乘以它们也给出一个整数:

As I would expect, adding, subtracting, or multiplying them also gives an integer:

class(a + b)
# [1] "integer"
class(a - b)
# [1] "integer"
class(a * b)
# [1] "integer"

但除以它们给出一个数字:

But dividing them gives a numeric:

class(a / b)
# [1] "numeric"

我想我能理解为什么:因为其他整数组合(例如 a < - 2L b< - 3L )将返回一个数字,它是一般更多的东西总是返回一个数字。

I think I can understand why: because other combinations of integers (e.g. a <- 2L and b <- 3L) would return a numeric, it is the more general thing to do to always return a numeric.

现在进行取幂:

class(a ^ b)
# [1] "numeric"

这是一个对我来说有点意外。任何人都可以解释为什么它是这样设计的吗?

This one is a bit of a surprise to me. Can anyone explain why it was designed this way?

推荐答案

这涵盖了指数为负数的情况。

This covers the case when the exponent is negative.

这篇关于当'a`和`b`都是整数时,为什么`a ^ b`返回一个数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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