美文网首页
Julia基础知识:算数运算,赋值和字符串

Julia基础知识:算数运算,赋值和字符串

作者: 蛮好蛮开心 | 来源:发表于2019-01-21 15:06 被阅读0次
    • 话题:
      • 算数运算
      • 数字运算
      • 字符串运算

    算数运算应该很熟悉

    3 + 7 # addition
    10 - 3 # subtraction
    20 * 5 # multiplication
    100 / 10 # division
    10 ^ 2 # exponentiation
    101 % 2 # remainder (modulus)
    sqrt(2) # square root
    √2 # Unicode to the rescue: \sqrt + TAB
    

    注意,除以两个整数会产生浮点数。 这里有两个额外的运算符可能会有所帮助:

    10 / 6
    10 ÷ 6 # \div TAB or the `div` function
    div(10, 6)
    10 // 6
    

    Numbers:不同方式来书写42

    fortytwos = (42, 42.0, 4.20e1, 4.20f1, 84//2, 0x2a)

    for x in fortytwos
        show(x)
        println("\tisa $(typeof(x))")
    end
    

    位运算

    0x2a & 0x70 # AND
    0x2a | 0x70 # OR
    42 & 112
    0b0010 << 2 # == 0b1000

    逻辑运算

    false&&true #and
    false || true # or

    注意复合运算

    x=-42
    x>0||error("x must be positive")
    

    比较运算

    1==1.0 # 相等
    1===1.0 # 编程相等
    3 < π
    1<=1
    .1+.2
    .1+.2≈ .3 # \approx + TAB
    

    比较“链”

    尝试在其中一个比较中插入括号
    2==2.0==0x02

    x=42
    0<x<100||error("x must be between 0 and 100")
    

    分配赋值

    x=1
    y=x
    x="hello"
    print(y)
    ϵ = eps(1.0) # You can make your own unicode names
    5ϵ # Juxtaposition is multiplication
    

    复合运算

    y+=1==y=y+1
    请注意,它只是为新值重新使用相同的名称。 这意味着类型甚至可能会改变:
    y/=2

    字符串

    s1 = "I am a string."
    s2 = """I am also a string. """
    "Here, we get an "error" because it's ambiguous where this string ends "
    """Look, Mom, no "errors"!!! """
    println("""The other nice thing about triple-quoted
                string literals is that they ignore leading
                indentation, which is nice for long strings
                in real code. Try changing these quotes!""")
    

    字符串不是用单个字符编写的,' - 用于单个字符:
    first(s1)
    '⊂'
    'If you try writing a string in single-quotes, you will get an error'

    字符串插值

    可以在字符串中使用美元符号来评估字符串中的Julia表达式 - 单个变量或更复杂的表达式:

    name = "Jane"
    num_fingers = 10
    num_toes = 10
    println("Hello, my name is $name.")
    println("I have $num_fingers fingers and $num_toes toes.")
    
    

    println("That is $(num_fingers + num_toes) digits in all!!")

    欢迎关注微信公众号Julia Code(_

    默认标题_横版二维码_2019.01.20.png

    相关文章

      网友评论

          本文标题:Julia基础知识:算数运算,赋值和字符串

          本文链接:https://www.haomeiwen.com/subject/rsucjqtx.html