Thanks @reyalp - userdata makes much more sense.
@pigeonhill - attached is a build for the M3 1.20f.
I've also attached a patch of the code changes (includes a test script).
I'm not a Lua expert so this could probably be improved in many ways; but it seems to work.
The usage is a bit different to what I posted earlier:
Create a new fmath double variable:
x = fmath.new(n,d)
Set x to n / d.
E.G.
x = fmath.new(45,10) sets x = 4.5
y = fmath.new(25,100) sets y = 0.25
Parameter d is optional and defaults to 1 - x = fmath.new(5) is equivalent to x = fmath.new(5,1)
Arithmetic - use standard operators.
E.G.
z = x + y
z = x - y
z = x * y
z = x / y
z = -x
z = x ^ y (power function)
So long as either parameter is an fmath double the result will be an fmath double. The following are both valid:
z = x + 1
z = 1 + x
Comparison - as above use standard operators ==, ~=, <, <=, >, >=
Math functions:
log, log2, log10, sqrt
Can be called via fmath library or object oriented syntax. The following are equivalent:
y = fmath.log(x) ==> y = x:log()
y = fmath.sqrt(x) ==> y = x:sqrt();
Conversion:
int, ceil, floor, round, deg, rad
These can also be called via the fmath library or object oriented syntax as above.
The int, ceil, floor and round functions return integer values not fmath doubles.
The deg and rad functions convert between degrees and radians and return an fmath double value.
There is also a function to convert to a string:
fmath.tostr(x, format) or x:tostr(format).
This returns a string of the double value using the supplied format string. The format parameter is optional and defaults to "%f".
The frac function from imathlib is not implemented. The imathlib function is mathematically wrong for negative values.
You can get the equivalent of the imathlib frac function using:
f = (x - x:int()) * 1000
The correct value would be:
f = (x - x:floor()) * 1000
Edit: attachments updated to include comparison operators and unary minus operator.