Scientific Notation
#7
OK see what breaks this?
(I did the rounding first then called Steves N2S$ then chopped to digits:
Code: (Select All)
_Title "N2S$ Test Steve's converter for number display" ' b+ 2022-11-10
Print "Type Double:"
Dim a As Double
For i = 1 To 10
    a = a + .000000001
    Print a; Tab(40); roundDP$(a, 10) ' Bombs at iteration 7.
Next
Print "Type Single:"
Dim b As Single
For i = 1 To 10
    b = b + .000000001
    Print b; Tab(40); roundDP$(b, 10) ' Bombs at iteration 10.
Next

' this function needs N2S$ watch number type??
Function roundDP$ (num, digits) 'unsimplified  2021-09-16
    Dim s$, dot
    s$ = N2S$(Str$(num + (Sgn(num) * .5) * 10 ^ -digits))
    dot = InStr(s$, ".")
    If dot Then roundDP$ = Mid$(s$, 1, dot + digits) Else roundDP$ = s$
End Function

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
b = b + ...
Reply


Messages In This Thread
Scientific Notation - by james2464 - 11-10-2022, 04:27 AM
RE: Scientific Notation - by BSpinoza - 11-10-2022, 04:56 AM
RE: Scientific Notation - by james2464 - 11-10-2022, 05:31 AM
RE: Scientific Notation - by Pete - 11-10-2022, 09:55 AM
RE: Scientific Notation - by bplus - 11-10-2022, 04:02 PM
RE: Scientific Notation - by mnrvovrfc - 11-10-2022, 04:52 PM
RE: Scientific Notation - by james2464 - 11-10-2022, 03:54 PM
RE: Scientific Notation - by bplus - 11-10-2022, 04:36 PM
RE: Scientific Notation - by Pete - 11-10-2022, 04:41 PM
RE: Scientific Notation - by bplus - 11-10-2022, 05:18 PM
RE: Scientific Notation - by Pete - 11-10-2022, 05:37 PM
RE: Scientific Notation - by SMcNeill - 11-10-2022, 08:03 PM
RE: Scientific Notation - by bplus - 11-10-2022, 08:43 PM
RE: Scientific Notation - by Pete - 11-10-2022, 09:40 PM



Users browsing this thread: 5 Guest(s)