introduce implicit multiplication
Originally reported on Google Code with ID 538
Many mathematical tools offer this notation simplification, and it is very handy :)
Instead of writing 3*pi one could write 3pi etc...
Reported by oversc0re
on 2014-11-13 08:05:57
Comments (19)
-
Account Deleted -
-
assigned issue to
- edited description
-
assigned issue to
-
- changed title to introduce implicit multiplication
- changed component to parser
I edited the title to properly name what the OP wanted to express.
With the introduction of units, implied multiplication is going to happen. Quite a few discussions have been had on this subject (issues
#19,#34,#54,#226,#301), none of whicj I was part of, and all of which I skillfully ignored when implementing this feature :)I would like to use this place to discuss possible shortcomings and enhancements to my implementation. Let me first describe how it works.
- Adapting the parser to include the new rule was reasonably straight forward. The tokenizer was left unchanged, only the 'compiler' was modified to treat the rule
identifier identifier
as a multiplication. Implicit multiplication is always treated with the same precedence as its explicit counterpart. Adding an asterisk*
should not modify the order of operations in any case. Examples:a/b c = a/b*c
,5x^2 = 5*(x^2)
.
- Obviously if the user wants to express
0 * x5
, but types0x5
, the result will be parsed as a hexadecimal number. The same goes for other bases. Still, I don't see this as a problem. - The right hand operand of the IM must currently be an identifier (e.g. a variable), but not a literal constant (e.g. '5'). This is results in the following behavior:
5a
->5*a
,a 5
->invalid
,ab
-> value of the variableab
x x
->x^2
(not the value ofxx
),3 3
->33
(thirty three),2(a+1)
->2*(a+1)
,(3)(3)
->invalid
,2 arcsin(sqrt(2)/2)
->invalid
, but2 sin(pi/3)
is ok.
I would love some feedback. I think it is clear that some of these are not ideal. At the time I did not put overly much thought into it and went with a conservative approach (one that often yields
invalid expression
). I am confident that any decision could be implemented easily (read: without too much trouble). -
repo owner From here, how hard would it be to support "3 sin(pi)"?
-
repo owner -
repo owner - changed status to closed
-
3 sin(pi)
is working just fine. Only a literal number is currently not allowed, e.g.3 sin(2)
.I think I remember why I chose to do it this way. IIRC, I'd get weird things like
0b104 = 8
(interpreted as 0b10 * 4), so I preferred not to allow implicit multiplication of two literals. Still, I agree it could be done better, but it is kinda hard with the parser at hand (it only knows 'constant' and 'identifier', so I worked with those). -
repo owner Yes, it's a bit strange for an user that "3 sin(3 pi)" works but "3 sin(3)" doesn't.
-
It's a bug. Will try to fix it.
-
No it's not. It is a side effect of intended behaviour. Doing it properly will either require a mid-sized change of the parser (not the full blown stuff we have been discussing) or a change of the tokenizer.
I fear that any 'fix' for this will merely be a hack. So I no longer fully agree with my former self when writing that it can be done easily.
Feel free to prove me wrong! :)
-
@polwel: Quickly fixed. :-)
-
@fk @tey: Don't forget to merge this one before your builds. Thanks.
-
IIRC I tried that one, but I can't remember why I rejected it....
We might also do this with parentheses, so that
2(2+3)
is allowed.Either way, good job!
EDIT: Builds? I don't think we are releasing just yet :)
-
repo owner - changed status to resolved
Fixes issue 538. Update regression tests for this new behavior. 3 sin (3 pi) was evaluated but not 3 sin (3).
→ <<cset 897609e9930c>>
-
repo owner Merged in thadrien/speedcrunch-complex/implicit-multiplication-bug (pull request #25)
Fixes issue 538.
→ <<cset 56b98235995e>>
-
@thadrien don't ping "tey" on bitbucket, it's not me :) Anyway, the build process fails currently but it will work again once pull request #26 is merged, so your changes will be included.
@polwel concerning your question about builds, Felix provides daily build for Ubuntu and I provide nightly builds for Windows.
-
repo owner - changed status to closed
-
@Teyut: thanks for your answers and your builds. I won't ping tey anymore. :-)
-
Issue
#618was marked as a duplicate of this issue. - Log in to comment
Reported by
helder.pereira.correia
on 2014-11-28 17:16:04 - Labels added: Type-Enhancement