Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
need a Scientific Notation to real number converter
#6
From this program:

Code: (Select All)

$CONSOLE:ONLY
DIM A AS DOUBLE

FOR L1 = 1 TO 9
IF L1 = 1 THEN A = 75 / 130
IF L1 = 2 THEN A = 1 / 103
IF L1 = 3 THEN A = 7 / 27
IF L1 = 4 THEN A = 11 / 42
IF L1 = 5 THEN A = 15 / 63
IF L1 = 6 THEN A = 35 / 118
IF L1 = 7 THEN A = 60 / 142
IF L1 = 8 THEN A = 47 / 125
IF L1 = 9 THEN A = 93 / 148

A=A^2
'A$ = StrNum$(A)
ANO$ = N2S$(LTRIM$(STR$(A)))
ANO$=MID$(ANO$,1,6)
P=P + VAL(ANO$)

PRINT ANO$
NEXT

P = (P/9)
ANO$ = N2S$(LTRIM$(STR$(P)))
ANO$=MID$(ANO$,1,6)
PRINT ANO$ + " <- averaged"
color 10
'print "Press any key"
'SLEEP
SYSTEM


' FUNCTION StrNum$ (n)
' value$ = UCASE$(LTRIM$(STR$(n)))
' XPOS1 = INSTR(value$, "D") + INSTR(value$, "E")
' IF XPOS1 THEN
' expo = VAL(MID$(value$, XPOS1 + 1))
' IF VAL(value$) < 0 THEN
' sign$ = "-": value$ = MID$(value$, 2, XPOS1 - 2)
' ELSE value$ = MID$(value$, 1, XPOS1 - 1)
' END IF
' dot = INSTR(value$, "."): L = LEN(value$)
' IF expo > 0 THEN ADD$ = StrNum$(expo - (L - dot), "0")
' IF expo < 0 THEN min$ = StrNum$(ABS(expo) - (dot - 1), "0"): DP$ = "."
' FOR N = 1 TO L
' IF MID$(value$, N, 1) <> "." THEN num$ = num$ + MID$(value$, N, 1)
' NEXT
' ELSE StrNum$ = value$: EXIT FUNCTION
' END IF
' StrNum$ = sign$ + DP$ + min$ + num$ + ADD$
' END FUNCTION


Sub FixNumbers (a$, b$)
'first remove scientific notation and spaces from both
a$ = _Trim$(N2S$(a$)): b$ = _Trim$(N2S$(b$))
'then find the decimal position for both and normalize the expressions
d1 = InStr(a$, "."): d2 = InStr(b$, ".")
If d1 <> 0 Then 'break down the left and right side of the decimal point for ease of processing (this is a$)
lefta$ = Left$(a$, d1 - 1)
righta$ = Mid$(a$, d1)
Else
lefta$ = a$
End If
If d2 <> 0 Then 'break down the left and right side of the decimal point for ease of processing (this is b$)
leftb$ = Left$(b$, d2 - 1)
rightb$ = Mid$(b$, d2)
Else
leftb$ = b$
End If

'normalize the right side of our expressions
l1 = Len(righta$): l2 = Len(rightb$)
If l1 < l2 Then
addzero = l2 - l1
If l1 = 0 Then righta$ = ".": addzero = addzero - 1
righta$ = righta$ + String$(addzero, "0")
ElseIf l1 > l2 Then
addzero = l1 - l2
'If l2 = 0 Then rightb$ = ".": addzero = addzero - 1
rightb$ = rightb$ + String$(addzero, "0")
End If



'strip off any plus/minus signs from the two numbers.
If Left$(lefta$, 1) = "-" Then signa$ = "-": lefta$ = Mid$(lefta$, 2)
If Left$(leftb$, 1) = "-" Then signb$ = "-": leftb$ = Mid$(leftb$, 2)
If Left$(lefta$, 1) = "+" Then signa$ = "": lefta$ = Mid$(lefta$, 2)
If Left$(leftb$, 1) = "+" Then signb$ = "": leftb$ = Mid$(leftb$, 2)
'normalize the left side of our expressions
l1 = Len(lefta$): l2 = Len(leftb$)
If l1 < l2 Then
addzero = l2 - l1
lefta$ = String$(addzero, "0") + lefta$
ElseIf l1 > l2 Then
addzero = l1 - l2
leftb$ = String$(addzero, "0") + leftb$
End If
'and then put it all together
a$ = signa$ + lefta$ + righta$
b$ = signb$ + leftb$ + rightb$
End Sub





Function N2S$ (exp$) 'scientific Notation to String

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 = 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$ = "0." + l$
Case Else
For i = 1 To r&&
l$ = l$ + "0"
Next
End Select

N2S$ = sign$ + l$
End Function


Function DWD$ (exp$) 'Deal With Duplicates
'To deal with duplicate operators in our code.
'Such as -- becomes a +
'++ becomes a +
'+- becomes a -
'-+ becomes a -
t$ = exp$
Do
bad = 0
Do
l = InStr(t$, "++")
If l Then t$ = Left$(t$, l - 1) + "+" + Mid$(t$, l + 2): bad = -1
Loop Until l = 0
Do
l = InStr(t$, "+-")
If l Then t$ = Left$(t$, l - 1) + "-" + Mid$(t$, l + 2): bad = -1
Loop Until l = 0
Do
l = InStr(t$, "-+")
If l Then t$ = Left$(t$, l - 1) + "-" + Mid$(t$, l + 2): bad = -1
Loop Until l = 0
Do
l = InStr(t$, "--")
If l Then t$ = Left$(t$, l - 1) + "+" + Mid$(t$, l + 2): bad = -1
Loop Until l = 0
Loop Until Not bad
DWD$ = t$
End Function

I get this output on Debian Linux:

Code: (Select All)
.33284
0.0000
.06721
0.0685
0.0566
0.0879
.17853
.14137
.39485
.14753 <- averaged

So I don't know where you're pulling a much-longer list of strange numbers from.

I noticed your `StrNum$()` routine uses recursion, which is one thing fundamentally wrong with it. You should not need recursion to detect a `D` or `E`, or anything else in a floating-point numerical value in scientific notation in BASIC.
Reply


Messages In This Thread
RE: need a Scientific Notation to real number converter - by mnrvovrfc - 10-12-2023, 04:38 AM



Users browsing this thread: 1 Guest(s)