代码之家  ›  专栏  ›  技术社区  ›  Julia Learner anothershrubery

把地球上的原子赋给Julia中的一个变量

  •  0
  • Julia Learner anothershrubery  · 技术社区  · 7 年前

    在Python3.6中,以下操作用于为变量指定地球中原子的估计值:

    In[6]: atoms_in_earth = 10**50
    In[7]: atoms_in_earth
    Out[7]: 100000000000000000000000000000000000000000000000000
    

    julia> atoms_in_earth = 10^50
    -5376172055173529600
    
    julia> atoms_in_earth = BigInt(10^50)
    -5376172055173529600
    
    julia> atoms_in_earth = BigFloat(10^50)
    -5.3761720551735296e+18
    
    julia> atoms_in_earth = big(10^50)
    -5376172055173529600
    
    julia> atoms_in_earth = big"10^50"
    ERROR: ArgumentError: invalid number format 10^50 for BigInt or BigFloat
    Stacktrace:
     [1] top-level scope at none:0
    

    我能让这些方法发挥作用:

    julia> atoms_in_earth = big"1_0000000000_0000000000_0000000000_0000000000_0000000000"
    100000000000000000000000000000000000000000000000000
    
    julia> float(ans)
    1.0e+50
    
    julia> atoms_in_earth = parse(BigInt, '1' * '0'^50)
    100000000000000000000000000000000000000000000000000
    
    julia> float(ans)
    1.0e+50
    

    我错过了什么?

    2 回复  |  直到 7 年前
        1
  •  5
  •   HarmonicaMuse    7 年前

    朱莉娅用 本地的 默认情况下为整数,这些值可能会溢出。Python使用 大的

    你的第一个例子溢出了 Int64 :

    julia> atoms_in_earth = 10^50
    -5376172055173529600
    

    julia> atoms_in_earth = BigInt(10^50)
    -5376172055173529600
    
    julia> atoms_in_earth = BigFloat(10^50)
    -5.3761720551735296e+18
    
    julia> atoms_in_earth = big(10^50)
    -5376172055173529600
    

    您的第五个示例不是有效的大文本:

    julia> atoms_in_earth = big"10^50"
    ERROR: ArgumentError: invalid number format 10^50 for BigInt or BigFloat
    

    BigInt ,在你的例子中 10 任何进一步的行动都将被提升到 算术,然后是:

    julia> x = 10
    10
    
    julia> typeof(x)
    Int64
    
    julia> x = BigInt(10)
    10
    
    julia> typeof(x)
    BigInt
    
    julia> big(10) == big"10" == big(10)
    true
    
    julia> y = x^50
    100000000000000000000000000000000000000000000000000
    
    julia> typeof(y)
    BigInt
    

    在这种情况下 50 x^50 比基特

    julia> BigInt(10)^50 == big"10"^50 == big(10)^50
    true
    
        2
  •  1
  •   Julia Learner anothershrubery    7 年前

    julia> atoms_in_earth = big"1e50"
    1.0e+50
    
    julia> typeof(ans)
    BigFloat
    
    推荐文章