Page 1 of 4

Issue-#147 Fraction function in System Math

Posted: Wed Feb 22, 2017 8:22 pm
by Robert
For the issue see https://redmine.blackboxframework.org/issues/147.

I propose adding a new function to System Math. This function would satisfy the equation
x = Floor(x) + Fraction(x) in a similar way that the existing function Frac satisfies x = Trunc(x) + Frac(x).

The changes to Mod/Math, Mod/SMath, Docu/Math & Rsrc/Strings are in the panel below.
This has worked reliably for me for years.

Code: Select all

	PROCEDURE Fraction* (x: REAL): REAL;
	BEGIN
		(* 20, x # INF  &  x # -INF *)
		FLD(x); FLDst0; FRNDINT;
		FCOM; FSWax; SAHF; JBE4; FLD1; FSUB;
		FSUB; WAIT; RETURN TOP()
	END Fraction;


	PROCEDURE Fraction* (x: SHORTREAL): SHORTREAL;
	BEGIN
		(* 20, x # INF  &  x # -INF *)
		FLD(x); FLDst0; FRNDINT;
		FCOM; FSWax; SAHF; JBE4; FLD1; FSUB;
		FSUB; WAIT; RETURN TOP()
	END Fraction;


		PROCEDURE Fraction (x: REAL): REAL;

You could easily implement the functions Sign, Floor, Ceiling, Trunc, Frac, Fraction, and Round yourself, using the ENTIER standard function. 


PROCEDURE Fraction (x: REAL): REAL
Frac is the fractional part of the argument.
The following equation holds: x = Floor(x) + Fraction(x), within the limits of 64-bit floating point arithmetic.

Pre
x # INF  &  x # -INF	20


Math.Fraction.143	Pre: x # INF  &  x # -INF

Re: Issue-#147 Fraction function in System Math

Posted: Thu Feb 23, 2017 7:51 am
by Josef Templ
The only problem I see is the naming.
We would get two functions (Frac and Fraction) with similar names
where the name does not indicate which function to choose.

Can we find a more expressive name for the new function?

Just a bold idea:
Isn't the new Fraction function like a floating point MOD operation
with modulus 1?
A name such as Mod1 may then be a candidate.

- Josef

Re: Issue-#147 Fraction function in System Math

Posted: Thu Feb 23, 2017 8:43 am
by Robert
Josef Templ wrote:Just a bold idea:
Isn't the new Fraction function like a floating point MOD operation
with modulus 1?
A name such as Mod1 may then be a candidate
I like it. In fact I prefer it as it is both shorter and less confusing.

Re: Issue-#147 Fraction function in System Math

Posted: Thu Feb 23, 2017 8:48 am
by Robert
The last line in the window in my first post is the addition to System Strings:
Math.Fraction.143 Pre: x # INF & x # -INF
I haven't added the corresponding line for SMath. Should I?

Should we also add all the other SMath error codes? - I have not investigated this yet.

Re: Issue-#147 Fraction function in System Math

Posted: Thu Feb 23, 2017 9:06 am
by Ivan Denisov
The only problem I see is the interface compatibility with 1.7. So this can not be the issue for 1.7.1.

From my point of view, we should publish 1.7.1 ASAP and then we can adopt the features like this one.

Re: Issue-#147 Fraction function in System Math

Posted: Thu Feb 23, 2017 9:11 am
by Robert
Robert wrote:Should we also add all the other SMath error codes? - I have not investigated this yet.
I think we should.


Ivan - I don't understand why this proposal introduces any incompatibility.

Re: Issue-#147 Fraction function in System Math

Posted: Thu Feb 23, 2017 9:15 am
by Ivan Denisov
Robert wrote:Ivan - I don't understand why this proposal introduces any incompatibility.
Extra exported procedure will change the module fingerprint, so all modules importing Math should be recompiled. This is not allowed for "patch" version.

Re: Issue-#147 Fraction function in System Math

Posted: Thu Feb 23, 2017 9:27 am
by Robert
Ivan Denisov wrote:Extra exported procedure will change the module fingerprint, so all modules importing Math should be recompiled. This is not allowed for "patch" version.
Are you sure? I know that an extra exported Method will do this, but for a static procedure I don't observe a problem.

Re: Issue-#147 Fraction function in System Math

Posted: Thu Feb 23, 2017 12:34 pm
by Ivan Denisov
Robert wrote:
Ivan Denisov wrote:Extra exported procedure will change the module fingerprint, so all modules importing Math should be recompiled. This is not allowed for "patch" version.
Are you sure? I know that an extra exported Method will do this, but for a static procedure I don't observe a problem.
I am not sure... if you are right, there will be no problem.

Re: Issue-#147 Fraction function in System Math

Posted: Thu Feb 23, 2017 6:57 pm
by Josef Templ
Adding an exported global procedure does not introduce any incompatibility in CP.
Every exported global procedure is fingerprinted individually. There is no global module
finger print as it was used in older Oberon compilers.

- Josef