This next example object demonstrates using integer constants (iB, iC, and iD) that are pre-translated to pseudo-real numbers, floating-point constants (B, C, and D) used in their native form by the FloatMath and FloatString library objects, and also those same floating-point constants translated to pseudo-real numbers at compile time.
{{ RealNumbers.spin}}
CON _clkmode = xtal1 + pll16x _xinfreq = 5_000_000 iB = 760 'Integer constants iC = 3875 iD = 1250 B = 7.6 'Floating-point constants C = 38.75 D = 12.5 K = 100.0 'Real-to-Pseudo-Real multiplier
OBJ Term : "TV_Terminal" F : "FloatMath" FS : "FloatString"
PUB Math Term.Start(12) {Integer constants (real numbers * 100) to do fast integer math} Term.Str(string("Pseudo-Real Number Result: ")) Term.Dec(iB*iC/iD) {Floating-point constants using FloatMath and FloatString objects} Term.Out(13) Term.Str(string("Floating-Point Number Result: ")) Term.Str(FS.FloatToString(F.FDiv(F.FMul(B, C), D))) {Floating-point constants translated to pseudo-real for fast math} Term.Out(13) Term.Str(string("Another Pseudo-Real Number Result: ")) Term.Dec(trunc(B*K)*trunc(C*K)/trunc(D*K))
Pseudo-Real Number Result: 2356
Floating-Point Number Result: 23.56
Another Pseudo-Real Number Result: 2356
The pseudo-real results, of course, each represent the value 23.56 but the entire value is shifted upwards by two digits to maintain integer math compatibility. With some additional code we could output it as 23.56 for display purposes.
The constants iB, iC, and iD are standard integer constants as we’ve seen before, but their values are really pseudo-real numbers representing the values in our example equation.
The constants B, C, D, and K, are floating-point constants (real numbers). The compiler automatically recognizes them as such and stores them in 32-bit single-precision floating-point format. They can be used in other compile-time floating-point expressions directly but at run time they should only be used with floating-point methods such as those found in the FloatMath and FloatString objects.
The statement Term.Dec(iB*iC/iD) uses the pre-translated pseudo-real constants as suggested by the Pseudo-Real Numbers technique, above. This is evaluated about 1.6 times faster than with the floating-point technique and takes much less code space.
The statement Term.Str(FS.FloatToString(F.FDiv(F.FMul(B, C), D))) calls FloatMath’s FMul method to multiply the floating-point values B and C, then calls FloatMath’s FDiv method to divide that result by the floating-point value D, translates the result to a string using FloatString’s FloatToString method and displays that on the TV.
The statement Term.Dec(trunc(B*K)*trunc(C*K)/trunc(D*K)) uses compile-time expressions inside of TRUNC directives to shift the floating-point constants B, C, and D upwards by two digits and truncate the values to integers. The resulting expression is equivalent to that of the first pseudo-real number equation Term.Dec(iB*iC/iD) but has the added benefit of allowing its component values to be defined in floating-point terms.
The TRUNC directive truncates fully resolved floating-point expressions to their integer form at compile time. It is required here since floating-point constant values can not be used directly by run-time expressions.
Propeller Help Version 1.1
Copyright © Parallax Inc.
5/13/2009