pxt-calliope/olddocs/js/math.md

102 lines
2.7 KiB
Markdown
Raw Normal View History

2016-03-26 00:47:20 +01:00
# Math Library
2016-04-02 01:22:47 +02:00
Functions in the math library.
2016-03-26 00:47:20 +01:00
### @parent js/language
2016-04-13 17:27:45 +02:00
The math library includes math related functions that you can use with [Numbers](/reference/types/number).
2016-03-26 00:47:20 +01:00
2016-04-13 17:27:45 +02:00
* In the [TouchDevelop editor](/js/editor), click `math` to access the functions described below
* In the [Block editor](/blocks/editor), click **maths** on the left to see the available blocks
2016-03-26 00:47:20 +01:00
The functions available in Touch Develop are:
### abs
2016-04-13 17:27:45 +02:00
math `->` abs (x : [Number](/reference/types/number)) *returns* [Number](/reference/types/number)
2016-03-26 00:47:20 +01:00
returns the absolute value of input parameter `x`
```
basic.showNumber(math.abs(-7), 150)
```
### clamp
2016-04-13 17:27:45 +02:00
math `->` clamp (min : [Number](/reference/types/number), max : [Number](/reference/types/number), value : [Number](/reference/types/number)) *returns* [Number](/reference/types/number)
2016-03-26 00:47:20 +01:00
limits a number to a range (between a min and max number); returns `min` if `value` is < `min`; returns `max` if `value` is > `max`; otherwise, returns `value`.
```
basic.showNumber(td.clamp(5, 9, 12), 150)
```
### max
2016-04-13 17:27:45 +02:00
math `->` max (x : [Number](/reference/types/number), y : [Number](/reference/types/number)) *returns* [Number](/reference/types/number)
2016-03-26 00:47:20 +01:00
returns the larger of two input numbers (`x` and `y`)
```
basic.showNumber(Math.max(9, 7), 150)
```
### min
2016-04-13 17:27:45 +02:00
math `->` min (x : [Number](/reference/types/number), y : [Number](/reference/types/number)) *returns* [Number](/reference/types/number)
2016-03-26 00:47:20 +01:00
returns the smaller of two input numbers (`x` and `y`)
```
basic.showNumber(Math.min(9, 7), 150)
```
### mod
2016-04-13 17:27:45 +02:00
math `->` mod (x : [Number](/reference/types/number), y : [Number](/reference/types/number)) *returns* [Number](/reference/types/number)
2016-03-26 00:47:20 +01:00
returns the integer modulus/remainder resulting from the division of the number `x` by the number `y`
```
basic.showNumber(math.mod(9, 7), 150)
```
### pow
2016-04-13 17:27:45 +02:00
math `->` pow (base : [Number](/reference/types/number), exp : [Number](/reference/types/number)) *returns* [Number](/reference/types/number)
2016-03-26 00:47:20 +01:00
returns the value of the number `base` raised to the power `exp`
```
basic.showNumber(math.pow(3, 3), 150)
```
### random
2016-04-13 17:27:45 +02:00
math `->` random (limit : [Number](/reference/types/number)) *returns* [Number](/reference/types/number)
2016-03-26 00:47:20 +01:00
2016-04-13 17:27:45 +02:00
returns a random [Number](/reference/types/number) between 0 and the parameter *limit* - 1
2016-03-26 00:47:20 +01:00
```
basic.showNumber(Math.random(10), 150)
```
### sign
2016-04-13 17:27:45 +02:00
math `->` sign (x : [Number](/reference/types/number)) *returns* [Number](/reference/types/number)
2016-03-26 00:47:20 +01:00
returns the sign of input parameter `x`
```
basic.showNumber(math.sign(-7), 150)
```
### Lessons
2016-04-13 17:27:45 +02:00
[flipping bird](/lessons/flipping-bird), [catch the egg game](/lessons/catch-the-egg-game)
2016-03-26 00:47:20 +01:00
### See also
2016-04-13 17:27:45 +02:00
[Bits library](/js/bits), [TouchDevelop documentation](/js/contents), [Number](/reference/types/number)
2016-03-26 00:47:20 +01:00