[英]R not calculating large cubes correctly?
最近有一些關於三個立方體的發現的消息總共為42個。即,Andrew Sutherland和Andrew Booker發現(-80538738812075974)^ 3 + 80435758145817515 ^ 3 + 12602123297335631 ^ 3 = 42( https://math.mit。教育/〜畫了/ )
我有點修修補補,而且我在R中得不到42。
我確實在其他地方(WolframAlpha)得到它,但R給了我這個:
> (-80538738812075974)^3 + 80435758145817515^3 + 12602123297335631^3
[1] 1.992544e+35
知道我做錯了什么嗎? 這是R中大數字的限制嗎? 或者我(很可能)只是在做一些愚蠢的事情?
如果你想用R
大(整數)數來計算你可以使用gmp
庫,如:
library(gmp)
as.bigz("-80538738812075974")^3 + as.bigz("80435758145817515")^3 + as.bigz("12602123297335631")^3
#[1] 42
#or even better use also Integer in the exponent:
as.bigz("-80538738812075974")^3L + as.bigz("80435758145817515")^3L + as.bigz("12602123297335631")^3L
正如@mrflick指出你已經使用numeric
- 雙。 所以計算結果大致正確。 使用Integer
會收到警告, R
會將其轉換為numeric
:
(-80538738812075974L)^3L + 80435758145817515L^3L + 12602123297335631L^3L
#[1] 1.992544e+35
#Warning messages:
#1: non-integer value 80538738812075974L qualified with L; using numeric value
#2: non-integer value 80435758145817515L qualified with L; using numeric value
#3: non-integer value 12602123297335631L qualified with L; using numeric value
請注意,您必須為as.bigz
提供一個string
。 當寫一個數字R
將它視為double或轉換,如上所述,大整數,加倍並可能失去精度。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.