Exponential Notation Routine in Just Basic

(c) 2015 by Barton Paul Levenson


Language:
Basic

Dialect:
Just Basic (by Shoptalk Systems)

Discussion:
This routine returns a string representation of a real number in scientific (exponential) notation. There is no preset routine to do this in Just Basic, as neither the Format nor the Print Using routines support scientific notation properly, or at all.

Parameters: x is the number to be printed. signif is the desired number of significant digits. exDigits is the desired number of digits in the exponent.


    ' log10 returns the common (base-ten) logarithm of x.
    function log10(x)
        log10 = log(x) / log(10)
    end function ' log10



    ' sci$ does the conversion.
    function sci$(x, signif, exDigits)
        k = int(log10(abs(x)))
        ratio = x / (10 ^ k)

        fmt$ = "##."
        for i = 1 to (signif - 1)
            fmt$ = fmt$ + "#"
        next i

        if k < 0 then
            sign$ = "-"
            ratio = ratio * 10
            k = abs(k) + 1
        else
            sign$ = "+"
        end if

        mant$ = using(fmt$, ratio)
        ex$ = "e" + sign$

        exNum$ = Str$(k)
        while len(exNum$) < exDigits
            exNum$ = "0" + exNum$
        wend

        sci$ = mant$ + ex$ + exNum$
    end function ' sci$


Page created:01/05/2015
Last modified:  01/05/2015
Author:BPL