bernhard Posted October 5, 2018 Share Posted October 5, 2018 DEPRECATED - see the new version here: Brand new module that I've built because I'm too lazy to always open the calculator when I have to do some calculations (like vat calculations etc): It adds some magic to FieldtypeDecimal, FieldtypeFloat and FieldtypeInteger fieldtypes. Of course you can define which fields you want to extend: https://github.com/BernhardBaumrock/MathParser Any ideas or contributions are welcome. What do you think about the name? What do you think about the styling? 12 1 Link to comment Share on other sites More sharing options...
kongondo Posted October 5, 2018 Share Posted October 5, 2018 Nice one @bernhard, A couple of thoughts: Always safer to deal with IDs rather than names. If I have a float field called 'price' and tomorrow I decide to change its name to 'unit_price', MathParser code would not find it (unless of course one is using the autoload feature, in which case it might still throw the warning about field does not exist). The superuser would be forced to change the config. I've noticed assets (MathParser.js, MathParser.css and math.js) are loaded in all Page Edits, even in ones where it is not required or where there are no compatible fields. Maybe there's no way to get around this? We started on the premise that we are lazy ?. So why not take lazy to its logical conclusion? Below, I present lazy max! I've changed the module config textarea inputs to ASM Selects. The descriptions are just dummy text. Only compatible fields (float, integer, decimal [do we have a Fieldtype like this?]) are listed for selection. I don't have a Gitlab account, so I've attached a zip file with the php files to this post. Having said above. The module did not work for me (even before the changes I made). Shouldn't it be auto-triggering? Demo MathParser-kongondo-edits.zip 4 Link to comment Share on other sites More sharing options...
elabx Posted October 6, 2018 Share Posted October 6, 2018 6 hours ago, kongondo said: I've noticed assets (MathParser.js, MathParser.css and math.js) are loaded in all Page Edits, even in ones where it is not required or where there are no compatible fields. Maybe there's no way to get around this? Maybe using the Fieldtype selector in the module autoload setting?? Though I guess that limits the compatibility minimum version. type=FieldtypeInteger|FiledtypeDecimal 1 Link to comment Share on other sites More sharing options...
szabesz Posted October 6, 2018 Share Posted October 6, 2018 (edited) 12 hours ago, kongondo said: decimal [do we have a Fieldtype like this?]) Sure we do, not yet in the core though... ? One can give a thumbs-up over here to support the request of adding it to the core: https://github.com/processwire/processwire-requests/issues/126 EDIT: ot here, but Padloper 2 should utilize FiledtypeDecimal, for example prices should be of this type. There are lots of currencies where a price like 100,000,000.00 is quite possible.... Edited October 6, 2018 by szabesz EDIT added 2 Link to comment Share on other sites More sharing options...
bernhard Posted October 8, 2018 Author Share Posted October 8, 2018 Hi @kongondo thx for your improvements! On 10/5/2018 at 10:36 PM, kongondo said: I don't have a Gitlab account Thx, I've moved the module to github. I'll do that for all my public modules. The workflow is now the same for me (it somehow didn't work before so I stayed with GitLab) and the pw modules directory does also only auto-update versions from github, not gitlab. I've merged your changes. On 10/5/2018 at 10:36 PM, kongondo said: Having said above. The module did not work for me (even before the changes I made) You forgot to change one occurance of id/name change. Not sure if/why it really didn't work before the changes. Can't really believe that ? Please try version 0.0.2 https://github.com/BernhardBaumrock/MathParser/commit/3b09d501b86df06f4502f8415db9a74d13ac5d23#diff-c4d3faaaa59bb6d7e0d306c2c1312e13L125 2 Link to comment Share on other sites More sharing options...
kongondo Posted October 8, 2018 Share Posted October 8, 2018 11 minutes ago, bernhard said: I've merged your changes. Great! I forgot to clean up some copy-paste comments here ? Link to comment Share on other sites More sharing options...
bernhard Posted October 8, 2018 Author Share Posted October 8, 2018 Thx, also added a showIf for the exclude config field. https://github.com/BernhardBaumrock/MathParser/commit/e0f0c27ae07a6d19efb5ba768ae9313babcc873e On 10/5/2018 at 10:36 PM, kongondo said: I've noticed assets (MathParser.js, MathParser.css and math.js) are loaded in all Page Edits, even in ones where it is not required or where there are no compatible fields. Maybe there's no way to get around this? I'm using renderReady - so I'm not sure why fields are loaded?! Any hints? Any experience with this? Working on it ? Link to comment Share on other sites More sharing options...
bernhard Posted October 8, 2018 Author Share Posted October 8, 2018 v0.0.4 fixes the bug mentioned by @kongondo, thank you! v0.0.5 adds a check to load assets only once 2 Link to comment Share on other sites More sharing options...
bernhard Posted May 21, 2019 Author Share Posted May 21, 2019 v0.0.6 adds support for runtime fields, that means you can now use mathparser in your processmodules as well just by specifying one property: /** @var InputfieldDecimal $f */ $f = $this->modules->get('InputfieldDecimal'); $f->label = "foo"; $f->attr('id+name', "bar"); $f->mathParser = true; 2 Link to comment Share on other sites More sharing options...
LostKobrakai Posted May 21, 2019 Share Posted May 21, 2019 I've two points. Does the math lib calculate using decimal math? If not is probably should, if not you should at least remove the parseFloat in your javascript. parseFloat("0.100000000000000005") // 0.1 Link to comment Share on other sites More sharing options...
bernhard Posted May 21, 2019 Author Share Posted May 21, 2019 Hi @LostKobrakai 2 hours ago, LostKobrakai said: Does the math lib calculate using decimal math? I don't know ? https://mathjs.org/ 2 hours ago, LostKobrakai said: If not is probably should, if not you should at least remove the parseFloat in your javascript. parseFloat("0.100000000000000005") // 0.1 Could you explain a little more verbose please why you think that parsefloat is a problem? For all my use-cases I can think of, this is exactly the output I want to get... Link to comment Share on other sites More sharing options...
LostKobrakai Posted May 22, 2019 Share Posted May 22, 2019 On the reasons why floats are bad for money values: https://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency https://mathjs.org/docs/datatypes/numbers.html#roundoff-errors Math.js seems to support something called "BigNumber" to do arbitrary precition math, which should always be use for monetary calculations. But math.js does seem to also detect issues in floating point math and give you a rounded value, so maybe it's fine without that for simple calculations, where automatic rounding doesn't result in errors in the result. 1 Link to comment Share on other sites More sharing options...
bernhard Posted May 23, 2019 Author Share Posted May 23, 2019 Thx @LostKobrakai for pointing me to the right spot in the docs! I use math.format with a precicion of 14 now for all calculations. v0.0.6: v0.0.7: Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now