[haiku-development] Re: DeskCalc Improvements (was need strtold() function)

  • From: John Scipione <jscipione@xxxxxxxxx>
  • To: haiku-development@xxxxxxxxxxxxx
  • Date: Fri, 22 Jan 2010 14:10:28 -0500

On Fri, Jan 22, 2010 at 1:24 PM, Ingo Weinhold <ingo_weinhold@xxxxxx> wrote:
>
> On 2010-01-22 at 17:58:13 [+0100], John Scipione <jscipione@xxxxxxxxx>
> wrote:
>> I figured out how to get the DeskCalc application to clip the result
>> based on the size of the window pretty well now by figuring out the
>> current font size and the window width and doing a bit of math.
>> However, while this is a nicety my testing has revealed what I believe
>> to be a bug. If you type 1E6 in to DeskCalc you'll get the correct
>> result, 1000000. But if you type 1E+6 you'll get 7 as a result.
>> DeskCalc ignores the E when it sees a + sign and thinks that some sort
>> of addition is going on. No amount of parenthesis seem to be able to
>> correct this. What is worse is that 1E-6 will result in -5 and not
>> 0.00001 as it should. This problem was revealed because I sometime
>> output the result in scientific notation and the MAPM library uses the
>> 999E+99 syntax to display its results. The code to parse the
>> expression entered from DeskCalc can be found in
>> src/kits/shared/ExpressionParser.cpp. I have modified the _ParseSum()
>> function to try and skip over the offending + and - token that follows
>> the E but have had no success. Here is some sample code to look at.
> [...]
>> Can somebody look at the ExpressionParser.cpp code and see if they can
>> give me some advice on what the best way to fix this parse bug is?
>
> Since the whole number should be a single token, the problem is not in the
> parser, but in the tokenizer. Have a look at Tokenizer::NextToken().
> There's already an attempt to handle scientific notation, but it doesn't
> seem to be correct.
>
> CU, Ingo
>
>

Thanks for the tip Ingo. The author did not follow the adage, never
use chars for tokens, always use pointers (ie strings). I've been
bitten by that in the past when I've written lexers/parsers. To fix
this bug I think am going to have to rewrite NextToken to look at each
token as a string (which most of the time will be of length 1) and not
as a single char, then I am going to have to check for the strings
"E+" (or "e+") and "E-" (or "e-") and treat them as a single token
each. This means that I'll have to change the definition of the Token
struct to include a pointer to *fCurrentPos instead of simply using
fCurrentChar. I can already tell that this is going to be difficult so
I'll probably need to ask for help again before I am done with it.

Thank you,
John Scipione

Other related posts: