builtin_fn_num.d.elv 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. #elvdoc:fn rand
  2. #
  3. # ```elvish
  4. # rand
  5. # ```
  6. #
  7. # Output a pseudo-random number in the interval [0, 1). Example:
  8. #
  9. # ```elvish-transcript
  10. # ~> rand
  11. # ▶ 0.17843564133528436
  12. # ```
  13. #elvdoc:fn num
  14. #
  15. # ```elvish
  16. # num $string-or-number
  17. # ```
  18. #
  19. # Constructs a [typed number](./language.html#number).
  20. #
  21. # If the argument is a string, this command outputs the typed number the
  22. # argument represents, or raises an exception if the argument is not a valid
  23. # representation of a number. If the argument is already a typed number, this
  24. # command outputs it as is.
  25. #
  26. # This command is usually not needed for working with numbers; see the
  27. # discussion of [numeric commands](#numeric-commands).
  28. #
  29. # Examples:
  30. #
  31. # ```elvish-transcript
  32. # ~> num 10
  33. # ▶ (num 10)
  34. # ~> num 0x10
  35. # ▶ (num 16)
  36. # ~> num 1/12
  37. # ▶ (num 1/12)
  38. # ~> num 3.14
  39. # ▶ (num 3.14)
  40. # ~> num (num 10)
  41. # ▶ (num 10)
  42. # ```
  43. #
  44. # @cf exact-num inexact-num
  45. #elvdoc:fn exact-num
  46. #
  47. # ```elvish
  48. # exact-num $string-or-number
  49. # ```
  50. #
  51. # Coerces the argument to an exact number. If the argument is infinity or NaN,
  52. # an exception is thrown.
  53. #
  54. # If the argument is a string, it is converted to a typed number first. If the
  55. # argument is already an exact number, it is returned as is.
  56. #
  57. # Examples:
  58. #
  59. # ```elvish-transcript
  60. # ~> exact-num (num 0.125)
  61. # ▶ (num 1/8)
  62. # ~> exact-num 0.125
  63. # ▶ (num 1/8)
  64. # ~> exact-num (num 1)
  65. # ▶ (num 1)
  66. # ```
  67. #
  68. # Beware that seemingly simple fractions that can't be represented precisely in
  69. # binary can result in the denominator being a very large power of 2:
  70. #
  71. # ```elvish-transcript
  72. # ~> exact-num 0.1
  73. # ▶ (num 3602879701896397/36028797018963968)
  74. # ```
  75. #
  76. # @cf num inexact-num
  77. #elvdoc:fn inexact-num
  78. #
  79. # ```elvish
  80. # inexact-num $string-or-number
  81. # ```
  82. #
  83. # Coerces the argument to an inexact number.
  84. #
  85. # If the argument is a string, it is converted to a typed number first. If the
  86. # argument is already an inexact number, it is returned as is.
  87. #
  88. # Examples:
  89. #
  90. # ```elvish-transcript
  91. # ~> inexact-num (num 1)
  92. # ▶ (num 1.0)
  93. # ~> inexact-num (num 0.5)
  94. # ▶ (num 0.5)
  95. # ~> inexact-num (num 1/2)
  96. # ▶ (num 0.5)
  97. # ~> inexact-num 1/2
  98. # ▶ (num 0.5)
  99. # ```
  100. #
  101. # Since the underlying representation for inexact numbers has limited range,
  102. # numbers with very large magnitudes may be converted to an infinite value:
  103. #
  104. # ```elvish-transcript
  105. # ~> inexact-num 1000000000000000000
  106. # ▶ (num 1e+18)
  107. # ~> inexact-num 10000000000000000000
  108. # ▶ (num +Inf)
  109. # ~> inexact-num -10000000000000000000
  110. # ▶ (num -Inf)
  111. # ```
  112. #
  113. # Likewise, numbers with very small magnitudes may be converted to 0:
  114. #
  115. # ```elvish-transcript
  116. # ~> use math
  117. # ~> math:pow 10 -323
  118. # ▶ (num 1/100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000)
  119. # ~> inexact-num (math:pow 10 -323)
  120. # ▶ (num 1e-323)
  121. # ~> math:pow 10 -324
  122. # ▶ (num 1/1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000)
  123. # ~> inexact-num (math:pow 10 -324)
  124. # ▶ (num 0.0)
  125. # ```
  126. #
  127. # @cf num exact-num
  128. #elvdoc:fn float64
  129. #
  130. # ```elvish
  131. # float64 $string-or-number
  132. # ```
  133. #
  134. # Constructs a floating-point number.
  135. #
  136. # This command is deprecated; use [`num`](#num) to construct a typed number, or
  137. # [`inexact-num`](#inexact-num) to construct an inexact number.
  138. #elvdoc:fn < <= == != > >= {#num-cmp}
  139. #
  140. # ```elvish
  141. # < $number... # less
  142. # <= $number... # less or equal
  143. # == $number... # equal
  144. # != $number... # not equal
  145. # > $number... # greater
  146. # >= $number... # greater or equal
  147. # ```
  148. #
  149. # Number comparisons. All of them accept an arbitrary number of arguments:
  150. #
  151. # 1. When given fewer than two arguments, all output `$true`.
  152. #
  153. # 2. When given two arguments, output whether the two arguments satisfy the named
  154. # relationship.
  155. #
  156. # 3. When given more than two arguments, output whether every adjacent pair of
  157. # numbers satisfy the named relationship.
  158. #
  159. # Examples:
  160. #
  161. # ```elvish-transcript
  162. # ~> == 3 3.0
  163. # ▶ $true
  164. # ~> < 3 4
  165. # ▶ $true
  166. # ~> < 3 4 10
  167. # ▶ $true
  168. # ~> < 6 9 1
  169. # ▶ $false
  170. # ```
  171. #
  172. # As a consequence of rule 3, the `!=` command outputs `$true` as long as any
  173. # _adjacent_ pair of numbers are not equal, even if some numbers that are not
  174. # adjacent are equal:
  175. #
  176. # ```elvish-transcript
  177. # ~> != 5 5 4
  178. # ▶ $false
  179. # ~> != 5 6 5
  180. # ▶ $true
  181. # ```
  182. #elvdoc:fn + {#add}
  183. #
  184. # ```elvish
  185. # + $num...
  186. # ```
  187. #
  188. # Outputs the sum of all arguments, or 0 when there are no arguments.
  189. #
  190. # This command is [exactness-preserving](#exactness-preserving).
  191. #
  192. # Examples:
  193. #
  194. # ```elvish-transcript
  195. # ~> + 5 2 7
  196. # ▶ (num 14)
  197. # ~> + 1/2 1/3 1/4
  198. # ▶ (num 13/12)
  199. # ~> + 1/2 0.5
  200. # ▶ (num 1.0)
  201. # ```
  202. #elvdoc:fn - {#sub}
  203. #
  204. # ```elvish
  205. # - $x-num $y-num...
  206. # ```
  207. #
  208. # Outputs the result of subtracting from `$x-num` all the `$y-num`s, working
  209. # from left to right. When no `$y-num` is given, outputs the negation of
  210. # `$x-num` instead (in other words, `- $x-num` is equivalent to `- 0 $x-num`).
  211. #
  212. # This command is [exactness-preserving](#exactness-preserving).
  213. #
  214. # Examples:
  215. #
  216. # ```elvish-transcript
  217. # ~> - 5
  218. # ▶ (num -5)
  219. # ~> - 5 2
  220. # ▶ (num 3)
  221. # ~> - 5 2 7
  222. # ▶ (num -4)
  223. # ~> - 1/2 1/3
  224. # ▶ (num 1/6)
  225. # ~> - 1/2 0.3
  226. # ▶ (num 0.2)
  227. # ~> - 10
  228. # ▶ (num -10)
  229. # ```
  230. #elvdoc:fn * {#mul}
  231. #
  232. # ```elvish
  233. # * $num...
  234. # ```
  235. #
  236. # Outputs the product of all arguments, or 1 when there are no arguments.
  237. #
  238. # This command is [exactness-preserving](#exactness-preserving). Additionally,
  239. # when any argument is exact 0 and no other argument is a floating-point
  240. # infinity, the result is exact 0.
  241. #
  242. # Examples:
  243. #
  244. # ```elvish-transcript
  245. # ~> * 2 5 7
  246. # ▶ (num 70)
  247. # ~> * 1/2 0.5
  248. # ▶ (num 0.25)
  249. # ~> * 0 0.5
  250. # ▶ (num 0)
  251. # ```
  252. #elvdoc:fn / {#div}
  253. #
  254. # ```elvish
  255. # / $x-num $y-num...
  256. # ```
  257. #
  258. # Outputs the result of dividing `$x-num` with all the `$y-num`s, working from
  259. # left to right. When no `$y-num` is given, outputs the reciprocal of `$x-num`
  260. # instead (in other words, `/ $y-num` is equivalent to `/ 1 $y-num`).
  261. #
  262. # Dividing by exact 0 raises an exception. Dividing by inexact 0 results with
  263. # either infinity or NaN according to floating-point semantics.
  264. #
  265. # This command is [exactness-preserving](#exactness-preserving). Additionally,
  266. # when `$x-num` is exact 0 and no `$y-num` is exact 0, the result is exact 0.
  267. #
  268. # Examples:
  269. #
  270. # ```elvish-transcript
  271. # ~> / 2
  272. # ▶ (num 1/2)
  273. # ~> / 2.0
  274. # ▶ (num 0.5)
  275. # ~> / 10 5
  276. # ▶ (num 2)
  277. # ~> / 2 5
  278. # ▶ (num 2/5)
  279. # ~> / 2 5 7
  280. # ▶ (num 2/35)
  281. # ~> / 0 1.0
  282. # ▶ (num 0)
  283. # ~> / 2 0
  284. # Exception: bad value: divisor must be number other than exact 0, but is exact 0
  285. # [tty 6], line 1: / 2 0
  286. # ~> / 2 0.0
  287. # ▶ (num +Inf)
  288. # ```
  289. #
  290. # When given no argument, this command is equivalent to `cd /`, due to the
  291. # implicit cd feature. (The implicit cd feature will probably change to avoid
  292. # this oddity).
  293. #elvdoc:fn % {#rem}
  294. #
  295. # ```elvish
  296. # % $x $y
  297. # ```
  298. #
  299. # Outputs the remainder after dividing `$x` by `$y`. The result has the same
  300. # sign as `$x`.
  301. #
  302. # Examples:
  303. #
  304. # ```elvish-transcript
  305. # ~> % 10 3
  306. # ▶ (num 1)
  307. # ~> % -10 3
  308. # ▶ (num -1)
  309. # ~> % 10 -3
  310. # ▶ (num 1)
  311. # ```
  312. #
  313. # Note that `%` requires both arguments to be within the range of signed
  314. # integers the size of a [machine
  315. # word](https://en.wikipedia.org/wiki/Word_(computer_architecture)), and throws
  316. # an exception otherwise:
  317. #
  318. # ```elvish-transcript
  319. # ~> % (math:pow 2 63) 3
  320. # Exception: wrong type for arg #0: must be integer
  321. # ```
  322. #
  323. # This limit may be lifted in the future.
  324. #elvdoc:fn randint
  325. #
  326. # ```elvish
  327. # randint $low? $high
  328. # ```
  329. #
  330. # Output a pseudo-random integer N such that `$low <= N < $high`. If not given,
  331. # `$low` defaults to 0. Examples:
  332. #
  333. # ```elvish-transcript
  334. # ~> # Emulate dice
  335. # randint 1 7
  336. # ▶ 6
  337. # ```
  338. #elvdoc:fn -randseed
  339. #
  340. # ```elvish
  341. # -randseed $seed
  342. # ```
  343. #
  344. # Sets the seed for the random number generator.
  345. #elvdoc:fn range
  346. #
  347. # ```elvish
  348. # range &step $start=0 $end
  349. # ```
  350. #
  351. # Outputs numbers, starting from `$start` and ending before `$end`, using
  352. # `&step` as the increment.
  353. #
  354. # - If `$start` <= `$end`, `&step` defaults to 1, and `range` outputs values as
  355. # long as they are smaller than `$end`. An exception is thrown if `&step` is
  356. # given a negative value.
  357. #
  358. # - If `$start` > `$end`, `&step` defaults to -1, and `range` outputs values as
  359. # long as they are greater than `$end`. An exception is thrown if `&step` is
  360. # given a positive value.
  361. #
  362. # As a special case, if the outputs are floating point numbers, `range` also
  363. # terminates if the values stop changing.
  364. #
  365. # This command is [exactness-preserving](#exactness-preserving).
  366. #
  367. # Examples:
  368. #
  369. # ```elvish-transcript
  370. # ~> range 4
  371. # ▶ (num 0)
  372. # ▶ (num 1)
  373. # ▶ (num 2)
  374. # ▶ (num 3)
  375. # ~> range 4 0
  376. # ▶ (num 4)
  377. # ▶ (num 3)
  378. # ▶ (num 2)
  379. # ▶ (num 1)
  380. # ~> range -3 3 &step=2
  381. # ▶ (num -3)
  382. # ▶ (num -1)
  383. # ▶ (num 1)
  384. # ~> range 3 -3 &step=-2
  385. # ▶ (num 3)
  386. # ▶ (num 1)
  387. # ▶ (num -1)
  388. # ~> range (- (math:pow 2 53) 1) +inf
  389. # ▶ (num 9007199254740991.0)
  390. # ▶ (num 9007199254740992.0)
  391. # ```
  392. #
  393. # When using floating-point numbers, beware that numerical errors can result in
  394. # an incorrect number of outputs:
  395. #
  396. # ```elvish-transcript
  397. # ~> range 0.9 &step=0.3
  398. # ▶ (num 0.0)
  399. # ▶ (num 0.3)
  400. # ▶ (num 0.6)
  401. # ▶ (num 0.8999999999999999)
  402. # ```
  403. #
  404. # Avoid this problem by using exact rationals:
  405. #
  406. # ```elvish-transcript
  407. # ~> range 9/10 &step=3/10
  408. # ▶ (num 0)
  409. # ▶ (num 3/10)
  410. # ▶ (num 3/5)
  411. # ```
  412. #
  413. # One usage of this command is to execute something a fixed number of times by
  414. # combining with [each](#each):
  415. #
  416. # ```elvish-transcript
  417. # ~> range 3 | each {|_| echo foo }
  418. # foo
  419. # foo
  420. # foo
  421. # ```
  422. #
  423. # Etymology:
  424. # [Python](https://docs.python.org/3/library/functions.html#func-range).