Since a floating-point number is stored in a
LONG variable it would normally be interpreted as an integer, and this interpretation will generally not give a number anything like the intended floating-point number.
To use floating-point numbers in expressions you must use the (rather complicated) floating-point conversion operator, which is the ! character.
This converts expressions and the normal maths and comparison operators to and from floating-point.
All expressions are, by default, integer expressions.
That is, they represent
LONG integer values, rather than floating-point values.
The first time a ! occurs in an expression the value of the expression so far is converted to floating-point and all the operators and variables after this point are considered floating-point.
The next time it occurs the (floating-point) value of the expression so far is converted to an integer, and the following operators and variables are considered integer again.
You can use ! as often as necessary within an expression.
Parts of an expression in parentheses are treated as separate expressions, so are, by default, integer expressions (this, includes function call arguments).
The integer/floating-point conversions performed by ! are not simple. They involve rounding and also bounding. Conversion, for example, from integer to floating-point and back again will generally not result in the original integer value.
Here's a few commented examples, where
f always holds a floating-point number, and
j always hold integers:
DEF f, i, j i:=1 f:=1.0 f:=i! -> i converted to floating-point (1.0) f:=6.2 i:=!f! -> the expression f is floating-point, -> then converted to integer (6)
In the first assignment, the integer value one is assigned to
In the second, the floating-point value 1.0 is assigned to
The expression on the right-hand side of third assignment is considered to be an integer until the ! is met, at which point it is converted to the nearest floating-point value.
f is assigned the floating-point value of one (i.e., 1.0), just like it is by the second assignment.
The expression in the final assignment needs to start off as floating-point in order to interpret the value stored in
f as floating-point.
The expression finishes by converting back to integer.
The overall result is to turn the floating-point value of
f into the nearest integer (in this case, six).
The assignments below are more complicated, but should be straight-forward to follow.
f always holds a floating-point number, and
j always hold integers.
f:=!f*f -> the whole expression is floating-point, -> and f is squared (6.2*6.2) f:=!f*(i!) -> the whole expression is floating-point, -> i is converted to floating-point and -> multiplied by f j:=!f/(i!)! -> the whole division is floating-point, -> with the result converted to integer j:=!f!/i -> floating-point f is converted to integer -> and is (integer) divided by i IF !f<230.0 THEN RETURN 0 -> floating-point comparison < IF !f>(i!) THEN RETURN 0 -> i converted to floating-point, -> then compared to f
If the ! were omitted from the first assignment, then not only would the value in
f be interpreted (incorrectly) as integer, but the multiplication performed would be integer multiplication, rather than floating-point.
In the second assignment, the parentheses around the expression involving
i are crucial.
Without the parentheses the value stored in
i would be interpreted as floating-point.
This would be wrong because
i actually stores an integer value, so parentheses are used to start a new expression (which defaults to being integer).
The value of
i is then interpreted correctly, and finally converted to floating-point (by the ! just before the closing parenthesis).
The (floating-point) multiplication then takes place with two floating-point values, and the result is stored in
In the last two assignments (using division),
j is assigned roughly the same value.
However, the expression in the first assignment allows for greater accuracy, since it uses floating-point division.
This means the result will be rounded, whereas it is truncated when integer division is used.
One important thing to know about floating-point numbers in E is that the following assignments store the same value in
f stores a floating-point number).
This is because no computation is performed and no conversion happens: the value in
f is simply copied to
This is especially important for function calls, as we shall see in the next section.
Strictly speaking, however, the second version is better, since it shows (to the reader of the code) that the value in
f is meant to be floating-point.
Go to the Next or Previous section, the Detailed Contents, or the Amiga E Encyclopedia.