Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Compiler setting for accurate math?
#6
Code: (Select All)
Dim x
For x = 1 To -0.001 Step -.05
    Print Round2$(x, -2)
    _Limit 4
Next x


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

Function Round2$ (anyNumber As _Float, dp As Long) ' uses N2S$
    ' 5 and up at decimal place dp+1 > +1 at decimal place  4 and down  > +0 at dp

    '2 1 0.-1 -2 -3 -4 ...  pick dp like this for this Round$ Function
    Dim sn$, dot, predot, postdot, rtn$

    sn$ = N2S$(Str$(anyNumber + .5 * 10 ^ dp)) 'get rid of sci notation, steve trims it so next find dot
    dot = InStr(sn$, ".")
    If dot Then
        predot = dot - 1
        postdot = Len(sn$) - (dot + 1)
    Else
        predot = Len(sn$)
        postdot = 0
    End If
    ' xxx.yyyyyy  dp = -2
    '      ^ dp
    If dp >= 0 Then
        rtn$ = Mid$(sn$, 1, predot - dp) + String$(dp, "0")
    Else
        rtn$ = Mid$(sn$, 1, predot) + "." + Mid$(sn$, dot + 1, -dp)
    End If
    If rtn$ = "" Then Round2$ = "0" Else Round2$ = rtn$
End Function

A few LOC less than string math and still QB64 Smile
  724  855  599  923  575  468  400  206  147  564  878  823  652  556 bxor cross forever
Reply


Messages In This Thread
RE: Compiler setting for accurate math? - by Jack - 11-30-2022, 11:48 PM
RE: Compiler setting for accurate math? - by Pete - 12-01-2022, 04:26 AM
RE: Compiler setting for accurate math? - by bplus - 12-01-2022, 06:18 AM
RE: Compiler setting for accurate math? - by Pete - 12-01-2022, 10:43 AM
RE: Compiler setting for accurate math? - by Pete - 12-01-2022, 04:52 PM
RE: Compiler setting for accurate math? - by Pete - 12-01-2022, 10:16 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Setting mouse to ignore outside of _NewImage PhilOfPerth 11 737 12-18-2025, 07:20 PM
Last Post: Pete
  random maze map. math help pmackay 4 572 08-10-2025, 11:22 AM
Last Post: pmackay
  GNU C++ Compiler error eoredson 55 8,206 12-26-2024, 05:27 AM
Last Post: eoredson
  Run code without compiler phil 9.0 7 1,328 12-05-2024, 08:52 AM
Last Post: phil 9.0
  Setting Line _RGB colours PhilOfPerth 10 1,983 11-03-2024, 05:37 AM
Last Post: PhilOfPerth

Forum Jump:


Users browsing this thread: 1 Guest(s)