github.com/arnodel/golua@v0.0.0-20230215163904-e0b5347eaaa1/runtime/lua/arith.lua (about)

     1  print(-(1+1))
     2  --> =-2
     3  
     4  print(-1.01)
     5  --> =-1.01
     6  
     7  print(pcall(function(x) return -x end, {}))
     8  --> ~^false\t
     9  
    10  print(1 + 1.2, 2.2 + 1, 3.2 + 1.2)
    11  --> =2.2	3.2	4.4
    12  
    13  local function testbin(f, x, y)
    14      print(not pcall(f, x, y) and not pcall(f, y, x))
    15  end
    16  
    17  testbin(function(x, y) return x + y end, 1, {})
    18  --> =true
    19  
    20  print(1 - 2, 3.5 - 2, 4 - 1.5, 4.5 - 2.5)
    21  --> =-1	1.5	2.5	2
    22   
    23  testbin(function(x, y) return x - y end, 1, {})
    24  --> =true
    25  
    26  print(1 * 1, 1.5 * 3, 2 * 2.5, 3.0 * 6.0)
    27  --> =1	4.5	5	18
    28  
    29  testbin(function(x, y) return x * y end, 1, {})
    30  --> =true
    31  
    32  print(4 / 2, 2 / 0.5, 4.0 / 2, 1.5 / 0.5)
    33  --> =2	4	2	3
    34  
    35  testbin(function(x, y) return x / y end, 1, {})
    36  --> =true
    37  
    38  print(4 // 2, 4.5 // 2, 2 // 0.8, 3.5 // 0.5)
    39  --> =2	2	2	7
    40  
    41  testbin(function(x, y) return x // y end, 1, {})
    42  --> =true
    43  
    44  print(5 % 2, 3.5 % 2, 2 % 0.5, 3.5 % 0.5)
    45  --> =1	1.5	0	0
    46  
    47  print(-3 % 2, -3.0 % 2)
    48  --> =1	1
    49  
    50  testbin(function(x, y) return x % y end, 1, {})
    51  --> =true
    52  
    53  print(3^2, 9^0.5, 0.5^2, 4.0^1.5)
    54  --> =9	3	0.25	8
    55  
    56  testbin(function(x, y) return x^y end, 1, {})
    57  --> =true
    58  
    59  testbin(function(x, y) return x^y end, 1, "a")
    60  --> =true
    61  
    62  testbin(function(x, y) return x+y end, 1, "12x")
    63  --> =true
    64  
    65  print(pcall(function() return 1//0 end))
    66  --> ~false\t.*divide by zero
    67  
    68  print(pcall(function() return 1%0 end))
    69  --> ~false\t.*perform 'n%0'
    70  
    71  print(0/0 == 0/0)
    72  --> =false
    73  
    74  print(1/0 == 1/0)
    75  --> =true
    76  
    77  print(-0.0 == 0.0)
    78  --> =true
    79  
    80  print(" 2 " * " 70")
    81  --> =140
    82  
    83  print(" 2e3  " ^ "  2.0" == 4000000)
    84  --> =true
    85  
    86  do
    87      local n = 100000000000000000000
    88  
    89      print(n == 1e20)
    90      --> =true
    91  
    92      print(math.type(n))
    93      --> =float
    94  end
    95  
    96  print("0x8.8" + 0)
    97  --> =8.5
    98  
    99  print("0x0.4p2" + 0)
   100  --> =1
   101  
   102  n=-12
   103  print(n%n)
   104  --> =0
   105  
   106  n=math.mininteger
   107  print(n%n)
   108  --> =0
   109  
   110  print("0xffff00000000000000000000000000000000001" + 1)
   111  --> =2