Issue-#147 Fraction function in System Math

Merged to the master branch
User avatar
Robert
Posts: 1024
Joined: Sat Sep 28, 2013 11:04 am
Location: Edinburgh, Scotland

Issue-#147 Fraction function in System Math

Post 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
User avatar
Josef Templ
Posts: 2047
Joined: Tue Sep 17, 2013 6:50 am

Re: Issue-#147 Fraction function in System Math

Post 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
User avatar
Robert
Posts: 1024
Joined: Sat Sep 28, 2013 11:04 am
Location: Edinburgh, Scotland

Re: Issue-#147 Fraction function in System Math

Post 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.
User avatar
Robert
Posts: 1024
Joined: Sat Sep 28, 2013 11:04 am
Location: Edinburgh, Scotland

Re: Issue-#147 Fraction function in System Math

Post 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.
Ivan Denisov
Posts: 1700
Joined: Tue Sep 17, 2013 12:21 am
Location: Russia

Re: Issue-#147 Fraction function in System Math

Post 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.
User avatar
Robert
Posts: 1024
Joined: Sat Sep 28, 2013 11:04 am
Location: Edinburgh, Scotland

Re: Issue-#147 Fraction function in System Math

Post 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.
Ivan Denisov
Posts: 1700
Joined: Tue Sep 17, 2013 12:21 am
Location: Russia

Re: Issue-#147 Fraction function in System Math

Post 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.
User avatar
Robert
Posts: 1024
Joined: Sat Sep 28, 2013 11:04 am
Location: Edinburgh, Scotland

Re: Issue-#147 Fraction function in System Math

Post 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.
Ivan Denisov
Posts: 1700
Joined: Tue Sep 17, 2013 12:21 am
Location: Russia

Re: Issue-#147 Fraction function in System Math

Post 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.
User avatar
Josef Templ
Posts: 2047
Joined: Tue Sep 17, 2013 6:50 am

Re: Issue-#147 Fraction function in System Math

Post 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
Post Reply