KSP-KOS / KSLib

The standard library and examples for the Kerboscript language
MIT License
120 stars 40 forks source link

Add lib_str_to_num #93

Closed gisikw closed 8 years ago

gisikw commented 8 years ago

Conversion library for turning numeric strings to numbers. Supports integer, floating point, and scientific notation.

crafty-geek commented 8 years ago

Duplicate of my comments on the Reddit thread: Optimization suggestions:

Whitespace leniency: for negatives, or scientific notation, use the trim function liberally. Often functions I write that return scientific notation for output will insert spaces for readability, this shouldn't make this output be un-parseable back to numbers. I'd suggest it as the first line of the function, and on either side of the "e" in scientific notation (unless, per later suggestions, you just split on the "e" and parse both sides, in which case the first-line set s to s:trim() would catch it).

Negative case: nix the 'n' variable as a negative indicator, removing it from all the multiplication cases (tough to maintain coherently), and instead alter the first if block to:

if s:startswith("-"){
    return -1* str_to_num(s:substring(1,s:length-1)).
}

perhaps paired with

if s:startswith("+"){
    return str_to_num(s:substring(1,s:length-1)).
}

This also permits down-the-road expression parsing of "minus -5" to "+5", or similar.

Scientific notation leniency (line 32): Allow "numEexp" (without a + or - symbol on exp) to be interpreted as "numE+exp", perhaps by just inserting a + within the if statement:

if numLex:haskey(m){
    set s to s:insert(e+1,"+"). set m to "+".
}else{return "NaN".}

Also, if you include my suggestion of parsing away +'s as an early if statement, l31 and 32 can be deleted (mooting the insertion suggested above), 33 edited to remove the + m, and 38-42 replaced with just line 39 (assuming the ^ operator can handle negative exponents).

gisikw commented 8 years ago

Responded on reddit, as well. But to summarize:

Designed this function explicitly so Number -> String -> Number -> String etc would be "lossless". That is, str_to_num(s) + "" = s for all valid s. Not to say that having a number inference function wouldn't be useful, but that can be placed in front of this function as wrapper, where the reverse would be more difficult (requiring passing in a bunch of flags, presumably).

Thanks for the advice on negative numbers. Kicking myself for not seeing from the get-go :)

TDW89 commented 8 years ago

Looks good.

Might want to add a line in the example that asks the user to add the required instruments to their vessel if they try and run it without them on board, could even have it check that they have the displays enabled and either enable them for the user or ask the user to do it.

Since both are just convenience things with the example and nothing to do with the lib file i will probably merge the PR at some point over the next couple of days, unless you let me know that you want to make the changes.