src.elv.sh@v0.21.0-dev.0.20240515223629-06979efb9a2a/pkg/mods/math/math_test.elvts (about)

     1  //each:eval use math
     2  
     3  ////////////
     4  # math:abs #
     5  ////////////
     6  
     7  ~> math:abs 2
     8  ▶ (num 2)
     9  ~> math:abs -2
    10  ▶ (num 2)
    11  ~> math:abs -2147483648 # -2^31
    12  ▶ (num 2147483648)
    13  ~> math:abs -9223372036854775808 # -2^63
    14  ▶ (num 9223372036854775808)
    15  ~> math:abs 100000000000000000000
    16  ▶ (num 100000000000000000000)
    17  ~> math:abs -100000000000000000000
    18  ▶ (num 100000000000000000000)
    19  ~> math:abs -1/2
    20  ▶ (num 1/2)
    21  ~> math:abs 1/2
    22  ▶ (num 1/2)
    23  ~> math:abs 2.1
    24  ▶ (num 2.1)
    25  ~> math:abs -2.1
    26  ▶ (num 2.1)
    27  
    28  /////////////
    29  # math:ceil #
    30  /////////////
    31  
    32  ~> math:ceil 2
    33  ▶ (num 2)
    34  ~> math:ceil 100000000000000000000
    35  ▶ (num 100000000000000000000)
    36  ~> math:ceil 3/2
    37  ▶ (num 2)
    38  ~> math:ceil -3/2
    39  ▶ (num -1)
    40  ~> math:ceil 2.1
    41  ▶ (num 3.0)
    42  ~> math:ceil -2.1
    43  ▶ (num -2.0)
    44  
    45  //////////////
    46  # math:floor #
    47  //////////////
    48  
    49  ~> math:floor 2
    50  ▶ (num 2)
    51  ~> math:floor 100000000000000000000
    52  ▶ (num 100000000000000000000)
    53  ~> math:floor 3/2
    54  ▶ (num 1)
    55  ~> math:floor -3/2
    56  ▶ (num -2)
    57  ~> math:floor 2.1
    58  ▶ (num 2.0)
    59  ~> math:floor -2.1
    60  ▶ (num -3.0)
    61  
    62  //////////////
    63  # math:round #
    64  //////////////
    65  
    66  ~> math:round 2
    67  ▶ (num 2)
    68  ~> math:round 100000000000000000000
    69  ▶ (num 100000000000000000000)
    70  ~> math:round 1/3
    71  ▶ (num 0)
    72  ~> math:round 1/2
    73  ▶ (num 1)
    74  ~> math:round 2/3
    75  ▶ (num 1)
    76  ~> math:round -1/3
    77  ▶ (num 0)
    78  ~> math:round -1/2
    79  ▶ (num -1)
    80  ~> math:round -2/3
    81  ▶ (num -1)
    82  ~> math:round 2.1
    83  ▶ (num 2.0)
    84  ~> math:round 2.5
    85  ▶ (num 3.0)
    86  
    87  //////////////////////
    88  # math:round-to-even #
    89  //////////////////////
    90  
    91  ~> math:round-to-even 2
    92  ▶ (num 2)
    93  ~> math:round-to-even 100000000000000000000
    94  ▶ (num 100000000000000000000)
    95  ~> math:round-to-even 1/3
    96  ▶ (num 0)
    97  ~> math:round-to-even 2/3
    98  ▶ (num 1)
    99  ~> math:round-to-even -1/3
   100  ▶ (num 0)
   101  ~> math:round-to-even -2/3
   102  ▶ (num -1)
   103  ~> math:round-to-even 2.5
   104  ▶ (num 2.0)
   105  ~> math:round-to-even -2.5
   106  ▶ (num -2.0)
   107  ~> math:round-to-even 1/2
   108  ▶ (num 0)
   109  ~> math:round-to-even 3/2
   110  ▶ (num 2)
   111  ~> math:round-to-even 5/2
   112  ▶ (num 2)
   113  ~> math:round-to-even 7/2
   114  ▶ (num 4)
   115  ~> math:round-to-even -1/2
   116  ▶ (num 0)
   117  ~> math:round-to-even -3/2
   118  ▶ (num -2)
   119  ~> math:round-to-even -5/2
   120  ▶ (num -2)
   121  ~> math:round-to-even -7/2
   122  ▶ (num -4)
   123  
   124  //////////////
   125  # math:trunc #
   126  //////////////
   127  
   128  ~> math:trunc 2
   129  ▶ (num 2)
   130  ~> math:trunc 100000000000000000000
   131  ▶ (num 100000000000000000000)
   132  ~> math:trunc 3/2
   133  ▶ (num 1)
   134  ~> math:trunc -3/2
   135  ▶ (num -1)
   136  ~> math:trunc 2.1
   137  ▶ (num 2.0)
   138  ~> math:trunc -2.1
   139  ▶ (num -2.0)
   140  ~> math:trunc (num Inf)
   141  ▶ (num +Inf)
   142  ~> math:trunc (num NaN)
   143  ▶ (num NaN)
   144  
   145  
   146  ///////////////
   147  # math:is-inf #
   148  ///////////////
   149  ~> math:is-inf 1.3
   150  ▶ $false
   151  ~> math:is-inf &sign=0 inf
   152  ▶ $true
   153  ~> math:is-inf &sign=1 inf
   154  ▶ $true
   155  ~> math:is-inf &sign=-1 -inf
   156  ▶ $true
   157  ~> math:is-inf &sign=1 -inf
   158  ▶ $false
   159  ~> math:is-inf -inf
   160  ▶ $true
   161  ~> math:is-inf nan
   162  ▶ $false
   163  ~> math:is-inf 1
   164  ▶ $false
   165  ~> math:is-inf 100000000000000000000
   166  ▶ $false
   167  ~> math:is-inf 1/2
   168  ▶ $false
   169  
   170  ///////////////
   171  # math:is-nan #
   172  ///////////////
   173  ~> math:is-nan 1.3
   174  ▶ $false
   175  ~> math:is-nan inf
   176  ▶ $false
   177  ~> math:is-nan nan
   178  ▶ $true
   179  ~> math:is-nan 1
   180  ▶ $false
   181  ~> math:is-nan 100000000000000000000
   182  ▶ $false
   183  ~> math:is-nan 1/2
   184  ▶ $false
   185  
   186  ////////////
   187  # math:max #
   188  ////////////
   189  ~> math:max
   190  Exception: arity mismatch: arguments must be 1 or more values, but is 0 values
   191    [tty]:1:1-8: math:max
   192  ~> math:max 42
   193  ▶ (num 42)
   194  ~> math:max -3 3 10 -4
   195  ▶ (num 10)
   196  ~> math:max 2 10 100000000000000000000
   197  ▶ (num 100000000000000000000)
   198  ~> math:max 100000000000000000001 100000000000000000002 100000000000000000000
   199  ▶ (num 100000000000000000002)
   200  ~> math:max 1/2 1/3 2/3
   201  ▶ (num 2/3)
   202  ~> math:max 1.0 2.0
   203  ▶ (num 2.0)
   204  ~> math:max 3 NaN 5
   205  ▶ (num NaN)
   206  
   207  ////////////
   208  # math:min #
   209  ////////////
   210  ~> math:min
   211  Exception: arity mismatch: arguments must be 1 or more values, but is 0 values
   212    [tty]:1:1-8: math:min
   213  ~> math:min 42
   214  ▶ (num 42)
   215  ~> math:min -3 3 10 -4
   216  ▶ (num -4)
   217  ~> math:min 2 10 100000000000000000000
   218  ▶ (num 2)
   219  ~> math:min 100000000000000000001 100000000000000000002 100000000000000000000
   220  ▶ (num 100000000000000000000)
   221  ~> math:min 1/2 1/3 2/3
   222  ▶ (num 1/3)
   223  ~> math:min 1.0 2.0
   224  ▶ (num 1.0)
   225  ~> math:min 3 NaN 5
   226  ▶ (num NaN)
   227  
   228  ////////////
   229  # math:pow #
   230  ////////////
   231  
   232  ## base is int, exp is int ##
   233  ~> math:pow 2 0
   234  ▶ (num 1)
   235  ~> math:pow 2 1
   236  ▶ (num 2)
   237  ~> math:pow 2 -1
   238  ▶ (num 1/2)
   239  ~> math:pow 2 3
   240  ▶ (num 8)
   241  ~> math:pow 2 -3
   242  ▶ (num 1/8)
   243  
   244  ## base is *big.Rat, exp is int ##
   245  ~> math:pow 2/3 0
   246  ▶ (num 1)
   247  ~> math:pow 2/3 1
   248  ▶ (num 2/3)
   249  ~> math:pow 2/3 -1
   250  ▶ (num 3/2)
   251  ~> math:pow 2/3 3
   252  ▶ (num 8/27)
   253  ~> math:pow 2/3 -3
   254  ▶ (num 27/8)
   255  
   256  ## exp is *big.Rat ##
   257  ~> math:pow 4 1/2
   258  ▶ (num 2.0)
   259  
   260  ## exp is float64 ##
   261  ~> math:pow 2 2.0
   262  ▶ (num 4.0)
   263  ~> math:pow 1/2 2.0
   264  ▶ (num 0.25)
   265  
   266  ## base is float64 ##
   267  ~> math:pow 2.0 2
   268  ▶ (num 4.0)
   269  
   270  ////////////
   271  # $math:pi #
   272  ////////////
   273  
   274  // The exact values of some floating-point numbers can vary slightly by
   275  // architecture (in particular s370x), so test them at a lower precision. Use
   276  // %.4f as a convention.
   277  ~> printf "%.4f\n" $math:pi
   278  3.1416
   279  
   280  ///////////
   281  # $math:e #
   282  ///////////
   283  
   284  ~> printf "%.4f\n" $math:e
   285  2.7183
   286  
   287  ////////////
   288  # math:log #
   289  ////////////
   290  
   291  ~> math:log $math:e
   292  ▶ (num 1.0)
   293  ~> math:log 1
   294  ▶ (num 0.0)
   295  ~> math:log 0
   296  ▶ (num -Inf)
   297  ~> math:log -1
   298  ▶ (num NaN)
   299  
   300  //////////////
   301  # math:log10 #
   302  //////////////
   303  ~> math:log10 10.0
   304  ▶ (num 1.0)
   305  ~> math:log10 100.0
   306  ▶ (num 2.0)
   307  ~> math:log10 1
   308  ▶ (num 0.0)
   309  ~> math:log10 0
   310  ▶ (num -Inf)
   311  ~> math:log10 -1
   312  ▶ (num NaN)
   313  
   314  /////////////
   315  # math:log2 #
   316  /////////////
   317  
   318  ~> math:log2 8
   319  ▶ (num 3.0)
   320  ~> math:log2 1024.0
   321  ▶ (num 10.0)
   322  ~> math:log2 1
   323  ▶ (num 0.0)
   324  ~> math:log2 0
   325  ▶ (num -Inf)
   326  ~> math:log2 -1
   327  ▶ (num NaN)
   328  
   329  ////////////
   330  # math:cos #
   331  ////////////
   332  
   333  ~> math:cos 0
   334  ▶ (num 1.0)
   335  ~> math:cos 1 | printf "%.4f\n" (one)
   336  0.5403
   337  ~> math:cos $math:pi
   338  ▶ (num -1.0)
   339  
   340  /////////////
   341  # math:cosh #
   342  /////////////
   343  
   344  ~> math:cosh 0
   345  ▶ (num 1.0)
   346  ~> math:cosh inf
   347  ▶ (num +Inf)
   348  ~> math:cosh nan
   349  ▶ (num NaN)
   350  
   351  ////////////
   352  # math:sin #
   353  ////////////
   354  
   355  ~> math:sin 0
   356  ▶ (num 0.0)
   357  ~> math:sin 1 | printf "%.4f\n" (one)
   358  0.8415
   359  ~> math:sin $math:pi | printf "%.4f\n" (one)
   360  0.0000
   361  
   362  /////////////
   363  # math:sinh #
   364  /////////////
   365  
   366  ~> math:sinh 0
   367  ▶ (num 0.0)
   368  ~> math:sinh inf
   369  ▶ (num +Inf)
   370  ~> math:sinh nan
   371  ▶ (num NaN)
   372  
   373  ////////////
   374  # math:tan #
   375  ////////////
   376  
   377  ~> math:tan 0
   378  ▶ (num 0.0)
   379  ~> math:tan 1 | printf "%.4f\n" (one)
   380  1.5574
   381  ~> math:tan $math:pi | printf "%.4f\n" (one)
   382  -0.0000
   383  
   384  /////////////
   385  # math:tanh #
   386  /////////////
   387  
   388  ~> math:tanh 0
   389  ▶ (num 0.0)
   390  ~> math:tanh inf
   391  ▶ (num 1.0)
   392  ~> math:tanh nan
   393  ▶ (num NaN)
   394  
   395  /////////////
   396  # math:sqrt #
   397  /////////////
   398  
   399  ~> math:sqrt 0
   400  ▶ (num 0.0)
   401  ~> math:sqrt 4
   402  ▶ (num 2.0)
   403  ~> math:sqrt -4
   404  ▶ (num NaN)
   405  
   406  /////////////
   407  # math:acos #
   408  /////////////
   409  
   410  ~> math:acos 0 | printf "%.4f\n" (one)
   411  1.5708
   412  ~> math:acos 1
   413  ▶ (num 0.0)
   414  ~> math:acos 1.00001
   415  ▶ (num NaN)
   416  
   417  /////////////
   418  # math:asin #
   419  /////////////
   420  
   421  ~> math:asin 0
   422  ▶ (num 0.0)
   423  ~> math:asin 1 | printf "%.4f\n" (one)
   424  1.5708
   425  ~> math:asin 1.00001
   426  ▶ (num NaN)
   427  
   428  /////////////
   429  # math:atan #
   430  /////////////
   431  
   432  ~> math:atan 0
   433  ▶ (num 0.0)
   434  ~> math:atan 1 | printf "%.4f\n" (one)
   435  0.7854
   436  ~> math:atan inf | printf "%.4f\n" (one)
   437  1.5708
   438  
   439  //////////////
   440  # math:atan2 #
   441  //////////////
   442  
   443  ~> math:atan2 0 0
   444  ▶ (num 0.0)
   445  ~> math:atan2 1 1
   446  ▶ (num 0.7853981633974483)
   447  ~> math:atan2 -1 -1
   448  ▶ (num -2.356194490192345)
   449  
   450  
   451  //////////////
   452  # math:acosh #
   453  //////////////
   454  
   455  ~> math:acosh 0
   456  ▶ (num NaN)
   457  ~> math:acosh 1
   458  ▶ (num 0.0)
   459  ~> math:acosh nan
   460  ▶ (num NaN)
   461  
   462  //////////////
   463  # math:asinh #
   464  //////////////
   465  
   466  ~> math:asinh 0
   467  ▶ (num 0.0)
   468  ~> math:asinh 1 | printf "%.4f\n" (one)
   469  0.8814
   470  ~> math:asinh inf
   471  ▶ (num +Inf)
   472  
   473  //////////////
   474  # math:atanh #
   475  //////////////
   476  
   477  ~> math:atanh 0
   478  ▶ (num 0.0)
   479  ~> math:atanh 1
   480  ▶ (num +Inf)