VAL of a single
Posted: Sun Nov 23, 2014 4:37 am
Maybe there's a way to do this already in Coridium BASIC, but I was on a military base with no internet access, and had to ham up something really quick to fix a required change in a parameter. It's quickly thrown together, not optimized, and not very fast, but it works and might give you a jumping-off place to make a faster, better version.
Lloyd
'==============================compute val of floating string into single var
'
function val_single(work_string$(20) as string) as single
' a true single representation can only be 9 characters long, but if somebody puts a lot
' of leading zeros on it, we want room... so make the working strings 20 characters
dim test$(20) as string
dim factor as integer
dim fd as integer
dim fl as integer
dim i as integer
dim intervar as integer
dim tempvar as single
dim mult as single
do while (left(work_string$,1)=" " OR left(work_string$,1)="0")
work_string$=right(work_string$,len(work_string$)-1) ' get rid of leading spaces and zeros
loop
if left(work_string$,1)="-" then ' if it's negative
factor=-1
else
factor=1
endif
if (left(work_string$,1)="-" or left(work_string$,1)="+") then ' if signed, get the absolute value now that
work_string$=right(work_string$,len(work_string$)-1) ' we have the factor
endif
fd=len(work_string$)
for fl=0 to len(work_string$)
test$=midstr(work_string$,fl,1) ' find any possible decimal point
if (test$=".") then
fd=fl
endif
next fl
if fd<=len(work_string$) then
test$=left(work_string$,fd) ' get the integer portion to the left of the decimal
intervar=val(test$)
tempvar=intervar ' take the integer val, and cast it to single
test$=right(work_string$,(len(work_string$)-fd)-1) ' get the right fractional portion, and figure out what
mult=1 ' the multiplier needs to be for the number of digits to the right
for i=1 to len(test$) ' figure out our multipliers
tempvar=tempvar*10 ' keep multiplying the integer part by 10 for each additional digit to the right
mult=mult*10
next i
intervar=val(test$) ' cast that result to a single, then
tempvar=factor*(tempvar+intervar)/mult ' then compute whole value of float
endif
return(tempvar)
endfunction
'-------------------------------------------------------------------------
Lloyd
'==============================compute val of floating string into single var
'
function val_single(work_string$(20) as string) as single
' a true single representation can only be 9 characters long, but if somebody puts a lot
' of leading zeros on it, we want room... so make the working strings 20 characters
dim test$(20) as string
dim factor as integer
dim fd as integer
dim fl as integer
dim i as integer
dim intervar as integer
dim tempvar as single
dim mult as single
do while (left(work_string$,1)=" " OR left(work_string$,1)="0")
work_string$=right(work_string$,len(work_string$)-1) ' get rid of leading spaces and zeros
loop
if left(work_string$,1)="-" then ' if it's negative
factor=-1
else
factor=1
endif
if (left(work_string$,1)="-" or left(work_string$,1)="+") then ' if signed, get the absolute value now that
work_string$=right(work_string$,len(work_string$)-1) ' we have the factor
endif
fd=len(work_string$)
for fl=0 to len(work_string$)
test$=midstr(work_string$,fl,1) ' find any possible decimal point
if (test$=".") then
fd=fl
endif
next fl
if fd<=len(work_string$) then
test$=left(work_string$,fd) ' get the integer portion to the left of the decimal
intervar=val(test$)
tempvar=intervar ' take the integer val, and cast it to single
test$=right(work_string$,(len(work_string$)-fd)-1) ' get the right fractional portion, and figure out what
mult=1 ' the multiplier needs to be for the number of digits to the right
for i=1 to len(test$) ' figure out our multipliers
tempvar=tempvar*10 ' keep multiplying the integer part by 10 for each additional digit to the right
mult=mult*10
next i
intervar=val(test$) ' cast that result to a single, then
tempvar=factor*(tempvar+intervar)/mult ' then compute whole value of float
endif
return(tempvar)
endfunction
'-------------------------------------------------------------------------