Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I'm adding SQR to my new faster string math routines...
#7
Okay, this should fix that exception for single digit floating points...

Code: (Select All)
WIDTH 160, 42
_SCREENMOVE 0, 0
DO
    betatest% = -1
    loops# = 29
    INPUT "Number: "; x#
    IF x# = 0 THEN SYSTEM
    IF x# < 0 THEN PRINT "Negatives not allowed. Redo..": _DELAY 3: RUN

    oldy$ = ""
    x$ = LTRIM$(STR$(x#))

    IF INSTR(x$, ".") THEN
        decx$ = MID$(x$, 1, INSTR(x$, ".") - 1)
        x$ = MID$(x$, 1, INSTR(x$, ".") - 1) + MID$(x$, INSTR(x$, ".") + 1)
        IF LEN(x$) = 1 THEN x$ = x$ + "0"
    ELSE
        decx$ = x$
    END IF

    j&& = LEN(decx$)
    a$ = RIGHT$(LTRIM$(STR$(j&&)), 1)

    IF VAL(a$) / 2 = VAL(a$) \ 2 THEN
        i&& = 1 ' Even number length.
    ELSE
        i&& = 0 ' Odd number length.
    END IF

    DO
        a$ = (MID$(x$, i&&, 2))
        z$ = LTRIM$(STR$(VAL(z$) - k#)) + a$

        oldy$ = ""
        FOR j&& = 1 TO 10
            IF i&& > 1 THEN y$ = LTRIM$(STR$(VAL(sqrt$) * 2)) + LTRIM$(STR$(j&&)) ELSE y$ = LTRIM$(STR$(j&&))
            REM COLOR 8, 0: PRINT "y$ = "; y$, VAL(y$) * j&&: COLOR 7, 0
            IF VAL(y$) * j&& >= VAL(z$) THEN
                IF VAL(y$) * j&& = VAL(z$) THEN
                    h% = 0: oldy$ = y$ ' Perfect square division.
                ELSE
                    h% = 1
                END IF

                IF VAL(z$) = 0 AND VAL(oldy$) * (j&& - h%) = 0 AND i&& >= LEN(decx$) THEN EXIT DO
                IF dp&& = 0 THEN IF i&& >= LEN(decx$) THEN dp&& = INT(LEN(decx$) / 2 + .5): IF dp&& = 0 THEN dp&& = -1

                IF betatest% THEN PRINT "Sqrt "; sqrt$; " * 2 = ";: COLOR 2, 0: PRINT LTRIM$(STR$(VAL(sqrt$) * 2));: COLOR 7, 0: PRINT LTRIM$(STR$(j&& - h%)); " * "; LTRIM$(STR$(j&& - h%)); " ="; VAL(oldy$) * (j&& - h%)
                sqrt$ = sqrt$ + LTRIM$(STR$(j&& - h%))
                k# = VAL(oldy$) * (j&& - h%)
                REM PRINT "oldy$ = "; oldy$; " h% ="; h%; "j&&-h% ="; j&& - h%, "k# ="; k#, "sqrt = "; sqrt$
                IF betatest% THEN PRINT "Remainder "; z$; " minus"; k#; "= ";
                EXIT FOR
            END IF
            oldy$ = y$
        NEXT

        IF betatest% THEN PRINT LTRIM$(STR$(VAL(z$) - k#)); " sqrt = "; sqrt$

        i&& = i&& + 2
        IF i&& > loops# THEN EXIT DO
        x$ = x$ + "00"
    LOOP

    PRINT

    IF dp&& THEN
        sqrt$ = MID$(sqrt$, 0, dp&& + 1) + "." + MID$(sqrt$, dp&& + 1)
    END IF

    PRINT "QB64 SQR:"; SQR(x#)
    PRINT "Pete SQR: "; sqrt$: _DELAY 1: CLEAR
    PRINT
LOOP
Reply


Messages In This Thread
RE: I might add SQR to my string math routines... - by Pete - 08-18-2022, 03:04 AM



Users browsing this thread: 5 Guest(s)