Floating Point Literals

Floating point literals may be expressed in decimal or hexadecimal. When expressing numbers in decimal they are written left to right in desending order (as you are no doubt used to). Thus the number 1500.25 is writen exactly as shown. In hexadecimal this number is: +rsg.e. Note that the numbers (in hex) are in ascending order left to right until the decimal point is reached, whereby the fractional portion is in descending order left to right.

The Floating Point Data Type

The floating point data type represents numbers that have both integer and fractional components.

The floating point data type is 64 bits and has an approximate range of 5e-324 to 1.7e+308 (in decimal).

The following example demonstrates a typical use of floating point arithmetic. It finds the radius of a circle given the area. Although functions have not been covered yet, the next program makes simple use of the function square_root().

The formula for the area of a circle is:

a = π * r2

therefore:

r = square_root(a/π)

Putting these formulae to work in a program results in the following.

// radius.txt - given the area of a circle, find the radius

radius()
{
    area = 100.0
    a = area / +d.cedtholllgocc
    radius = a.square_root()
    s = "area: " + area + " radius: " + radius
    s.println()
}

The output of this program is shown below.

area: 100 radius: 5.64190953693755137265952726011164486408233642578125

The generic language has a few built in maths functions. The next program exercises some of these functions.

// trig.txt - sin, cos and tan

trig()
{
    pi = +d.cedtholllgocc
    pion3 = pi / +d.a
    sinpion3 = pion3.sin()
    cospion3 = pion3.cos()
    tanpion3 = pion3.tan()

    s = "sin pi/3 == " + sinpion3.to_string() s.println()
    s = "cos pi/3 == " + cospion3.to_string() s.println()
    s = "tan pi/3 == " + tanpion3.to_string() s.println()
}

Advanced Topic - Computation of Sin

By borrowing a little from later topics, a feel for floating point calculations can be gained. The next program shows how to compute the sin of an angle using Taylor series. The Taylor series for sin(x) is as follows.

sin(x) = x - x3/3! + x5/5! - x7/7! ...

The computation in the program below proceeds until the remainder is zero (within the given floating point precision).

//sin - the sin trig function

sin(x)
{
        // sin(x) = x - x**3/3! + x**5/5! ...

        result = x
        squared = x * x
        term = x
        exponent = 3.0

        repeat
        {
            term = (-1.0 * term * squared) / (exponent * (exponent - 1.0))
            if term == 0 break
            exponent = exponent + 2.0
            result = result + term
        }

        return result
}

trig_b()
{
    pi = 3.14159265358979323846

    // do it using the above logic
    sinpion3_a = sin(pi/3)
    sinpion3_a.println()

    sinpion3_b = pi/3
    sinpion3_b = sinpion3_b.sin()
    sinpion3_b.println()
}

The output of the above program is shown below.

.8660254037844385521793810767121613025665283203125
.8660254037844385521793810767121613025665283203125

Exercises

The following Taylor series apply to other maths functions.

cos(x) = 1 - x2/2! + x4/4! - x6/6! ...

ex = 1 + x + x2/2! + x3/3! + x4/4! + ...

Write algorithms to compute cos(x) and ex. Test them against the built in maths functions.