Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Scientific Notation
#6
(11-10-2022, 09:55 AM)Pete Wrote: Actually, I think you need a semi colon, not a comma there...

Code: (Select All)
PRINT USING "####.##########"; a

Anyway, there are a lot of these floating point bombs to contend with.Here's two simple addition ones that differ with variable types.

Code: (Select All)
DIM a AS DOUBLE
FOR i = 1 TO 10
    a = a + .01
    PRINT a ' Bombs at iteration 7.
NEXT

DIM b as SINGLE
FOR i = 1 TO 10
    b = b + .01
    PRINT b ' Bombs at iteration 10.
NEXT

PRINT USING and FIX() are a couple of workarounds but for better reliability across a large number spectrum you need to consider using something like my string math routine, I believe Mark and Steve have ones of their own, too, or Jack's excellent numeric dec-float routine. Of course you need to call these as libraries and add them with an $INCLUDE statement or just get comfortable with a sub addition that consists of several hundreds of lines.

Pete

Yeah but expensive in LOC and there always seems to be something else you need to fix!
Here we need a rounder and that usually needs to convert number back from string.
Code: (Select All)
_Title "N2S$ Test Steve's converter for number display" ' b+ 2022-11-10

Dim a As Double
For i = 1 To 10
    a = a + .01
    Print a; Tab(40); N2S$(Str$(a)) ' Bombs at iteration 7.
Next

Dim b As Single
For i = 1 To 10
    b = b + .01
    Print b; Tab(40); N2S$(Str$(b)) ' Bombs at iteration 10.
Next

' call N2S$(Str$(YourNumberOfAnyType))
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

Another case solved by string math!
  724  855  599  923  575  468  400  206  147  564  878  823  652  556 bxor cross forever
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

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to display a float number in scientific? desA 19 3,137 08-05-2024, 06:55 PM
Last Post: Jack
  need a Scientific Notation to real number converter random1 24 4,566 10-20-2023, 05:11 PM
Last Post: bplus
  rounding numbers and converting to string (hiding scientific notation) madscijr 9 2,182 07-29-2022, 07:43 PM
Last Post: Pete

Forum Jump:


Users browsing this thread: 1 Guest(s)