There are functions for formatting floating-point numbers to E-strings (so that they can be printed) and for decoding floating-point numbers from strings. There are also a number of built-in, floating-point functions which compute some of the less common mathematical functions, such as the various trigonometric functions.

`RealVal(`

`string`)-
This works in a similar way to
`Val`

for extracting integers from a string. The decoded floating-point value is returned as the regular return value, and the number of characters of`string`that were read to make the number is returned as the first optional return value. If a floating-point value could not be decoded from the string then zero is returned as the optional return value and the regular return value will be zero (i.e.,*0.0*). `RealF(`

`e-string`,`float`,`digits`)-
Converts the floating-point value
`float`

into a string which is stored in`e-string`. The number of digits to use after the decimal point is specified by`digits`, which can be zero to eight. The floating-point value is rounded to the specified number of digits. A value of zero for`digits`gives a result with no fractional part and no decimal point. The`e-string`is returned by this function, and this makes it easy to use with`WriteF`

.PROC main() DEF s[20]:STRING, f, i f:=21.60539 FOR i:=0 TO 8 WriteF('f is \s (using digits=\d)\n', RealF(s, f, i), i) ENDFOR ENDPROC

`f`

, to`RealF`

does not need a leading`!`because we are simply passing its value and not performing a computation with it. The program should generate the following output:f is 22 (using digits=0) f is 21.6 (using digits=1) f is 21.61 (using digits=2) f is 21.605 (using digits=3) f is 21.6054 (using digits=4) f is 21.60539 (using digits=5) f is 21.605390 (using digits=6) f is 21.6053900 (using digits=7) f is 21.60539000 (using digits=8)

`Fsin(`

,`float`)`Fcos(`

,`float`)`Ftan(`

`float`)-
These compute the sine, cosine and tangent (respectively) of the supplied
`float`angle, which is specified in radians. `Fabs(`

`float`)-
Returns the absolute value of
`float`, much like`Abs`

does for integers. `Ffloor(`

,`float`)`Fceil(`

`float`)-
The
`Ffloor`

function rounds a floating-point value down to the nearest, whole floating-point value. The`Fceil`

function rounds it up. `Fsqrt(`

`float`)-
Returns the square root of
`float`. `Fpow(`

,`x`,`y`)`Fexp(`

`float`)-
The
`Fpow`

function returns the value of`x`raised to the power of`y`(which are both floating-point values). The`Fexp`

function returns the value of*e*raised to the power of`float`, where*e*is the mathematically special value (roughly*2.718282*). `Raising to a power' is known as**exponentiation**. `Flog10(`

,`float`)`Flog(`

`float`)-
The
`Flog10`

function returns the log to base ten of`float`(the**common logarithm**). The`Flog`

function returns the log to base*e*of`float`(the**natural logarithm**).`Flog10`

and`Fpow`

are linked in the following way (ignoring floating-point inaccuracies):x = Fpow(10.0, Flog10(x))

`Flog`

and`Fexp`

are similarly related (`Fexp`

could be used again, using*2.718282*as the first argument in place of*10.0*).x = Fexp(Flog(x))

Here's a small program which uses a few of the above functions, and shows how to define functions which use and/or return floating-point values.

DEF f, i, s[20]:STRING PROC print_float() WriteF('\tf is \s\n', RealF(s, !f, 8)) ENDPROC PROC print_both() WriteF('\ti is \d, ', i) print_float() ENDPROC /* Square a float */ PROC square_float(f) IS !f*f /* Square an integer */ PROC square_integer(i) IS i*i /* Converts a float to an integer */ PROC convert_to_integer(f) IS Val(RealF(s, !f, 0)) /* Converts an integer to a float */ PROC convert_to_float(i) IS RealVal(StringF(s, '\d', i)) /* This should be the same as Ftan */ PROC my_tan(f) IS !Fsin(!f)/Fcos(!f) /* This should show float inaccuracies */ PROC inaccurate(f) IS Fexp(Flog(!f)) PROC main() WriteF('Next 2 lines should be the same\n') f:=2.7; i:=!f! print_both() f:=2.7; i:=convert_to_integer(!f) print_both() WriteF('Next 2 lines should be the same\n') i:=10; f:=i! print_both() i:=10; f:=convert_to_float(i) print_both() WriteF('f and i should be the same\n') i:=square_integer(i) f:=square_float(f) print_both() WriteF('Next 2 lines should be the same\n') f:=Ftan(.8) print_float() f:=my_tan(.8) print_float() WriteF('Next 2 lines should be the same\n') f:=.35 print_float() f:=inaccurate(f) print_float() ENDPROC

The `convert_to_integer`

and `convert_to_float`

functions perform similar conversions to those done by `!` when it occurs in an expression.
To make things more explicit, there are a lot of unnecessary uses of `!`, and these are when `f`

is passed directly as a parameter to a function (in these cases, the `!` could safely be omitted).
All of the examples have the potential to give different results where they ought to give the same, and this is due to the inaccuracy of floating-point numbers.
The last example has been carefully chosen to show this.

Go to the Next or Previous section, the Detailed Contents, or the Amiga E Encyclopedia.