Posted: 16th Dec 2011 13:09
It seems we cannot get floats from val(), or am I missing another command?


+ Code Snippet
getVal()

end

` *************************************
` *** Val cannot handle floats
` *************************************
function GetVal()

    myVal1$ = "12345"
    myVal2$ = "123.45"
    myVal3$ = "0.12345"

    myVal1 = val(myVal1$)
    myVal2 = val(myVal2$)
    myVal3 = val(myVal3$)

    do
        print(myVal1$ + " : " + str(myVal1))
        print(myVal2$ + " : " + str(myVal2))
        print(myVal3$ + " : " + str(myVal3))
        sync()
    loop

endfunction


result :

"
Posted: 16th Dec 2011 14:21
+ Code Snippet
function reVal(s$, prec)
    //check if a . is in the string, if not exit with int(s$)
    isNeg = 0
    result# = 0.0
    pos = findValPos(s$,".")

    if pos = -1
        // must be an integer
        retval# = val(s$)
        exitfunction retval#
    endif

    // check if first character is a negative sign
    if mid(s$,1,1) = "-"
        isNeg = 1
        s$ = mid(s$,2,len(s$)-1)
    endif


    base = val(left(s$,pos-1))
    //print(base)
    d$ = right(s$,len(s$)-pos)
    //print("right - " + d$)
    decc = val(d$)

    sz = len(d$)
    if sz > prec
        sz = prec
        decc = val(left(d$,sz))
        //print("final decc - "+str(decc))
    endif
    select sz
        case 1
            result# = 0.1
        endcase
        case 2
            result# = 0.01
        endcase
        case 3
            result# = 0.001
        endcase
        case 4
            result# = 0.0001
        endcase
        case 5
            result# = 0.00001
        endcase
        case 6
            result# = 0.000001
        endcase

    endselect

    reval# = decc * result#
    reval# = base + reval#
    if isNeg = 1 then reval# = reval# * -1.0

endfunction reval#
Posted: 16th Dec 2011 21:16
Edit - never mind, misread something.
Posted: 16th Dec 2011 22:49
Thanks netmon, complicated but fixes the problem.

I actually took an alternative approach, which works for me but probably won't for all scenarios. I'm reading values from an ini file, so I have saved all values as value * 100, then I divide by 100 when I read it back in.

[EDIT] - fixed in V107 - http://code.google.com/p/agk/issues/detail?id=54&q=val
Posted: 16th Dec 2011 23:26
I'm reading values from an ini file, so I have saved all values as value * 100, then I divide by 100 when I read it back in.

Do you lose any accuracy with that (on certain numbers anyway e.g 123.45)?

I quickly wrote a function to convert strings to decimals for fun but I loose accuracy when I divide.

+ Code Snippet
rem A Wizard Did It!
val1$ = "1.234"
val2$ = "4.35"
Val3$ = "123.45"

dec1# = getDecimalVal(val1$)
dec2# = getDecimalVal(val2$)
dec3# = getDecimalVal(val3$)

b# = 12345.0 / 100.0

do
 Print(val1$ + " : " + str(dec1#))
 Print(val2$ + " : " + str(dec2#))
 Print(val3$ + " : " + str(dec3#))
 print(b#)
 Sync()
loop

function getDecimalVal( decimal$ as string )

    // locals
    length as integer = 0 // length of string
    place as integer = 0 // the place of the current character when looping through the string
    p10 as integer = 0 // power of 10
    value# as float = 0.0 // the decimal to be returned
    intvalue as float = 0.0 // the integer half of the value e.g 123 is the intvalue in 123.45
    decvalue as float = 0.0 // the complement of intvalue e.g 45 is the decvalue in 123.45

    // get the length of the decimal
    length = len(decimal$)

    // loop through until we find the decimal point
    repeat

    place = place + 1

    until mid(decimal$, place, 1) = "." or place > length

    p10 = length - place // get the power of 10 we will need to multipy by and divide by

    intvalue = val( left( decimal$, place-1)) // get the value for intvalue
    decvalue = val( right( decimal$, p10)) // get the decimal value in integer form

    // multiply by the power of 10, add the decimal and then divide by the power of 10
    value# = ((intvalue * (10.0^p10)) + decvalue) / (10.0^p10)

endfunction value# // return decimal.


It may just be the general loss of accuracy though which many languages suffer from so there's probably no point trying to fix the above.

[EDIT] - fixed in V107

Good to hear.
Posted: 17th Dec 2011 11:28
Yes you would lose accuracy, but then you would if you converted the string too I think. 0.1 is a good example, there is no float equivalent.

For me, it's accurate enough. These are seconds and 0.1 is as good as 0.99996....