Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Old code needs fixing (problem solved)
#1
I got this from Steve at old forum and tested it today on problem encountered by Cortez at another forum.

It does not work:
Code: (Select All)
' !!!!    2024-09-27 this does not work
' test
Print N2S$("1E6")
Print N2S$("10E6")
Print N2S$("100E6")
Print N2S$("1E-6")
Print N2S$("10E-6")
Print N2S$("100E-6")
Function N2S$ (EXP$) 'remove scientific Notation to String (~40 LOC)
    'SMcNeill Jan 7, 2020 ref: https://www.qb64.org/forum/index.php?topic=1555.msg112989#msg112989
    'Last Function in code marked Best Answer (removed debug comments and blank lines added these 2 lines.)
    ReDim t$, sign$, l$, r$, r&&
    ReDim dp As Long, dm As Long, ep As Long, em As Long, check1 As Long, l As Long, i As Long
    t$ = LTrim$(RTrim$(EXP$))
    If Left$(t$, 1) = "-" Or Left$(t$, 1) = "N" Then sign$ = "-": t$ = Mid$(t$, 2)
    dp = InStr(t$, "D+"): dm = InStr(t$, "D-")
    ep = InStr(t$, "E+"): em = InStr(t$, "E-")
    check1 = Sgn(dp) + Sgn(dm) + Sgn(ep) + Sgn(em)
    If check1 < 1 Or check1 > 1 Then N2S = _Trim$(EXP$): Exit Function 'If no scientic notation is found, or if we find more than 1 type, it's not SN!
    Select Case l 'l now tells us where the SN starts at.
        Case Is < dp: l = dp
        Case Is < dm: l = dm
        Case Is < ep: l = ep
        Case Is < em: l = em
    End Select
    l$ = Left$(t$, l - 1) 'The left of the SN
    r$ = Mid$(t$, l + 1): r&& = Val(r$) 'The right of the SN, turned into a workable long
    If InStr(l$, ".") Then 'Location of the decimal, if any
        If r&& > 0 Then
            r&& = r&& - Len(l$) + 2
        Else
            r&& = r&& + 1
        End If
        l$ = Left$(l$, 1) + Mid$(l$, 3)
    End If
    Select Case r&&
        Case 0 'what the heck? We solved it already?
            'l$ = l$
        Case Is < 0
            For i = 1 To -r&&
                l$ = "0" + l$
            Next
            l$ = "." + l$
        Case Else
            For i = 1 To r&&
                l$ = l$ + "0"
            Next
            l$ = l$
    End Select
    N2S$ = sign$ + l$
End Function

Anyone have a better way of doing this?

Cortez was testing Val() instead of N2S$() which didn't work either. Val has a bug also.
b = b + ...
Reply
#2
I'm looking at it. Is it taken into account that Check 1 could be negative? How could that ever happen? After all, instr returns 0 or a positive value, so SGN is 0 or 1. Well, I keep looking, just apparently guessing what is expected from it.


Reply
#3
If I understood the explanation correctly, scientific notation should be displayed in a "normal" representation. How about that?

Code: (Select All)

Print Using "###.######"; 100E-6

Print Using "**###.######"; 100E-6

Print Using "###.######^^^^^"; 000.000100

Print Using "**###.######"; 99.999997E-6
Reply
#4
Print Using requires a number not string, could it do 1.23D-20?

Looks like it throws in some BS on the end:
Code: (Select All)
print using "###.#######################################################################################################";1.23D-20

maybe Print Using combined with string("#", D or E amounts) in a function?


Oh maybe the left number is not expected to be above 9.xxxx... ie < 10.000... and also > 1.0000... for proper Sci Notation.
b = b + ...
Reply
#5
It works!

[Image: Wissenschaft-Format2024-09-27.jpg]
Reply
#6
The problem was not using proper Sci Notation. Angry

First number must be between 1 and < 10 and you have to use + for positive powers of 10.

