go.starlark.net@v0.0.0-20231101134539-556fd59b42f6/starlark/testdata/math.star (about)

     1  # Tests of math module.
     2  
     3  load('math.star', 'math')
     4  load('assert.star', 'assert')
     5  
     6  def near(got, want, threshold):
     7    return math.fabs(got-want) < threshold
     8  
     9  inf, nan = float("inf"), float("nan")
    10  
    11  # ceil
    12  assert.eq(math.ceil(0.0), 0.0)
    13  assert.eq(math.ceil(0.4), 1.0)
    14  assert.eq(math.ceil(0.5), 1.0)
    15  assert.eq(math.ceil(1.0), 1.0)
    16  assert.eq(math.ceil(10.0), 10.0)
    17  assert.eq(math.ceil(0), 0.0)
    18  assert.eq(math.ceil(1), 1.0)
    19  assert.eq(math.ceil(10), 10.0)
    20  assert.eq(math.ceil(-0.0), 0.0)
    21  assert.eq(math.ceil(-0.4), 0.0)
    22  assert.eq(math.ceil(-0.5), 0.0)
    23  assert.eq(math.ceil(-1.0), -1.0)
    24  assert.eq(math.ceil(-10.0), -10.0)
    25  assert.eq(math.ceil(-1), -1.0)
    26  assert.eq(math.ceil(-10), -10.0)
    27  assert.eq(type(math.ceil(0)), "int")
    28  assert.eq(type(math.ceil(0.4)), "int")
    29  assert.eq(type(math.ceil(10)), "int")
    30  assert.eq(type(math.ceil(-10.0)), "int")
    31  assert.eq(type(math.ceil(-0.5)), "int")
    32  assert.eq(math.ceil((1<<63) + 0.5), int(float((1<<63) + 1)))
    33  assert.fails(
    34    lambda: math.ceil(inf), "cannot convert float infinity to integer")
    35  assert.fails(
    36    lambda: math.ceil(-inf), "cannot convert float infinity to integer")
    37  assert.fails(
    38    lambda: math.ceil(nan), "cannot convert float NaN to integer")
    39  assert.fails(lambda: math.ceil("0"), "got string, want float or int")
    40  # fabs
    41  assert.eq(math.fabs(2.0), 2.0)
    42  assert.eq(math.fabs(0.0), 0.0)
    43  assert.eq(math.fabs(-2.0), 2.0)
    44  assert.eq(math.fabs(2), 2)
    45  assert.eq(math.fabs(0), 0)
    46  assert.eq(math.fabs(-2), 2)
    47  assert.eq(math.fabs(inf), inf)
    48  assert.eq(math.fabs(-inf), inf)
    49  assert.eq(math.fabs(nan), nan)
    50  assert.fails(lambda: math.fabs("0"), "got string, want float or int")
    51  # floor
    52  assert.eq(math.floor(0.0), 0.0)
    53  assert.eq(math.floor(0.4), 0.0)
    54  assert.eq(math.floor(0.5), 0.0)
    55  assert.eq(math.floor(1.0), 1.0)
    56  assert.eq(math.floor(10.0), 10.0)
    57  assert.eq(math.floor(-0.0), 0.0)
    58  assert.eq(math.floor(-0.4), -1.0)
    59  assert.eq(math.floor(-0.5), -1.0)
    60  assert.eq(math.floor(-1.0), -1.0)
    61  assert.eq(math.floor(-10.0), -10.0)
    62  assert.eq(type(math.floor(0)), "int")
    63  assert.eq(type(math.floor(0.4)), "int")
    64  assert.eq(type(math.floor(10)), "int")
    65  assert.eq(type(math.floor(-10.0)), "int")
    66  assert.eq(type(math.floor(-0.5)), "int")
    67  assert.eq(math.floor((1<<63) + 0.5), int(float(1<<63)))
    68  assert.fails(
    69    lambda: math.floor(inf), "cannot convert float infinity to integer")
    70  assert.fails(
    71    lambda: math.floor(-inf), "cannot convert float infinity to integer")
    72  assert.fails(
    73    lambda: math.floor(nan), "cannot convert float NaN to integer")
    74  assert.fails(lambda: math.floor("0"), "got string, want float or int")
    75  # mod
    76  assert.eq(math.mod(5, 3), 2)
    77  assert.eq(math.mod(inf, 1), nan)
    78  assert.eq(math.mod(-inf, 1.0), nan)
    79  assert.eq(math.mod(nan, 1.0), nan)
    80  assert.eq(math.mod(1.0, 0.0), nan)
    81  assert.eq(math.mod(1.0, inf), 1)
    82  assert.eq(math.mod(1.0, -inf), 1)
    83  assert.eq(math.mod(1.0, nan), nan)
    84  assert.fails(lambda: math.mod("0", 1.0), "got string, want float or int")
    85  assert.fails(lambda: math.mod(1.0, "0"), "got string, want float or int")
    86  # pow
    87  assert.eq(math.pow(5, 3), 125)
    88  assert.eq(math.pow(5, 0), 1)
    89  assert.eq(math.pow(5, 1), 5)
    90  assert.eq(math.pow(1, 5), 1)
    91  assert.eq(math.pow(inf, 1), inf)
    92  assert.eq(math.pow(-inf, 1.0), -inf)
    93  assert.eq(math.pow(nan, 1.0), nan)
    94  assert.eq(math.pow(1.1, inf), inf)
    95  assert.eq(math.pow(1.1, -inf), 0)
    96  assert.eq(math.pow(2.0, nan), nan)
    97  assert.fails(lambda: math.pow("0", 1.0), "got string, want float or int")
    98  assert.fails(lambda: math.pow(1.0, "0"), "got string, want float or int")
    99  # copysign
   100  assert.eq(math.copysign(3.2, -1), -3.2)
   101  assert.eq(math.copysign(inf, -1.0),-inf)
   102  assert.eq(math.copysign(-inf, -1), -inf)
   103  assert.eq(math.copysign(nan, -1), nan)
   104  assert.eq(math.copysign(-1, nan), 1)
   105  assert.fails(lambda: math.copysign("0", 1.0), "got string, want float or int")
   106  assert.fails(lambda: math.copysign(1.0, "0"), "got string, want float or int")
   107  # remainder
   108  assert.eq(math.remainder(3, 5), -2)
   109  assert.eq(math.remainder(1, 0), nan)
   110  assert.eq(math.remainder(2, inf), 2)
   111  assert.eq(math.remainder(2, -inf), 2)
   112  assert.eq(math.remainder(inf, -1.0), nan)
   113  assert.eq(math.remainder(-inf, -1), nan)
   114  assert.eq(math.remainder(nan, -1), nan)
   115  assert.eq(math.remainder(-1, nan), nan)
   116  assert.fails(lambda: math.remainder("0", 1.0), "got string, want float or int")
   117  assert.fails(lambda: math.remainder(1.0, "0"), "got string, want float or int")
   118  # round
   119  assert.eq(math.round(0.0), 0.0)
   120  assert.eq(math.round(0.4), 0.0)
   121  assert.eq(math.round(0.5), 1.0)
   122  assert.eq(math.round(0.6), 1.0)
   123  assert.eq(math.round(1.0), 1.0)
   124  assert.eq(math.round(10.0), 10.0)
   125  assert.eq(math.round(inf), inf)
   126  assert.eq(math.round(nan), nan)
   127  assert.eq(math.round(-0.4), 0.0)
   128  assert.eq(math.round(-0.5), -1.0)
   129  assert.eq(math.round(-0.6), -1.0)
   130  assert.eq(math.round(-1.0), -1.0)
   131  assert.eq(math.round(-10.0), -10.0)
   132  assert.eq(math.round(-inf), -inf)
   133  assert.fails(lambda: math.round("0"), "got string, want float or int")
   134  # exp
   135  assert.eq(math.exp(0.0), 1)
   136  assert.eq(math.exp(1.0), math.e)
   137  assert.true(near(math.exp(2.0), math.e * math.e, 0.00000000000001))
   138  assert.eq(math.exp(-1.0), 1 / math.e)
   139  assert.eq(math.exp(0), 1)
   140  assert.eq(math.exp(1), math.e)
   141  assert.true(near(math.exp(2), math.e * math.e, 0.00000000000001))
   142  assert.eq(math.exp(-1), 1 / math.e)
   143  assert.eq(math.exp(inf), inf)
   144  assert.eq(math.exp(-inf), 0)
   145  assert.eq(math.exp(nan), nan)
   146  assert.fails(lambda: math.exp("0"), "got string, want float or int")
   147  # sqrt
   148  assert.eq(math.sqrt(0.0), 0.0)
   149  assert.eq(math.sqrt(4.0), 2.0)
   150  assert.eq(math.sqrt(-4.0), nan)
   151  assert.eq(math.sqrt(0), 0)
   152  assert.eq(math.sqrt(4), 2)
   153  assert.eq(math.sqrt(-4), nan)
   154  assert.eq(math.sqrt(nan), nan)
   155  assert.eq(math.sqrt(inf), inf)
   156  assert.eq(math.sqrt(-inf), nan)
   157  assert.fails(lambda: math.sqrt("0"), "got string, want float or int")
   158  # acos
   159  assert.eq(math.acos(1.0), 0)
   160  assert.eq(math.acos(1), 0)
   161  assert.eq(math.acos(0.0), math.pi / 2)
   162  assert.eq(math.acos(0), math.pi / 2)
   163  assert.eq(math.acos(-1.0), math.pi)
   164  assert.eq(math.acos(-1), math.pi)
   165  assert.eq(math.acos(1.01), nan)
   166  assert.eq(math.acos(-1.01), nan)
   167  assert.eq(math.acos(inf), nan)
   168  assert.eq(math.acos(-inf), nan)
   169  assert.eq(math.acos(nan), nan)
   170  assert.fails(lambda: math.acos("0"), "got string, want float or int")
   171  # asin
   172  assert.eq(math.asin(0.0), 0)
   173  assert.eq(math.asin(1.0), math.pi / 2)
   174  assert.eq(math.asin(-1.0), -math.pi / 2)
   175  assert.eq(math.asin(0), 0)
   176  assert.eq(math.asin(1), math.pi / 2)
   177  assert.eq(math.asin(-1), -math.pi / 2)
   178  assert.eq(math.asin(1.01), nan)
   179  assert.eq(math.asin(-1.01), nan)
   180  assert.eq(math.asin(inf), nan)
   181  assert.eq(math.asin(-inf), nan)
   182  assert.eq(math.asin(nan), nan)
   183  assert.fails(lambda: math.asin("0"), "got string, want float or int")
   184  # atan
   185  assert.eq(math.atan(0.0), 0)
   186  assert.eq(math.atan(1.0), math.pi / 4)
   187  assert.eq(math.atan(-1.0), -math.pi / 4)
   188  assert.eq(math.atan(1), math.pi / 4)
   189  assert.eq(math.atan(-1), -math.pi / 4)
   190  assert.eq(math.atan(inf), math.pi / 2)
   191  assert.eq(math.atan(-inf), -math.pi / 2)
   192  assert.eq(math.atan(nan), nan)
   193  assert.fails(lambda: math.atan("0"), "got string, want float or int")
   194  # atan2
   195  assert.eq(math.atan2(1.0, 1.0), math.pi / 4)
   196  assert.eq(math.atan2(-1.0, 1.0), -math.pi / 4)
   197  assert.eq(math.atan2(0.0, 10.0), 0)
   198  assert.eq(math.atan2(0.0, -10.0), math.pi)
   199  assert.eq(math.atan2(-0.0, -10.0), -math.pi)
   200  assert.eq(math.atan2(10.0, 0.0), math.pi / 2)
   201  assert.eq(math.atan2(-10.0, 0.0), -math.pi / 2)
   202  assert.eq(math.atan2(1, 1), math.pi / 4)
   203  assert.eq(math.atan2(-1, 1), -math.pi / 4)
   204  assert.eq(math.atan2(0, 10.0), 0)
   205  assert.eq(math.atan2(0.0, -10), math.pi)
   206  assert.eq(math.atan2(-0.0, -10), -math.pi)
   207  assert.eq(math.atan2(10.0, 0), math.pi / 2)
   208  assert.eq(math.atan2(-10.0, 0), -math.pi / 2)
   209  assert.eq(math.atan2(1.0, nan), nan)
   210  assert.eq(math.atan2(nan, 1.0), nan)
   211  assert.eq(math.atan2(10.0, inf), 0)
   212  assert.eq(math.atan2(-10.0, inf), 0)
   213  assert.eq(math.atan2(10.0, -inf), math.pi)
   214  assert.eq(math.atan2(-10.0, -inf), -math.pi)
   215  assert.eq(math.atan2(inf, 10.0), math.pi / 2)
   216  assert.eq(math.atan2(inf, -10.0), math.pi / 2)
   217  assert.eq(math.atan2(-inf, 10.0), -math.pi / 2)
   218  assert.eq(math.atan2(-inf, -10.0), -math.pi / 2)
   219  assert.eq(math.atan2(inf, inf), math.pi / 4)
   220  assert.eq(math.atan2(-inf, inf), -math.pi / 4)
   221  assert.eq(math.atan2(inf, -inf), 3 * math.pi / 4)
   222  assert.eq(math.atan2(-inf, -inf), -3 * math.pi / 4)
   223  assert.fails(lambda: math.atan2("0", 1.0), "got string, want float or int")
   224  assert.fails(lambda: math.atan2(1.0, "0"), "got string, want float or int")
   225  # cos
   226  assert.eq(math.cos(0.0), 1)
   227  assert.true(near(math.cos(math.pi / 2), 0, 0.00000000000001))
   228  assert.eq(math.cos(math.pi), -1)
   229  assert.true(near(math.cos(-math.pi / 2), 0, 0.00000000000001))
   230  assert.eq(math.cos(-math.pi), -1)
   231  assert.eq(math.cos(inf), nan)
   232  assert.eq(math.cos(-inf), nan)
   233  assert.eq(math.cos(nan), nan)
   234  assert.fails(lambda: math.cos("0"), "got string, want float or int")
   235  # hypot
   236  assert.eq(math.hypot(4.0, 3.0), 5.0)
   237  assert.eq(math.hypot(4, 3), 5.0)
   238  assert.eq(math.hypot(inf, 3.0), inf)
   239  assert.eq(math.hypot(-inf, 3.0), inf)
   240  assert.eq(math.hypot(3.0, inf), inf)
   241  assert.eq(math.hypot(3.0, -inf), inf)
   242  assert.eq(math.hypot(nan, 3.0), nan)
   243  assert.eq(math.hypot(3.0, nan), nan)
   244  assert.fails(lambda: math.hypot("0", 1.0), "got string, want float or int")
   245  assert.fails(lambda: math.hypot(1.0, "0"), "got string, want float or int")
   246  # sin
   247  assert.eq(math.sin(0.0), 0)
   248  assert.eq(math.sin(0), 0)
   249  assert.eq(math.sin(math.pi / 2), 1)
   250  assert.eq(math.sin(-math.pi / 2), -1)
   251  assert.eq(math.sin(inf), nan)
   252  assert.eq(math.sin(-inf), nan)
   253  assert.eq(math.sin(nan), nan)
   254  assert.fails(lambda: math.sin("0"), "got string, want float or int")
   255  # tan
   256  assert.eq(math.tan(0.0), 0)
   257  assert.eq(math.tan(0), 0)
   258  assert.true(near(math.tan(math.pi / 4), 1, 0.00000000000001))
   259  assert.true(near(math.tan(-math.pi / 4), -1, 0.00000000000001))
   260  assert.eq(math.tan(inf), nan)
   261  assert.eq(math.tan(-inf), nan)
   262  assert.eq(math.tan(nan), nan)
   263  assert.fails(lambda: math.tan("0"), "got string, want float or int")
   264  # degrees
   265  oneDeg = 57.29577951308232
   266  assert.eq(math.degrees(1.0), oneDeg)
   267  assert.eq(math.degrees(1), oneDeg)
   268  assert.eq(math.degrees(-1.0), -oneDeg)
   269  assert.eq(math.degrees(-1), -oneDeg)
   270  assert.eq(math.degrees(inf), inf)
   271  assert.eq(math.degrees(-inf), -inf)
   272  assert.eq(math.degrees(nan), nan)
   273  assert.fails(lambda: math.degrees("0"), "got string, want float or int")
   274  # radians
   275  oneRad = 0.017453292519943295
   276  assert.eq(math.radians(1.0), oneRad)
   277  assert.eq(math.radians(-1.0), -oneRad)
   278  assert.eq(math.radians(1), oneRad)
   279  assert.eq(math.radians(-1), -oneRad)
   280  assert.eq(math.radians(inf), inf)
   281  assert.eq(math.radians(-inf), -inf)
   282  assert.eq(math.radians(nan), nan)
   283  assert.fails(lambda: math.radians("0"), "got string, want float or int")
   284  # acosh
   285  assert.eq(math.acosh(1.0), 0)
   286  assert.eq(math.acosh(1), 0)
   287  assert.eq(math.acosh(0.99), nan)
   288  assert.eq(math.acosh(0), nan)
   289  assert.eq(math.acosh(-0.99), nan)
   290  assert.eq(math.acosh(-inf), nan)
   291  assert.eq(math.acosh(inf), inf)
   292  assert.eq(math.acosh(nan), nan)
   293  assert.fails(lambda: math.acosh("0"), "got string, want float or int")
   294  # asinh
   295  asinhOne = 0.8813735870195432
   296  assert.eq(math.asinh(0.0), 0)
   297  assert.eq(math.asinh(0), 0)
   298  assert.true(near(math.asinh(1.0), asinhOne, 0.00000001))
   299  assert.true(near(math.asinh(1), asinhOne, 0.00000001))
   300  assert.true(near(math.asinh(-1.0), -asinhOne, 0.00000001))
   301  assert.true(near(math.asinh(-1), -asinhOne, 0.00000001))
   302  assert.eq(math.asinh(inf), inf)
   303  assert.eq(math.asinh(-inf), -inf)
   304  assert.eq(math.asinh(nan), nan)
   305  assert.fails(lambda: math.asinh("0"), "got string, want float or int")
   306  # atanh
   307  atanhHalf = 0.5493061443340548
   308  assert.eq(math.atanh(0.0), 0)
   309  assert.eq(math.atanh(0), 0)
   310  assert.eq(math.atanh(0.5), atanhHalf)
   311  assert.eq(math.atanh(-0.5), -atanhHalf)
   312  assert.eq(math.atanh(1), inf)
   313  assert.eq(math.atanh(-1), -inf)
   314  assert.eq(math.atanh(1.1), nan)
   315  assert.eq(math.atanh(-1.1), nan)
   316  assert.eq(math.atanh(inf), nan)
   317  assert.eq(math.atanh(-inf), nan)
   318  assert.eq(math.atanh(nan), nan)
   319  assert.fails(lambda: math.atanh("0"), "got string, want float or int")
   320  # cosh
   321  coshOne = 1.5430806348152437
   322  assert.eq(math.cosh(1.0), coshOne)
   323  assert.eq(math.cosh(1), coshOne)
   324  assert.eq(math.cosh(0.0), 1)
   325  assert.eq(math.cosh(0), 1)
   326  assert.eq(math.cosh(-inf), inf)
   327  assert.eq(math.cosh(inf), inf)
   328  assert.eq(math.cosh(nan), nan)
   329  assert.fails(lambda: math.cosh("0"), "got string, want float or int")
   330  # sinh
   331  sinhOne = 1.1752011936438014
   332  assert.eq(math.sinh(0.0), 0)
   333  assert.eq(math.sinh(0), 0)
   334  assert.eq(math.sinh(1.0), sinhOne)
   335  assert.eq(math.sinh(1), sinhOne)
   336  assert.eq(math.sinh(-1.0), -sinhOne)
   337  assert.eq(math.sinh(-1), -sinhOne)
   338  assert.eq(math.sinh(-inf), -inf)
   339  assert.eq(math.sinh(inf), inf)
   340  assert.eq(math.sinh(nan), nan)
   341  assert.fails(lambda: math.sinh("0"), "got string, want float or int")
   342  # tanh
   343  tanhOne = 0.7615941559557649
   344  assert.eq(math.tanh(0.0), 0)
   345  assert.eq(math.tanh(0), 0)
   346  assert.eq(math.tanh(1.0), tanhOne)
   347  assert.eq(math.tanh(1), tanhOne)
   348  assert.eq(math.tanh(-1.0), -tanhOne)
   349  assert.eq(math.tanh(-1), -tanhOne)
   350  assert.eq(math.tanh(-inf), -1)
   351  assert.eq(math.tanh(inf), 1)
   352  assert.eq(math.tanh(nan), nan)
   353  assert.fails(lambda: math.tanh("0"), "got string, want float or int")
   354  # log
   355  assert.eq(math.log(math.e), 1)
   356  assert.eq(math.log(10, 10), 1)
   357  assert.eq(math.log(10.0, 10.0), 1)
   358  assert.eq(math.log(2, 2.0), 1)
   359  assert.fails(lambda: math.log(2, 1), "division by zero")
   360  assert.fails(lambda: math.log(0.99, 1.0), "division by zero")
   361  assert.eq(math.log(0.0), -inf)
   362  assert.eq(math.log(0), -inf)
   363  assert.eq(math.log(-1.0), nan)
   364  assert.eq(math.log(-1), nan)
   365  assert.eq(math.log(nan), nan)
   366  assert.fails(lambda: math.log("0"), "got string, want float or int")
   367  assert.fails(lambda: math.log(10, "10"), "got string, want float or int")
   368  # gamma
   369  assert.eq(math.gamma(1.0), 1)
   370  assert.eq(math.gamma(1), 1)
   371  assert.eq(math.gamma(-1), nan)
   372  assert.eq(math.gamma(0), inf)
   373  assert.eq(math.gamma(-inf), nan)
   374  assert.eq(math.gamma(inf), inf)
   375  assert.eq(math.gamma(nan), nan)
   376  assert.fails(lambda: math.gamma("0"), "got string, want float or int")
   377  # Constants
   378  assert.eq(math.e, 2.7182818284590452)
   379  assert.eq(math.pi, 3.1415926535897932)