VAL of a single

Questions about the BASICtools and MakeItC
Post Reply
AMDlloydsp
Posts: 99
Joined: Mon Apr 15, 2013 3:51 pm
Location: NE Central FL

VAL of a single

Post by AMDlloydsp »

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
'-------------------------------------------------------------------------
Last edited by AMDlloydsp on Mon Nov 24, 2014 4:49 am, edited 1 time in total.



basicchip
Posts: 1090
Joined: Fri Oct 19, 2012 2:39 am
Location: Weeki Watchee, FL
Contact:

Re: VAL of a single

Post by basicchip »

There is no function like that in the firmware, unlike PRINTF and SPRINTF which were already in there to handle the PRINT in BASIC.

The compiler running on the PC handles that in the user source, so there is nothing at run time.

AMDlloydsp
Posts: 99
Joined: Mon Apr 15, 2013 3:51 pm
Location: NE Central FL

Re: VAL of a single

Post by AMDlloydsp »

The compiler running on the PC handles that in the user source
--------------
???
You mean I did not need to write that? That the compiler will 'manage' val of a string representing a single?

Having read the docs for VAL(), I didn't even try! Oh, no!

"Description

VAL converts a string to a decimal number. For example, VAL("10") will return 10. The function parses the string from the left and returns the longest number it can read, stopping at the first non-suitable character it finds.

Incidentally, this function is the opposite of STR , which converts a number to a string.

string can contain negative numbers or hex numbers when preceded by a $.

VAL does only INTEGER interpretation"

Lloyd

basicchip
Posts: 1090
Joined: Fri Oct 19, 2012 2:39 am
Location: Weeki Watchee, FL
Contact:

Re: VAL of a single

Post by basicchip »

No, I think you did need to write that, as I am assuming you are receiving some floating point number as a string from some other device.

What I mean about the PC is that the compiler has to handle --

x = 1.24567

But that is done at compile time on the PC. The ARM VAL function is limited to integers, and as the firmware is just below 12K on the M0 parts, there isn't much room to add any new runtime routines.

AMDlloydsp
Posts: 99
Joined: Mon Apr 15, 2013 3:51 pm
Location: NE Central FL

Re: VAL of a single

Post by AMDlloydsp »

Ah... good. I thought I had wasted time.

Yes, I knew the compiler would automatically assign a variable type at compile time, but I don't like to count on that, just on "GPs".

I explicitly DIM every variable.

Yeah... the client changed from a weighing module that reported in milligrams to one that reports grams and decimal fractions.

The baud rate isn't high, and the SuperPro is super fast, so it's not a problem, even though that's more code that it needs to be. I'll tighten it up when the need or the urge makes me do it.

For now, it works, and the customer is happy.
Lloyd

Post Reply