Code: (Select All)
Print Val("1.1E+6")
Print Val("1.01E+7")
Print Val("1.001E+8")
Print Val("1.E-6")
Print Val("1.1E-6")
Print Val("1.01E-6")
Perfect

Code: (Select All)
_Title "N2S$ Test Steve's converter for number display" ' b+ 2022-11-10

' test
Print N2S$("1.1E+6")
Print N2S$("1.01E+7")
Print N2S$("1.001E+8")
Print N2S$("1.E-6")
Print N2S$("1.1E-6")
Print N2S$("1.01E-6")


Function N2S$ (EXP$) 'remove scientific Notation to String (~40 LOC)
    'SMcNeill Jan 7, 2020 ref: https://www.qb64.org/forum/index.php?topic=1555.msg112989#msg112989
    'Last Function in code marked Best Answer (removed debug comments and blank lines added these 2 lines.)
    ReDim t$, sign$, l$, r$, r&&
    ReDim dp As Long, dm As Long, ep As Long, em As Long, check1 As Long, l As Long, i As Long
    t$ = LTrim$(RTrim$(EXP$))
    If Left$(t$, 1) = "-" Or Left$(t$, 1) = "N" Then sign$ = "-": t$ = Mid$(t$, 2)
    dp = InStr(t$, "D+"): dm = InStr(t$, "D-")
    ep = InStr(t$, "E+"): em = InStr(t$, "E-")
    check1 = Sgn(dp) + Sgn(dm) + Sgn(ep) + Sgn(em)
    If check1 < 1 Or check1 > 1 Then N2S = _Trim$(EXP$): Exit Function 'If no scientic notation is found, or if we find more than 1 type, it's not SN!
    Select Case l 'l now tells us where the SN starts at.
        Case Is < dp: l = dp
        Case Is < dm: l = dm
        Case Is < ep: l = ep
        Case Is < em: l = em
    End Select
    l$ = Left$(t$, l - 1) 'The left of the SN
    r$ = Mid$(t$, l + 1): r&& = Val(r$) 'The right of the SN, turned into a workable long
    If InStr(l$, ".") Then 'Location of the decimal, if any
        If r&& > 0 Then
            r&& = r&& - Len(l$) + 2
        Else
            r&& = r&& + 1
        End If
        l$ = Left$(l$, 1) + Mid$(l$, 3) ' remove decimal
    End If
    Select Case r&&
        Case 0 'what the heck? We solved it already?
            'l$ = l$
        Case Is < 0
            For i = 1 To -r&&
                l$ = "0" + l$
            Next
            l$ = "." + l$
        Case Else
            For i = 1 To r&&
                l$ = l$ + "0"
            Next
            l$ = l$
    End Select
    N2S$ = sign$ + l$
End Function

Also fine!
b = b + ...
Reply
#7
(09-27-2024, 09:31 AM)bplus Wrote: I got this from Steve at old forum and tested it today on problem encountered by Cortez at another forum.

It does not work:

(09-27-2024, 02:26 PM)bplus Wrote: The problem was not using proper Sci Notation.  Angry

First number must be between 1 and < 10 and you have to use + for positive powers of 10.

Also fine!

Big Grin  Big Grin  Big Grin

It only works when the notation works.  Pass it bad data, it returns bad results. There should've been a helper function with it somewhere called Function VerifyNum that makes certain your passing a valid number.  It's a little complex and a lot of basic error checking, and as such I didn't make it part of the main process so that it could be easily bypassed for those times when you KNEW your data was valid and didn't want to add in those extra checks for processing delays.

Back in the day, these routines were used for String Math routines, and as such reducing non-necessary error checking was an important thing to keep run time as quick as possible.  If they're not certain the data is always in valid format, then you might need to hunt down that VerifyNum (or was it CheckNum??) routine and add it as a pre-check before running the N2S$ routine.  Wink
Reply




Users browsing this thread: 2 Guest(s)