|
Post by Pjot on Jun 2, 2014 20:42:00 GMT 1
OK, in the meantime the optional TYPE keyword for functions was added, the following should work with the latest beta: FUNCTION binom(FLOATING n, FLOATING r, FLOATING p) TYPE double
LOCAL coeff = 1 TYPE FLOATING
FOR i = 1 TO r coeff = coeff * (n - i + 1) / i NEXT i
RETURN coeff * POW(p, r) * POW(1-p, n-r)
END FUNCTION
PRINT binom(9, 5, 0.5) FORMAT "%.8f"
The '%' and '#' suffixes are more work to implement and will take longer time. But I'll post it here when done. BR Peter
|
|
|
Post by bitvast on Jun 2, 2014 21:46:03 GMT 1
Works as advertised. I made a slight amendment on the 2nd line. FUNCTION binom(FLOATING n, FLOATING r, FLOATING p) TYPE FLOATING LOCAL coeff = 1.0 FOR i = 1 TO r coeff = coeff * (n - i + 1) / i NEXT i RETURN coeff * POW(p, r) * POW(1-p, n-r) END FUNCTION
PRINT binom(12, 6, 0.5) FORMAT "%.8f" I think it's the case that if you a initialise a variable with a value containing a decimal point, there's no need to explicitly declare it as a float?
|
|
|
Post by Pjot on Jun 3, 2014 19:54:39 GMT 1
No, you have to add the float declaration, otherwise the variable will be of 'long' type. Your example seems to work, but this is luck, because coeff has assigned the value 1, which happens to be the same in case of a float or integer.
|
|
|
Post by Pjot on Jun 8, 2014 8:35:15 GMT 1
So I have added preliminary support for the type suffixes. By default, the '%' denotes a long type, and the '#' indicates float. The following program now works: FUNCTION binom#(FLOATING n, FLOATING r, FLOATING p)
LOCAL coeff = 1 TYPE FLOATING
FOR i = 1 TO r coeff = coeff * (n - i + 1) / i NEXT i
RETURN coeff * POW(p, r) * POW(1-p, n-r)
END FUNCTION
PRINT binom#(9, 5, 0.5) FORMAT "%.8f"
Declarations and assignments work, also for arrays. Only support for implicitly assigned values needs to be added. Latest beta here. BR Peter
|
|
|
Post by bitvast on Jun 9, 2014 15:24:59 GMT 1
Cool, thanks Peter.
|
|
|
Post by alexfish on Jun 24, 2014 15:21:42 GMT 1
Wrong post
|
|