Module FLOAT
Float (F)
Floating-point numbers are a fundamental data type in computing used to represent real numbers (numbers with fractional parts).
They are designed to handle a wide range of magnitudes, from very small to very large values, by using a scientific notation-like format in binary.
Tollerance EPS=0.000001.
Global Float Functions
F.new ([octet]) | Create a new float number. |
F.is_float (data) | Check whether a given Lua value is a floating-point number or a string representation of a floating-point number. |
F.opposite (data) | Find the opposite of a float number. |
Class Float
f:octet () | Convert a float number into an octet. |
f:__eq (num) | Check whether two float numbers are approximately equal within a small tolerance (EPS). |
f:__lt (num) | Check whether one float number is less than another float number. |
f:__add (num) | Perform addition on two float numbers. |
f:__sub (num) | Perform subtraction on two float numbers. |
f:__mul (num) | Perform multiplication on two float numbers. |
f:__div (num) | Perform division on two float numbers. |
f:__mod (m) | Compute the floating-point modulo of two float numbers. |
f:__tostring () | Convert a float number into a string representation. |
Global Float Functions
- F.new ([octet])
-
Create a new float number. If an argument is present,
import it as OCTET and initialise it with its value.
Parameters:
- octet value (optional)
Returns:
-
a new float number
Usage:
--create a new float number n = F.new(1.2343233) print(n) --print: 1.234323
- F.is_float (data)
-
Check whether a given Lua value is a floating-point number or a string representation of a floating-point number.
Parameters:
- data a Lua value (number or string)
Returns:
-
a boolean value (true if data is float)
Usage:
if (F.is_float(1.234)) then print("ok") else print("no") end --print: ok
- F.opposite (data)
-
Find the opposite of a float number.
Parameters:
- data a float number
Returns:
-
the opposite of the float number
Usage:
--create a float number f = F.new(1.234) print(F.opposite(f)) --print: -1.234
Class Float
Object Methods
- f:octet ()
-
Convert a float number into an octet.
Returns:
-
the octet representation of the float number
Usage:
--create a float number f = BIG.new(0.245432) --convert into an octet and print in hex print(f:octet():hex()) --print: 302e323435343332
- f:__eq (num)
-
Check whether two float numbers are approximately equal within a small tolerance (EPS).
Parameters:
- num a float number
Returns:
-
a boolean value
Usage:
--create two float numbers f = F.new(0.24543256) num = F.new(0.24543257) --check if they are equal within EPS if(f:__eq(num)) then print("ok") else print("no") end --print: ok
- f:__lt (num)
-
Check whether one float number is less than another float number.
Parameters:
- num a float number
Returns:
-
a boolean value
- f:__add (num)
-
Perform addition on two float numbers.
Parameters:
- num a float number
Returns:
-
the result of the addition
Usage:
--create two float numbers f = F.new(0.2454325) num = F.new(0.2454326) --make the addition print(f:__add(num)) --print: 0.490865
- f:__sub (num)
-
Perform subtraction on two float numbers.
Parameters:
- num a float number
Returns:
-
the result of the subtraction
Usage:
--create two float numbers f = F.new(1.2342) num = F.new(2.4223) --make the subtraction print(f:__sub(num)) --print: -1.1881
- f:__mul (num)
-
Perform multiplication on two float numbers.
Parameters:
- num a float number
Returns:
-
the result of the multiplication
Usage:
--create two float numbers f = F.new(1.2342) num = F.new(2.4223) --make the multiplication print(f:__mul(num)) --print: 2.989603
- f:__div (num)
-
Perform division on two float numbers.
Parameters:
- num a float number
Returns:
-
the result of the division
Usage:
--create two float numbers f = F.new(1.2342) num = F.new(2.4223) --make the division print(f:__div(num)) --print: 0.509516
- f:__mod (m)
-
Compute the floating-point modulo of two float numbers.
Parameters:
- m the modulus
Returns:
-
the result of the modulo operation
Usage:
--create two float numbers f = F.new(1.43223) m = F.new(0.3423) --apply the modulo operation print(f:__mod(m)) --print: 0.06303
- f:__tostring ()
-
Convert a float number into a string representation.
Returns:
-
the string representation