| Message |
Operations supported by the bc library are:
- bc.number (n) - make a new "big" number. eg.
i = bc.number (2)
i = i ^ 50
print (i) --> 1267650600228229401496703205376
The argument can be a string, eg.
PI = bc.number ("3.141592653589793238462643383279502884197")
- bc.digits (n) - do subsequent calculations to n digits after the decimal point. eg.
i = bc.sqrt (2)
print (i) --> 1
bc.digits (40)
i = bc.sqrt (2)
print (i) --> 1.4142135623730950488016887242096980785696
bc.digits returns what the number of digits was previously (so you could save and restore it).
- bc.compare (a, b) - returns -1 if a < b, 0 if a == b, and 1 if a > b. eg.
print (bc.compare (1, 2)) --> -1
print (bc.compare (2, 2)) --> 0
print (bc.compare (3, 2)) --> 1
- bc.add (a, b) -- adds a + b
- bc.sub (a, b) -- subtracts a - b
- bc.div (a, b) -- divides a / b
- bc.mod (a, b) -- returns a modulus b
- bc.mul (a, b) -- multiplies a * b
- bc.isneg (a) -- returns true if a is negative, false otherwise
- bc.iszero (a) -- returns true if a is zero, false otherwise
- bc.pow (a, b) -- raises a to the power b
- bc.sqrt (a) -- returns the square root of a
- bc.tostring (a) -- converts its argument to a string (eg. for printing)
When you create a "big number" with bc.number, you get a userdata with a metatable associated with it. The metatable supports the following operations:
- __add
- __div
- __eq
- __lt
- __mul
- __pow
- __sub
- __tostring
- __unm
This means you can directly add, subtract, divide etc. big numbers (eg. a = b + c) without having to use functions like bc.add.
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | top |
|