UBBFriend: Email This Page to Someone!
  uCalc Message Forums
  Archives: Old messages for uCalc Fast Math Parser 2.0
  strange behavior with factorials

Post New Topic  
profile | register | preferences | faq | search

next newest topic | next oldest topic
Author Topic:   strange behavior with factorials
bcrain
Member
posted 09-08-2006 07:44 PM     Click Here to See the Profile for bcrain     Edit/Delete Message Reply w/Quote
Using FM Parser 2.0, I was surprised to get this result when I parsed and calculated a simple factorial:

3.4! = 6
3.6! = 24
(-3)! = 0

Obviously the first two examples are being rounded before the factorial is taken.

I think none of these should return a number. They should trigger an error code and return Nan.

True, Mathematica returns

3.4! = 10.1361

It must be using some generalization of the factorial -- I haven't looked into it. But I don't want that behavior. I want the simple factorial defined for non-negative integers, and undefined otherwise. Returning 0 for (-3)! is unacceptable. (Mathematica returns ComplexInfinity -- in effect, Nan.) You might say I could recognize 0 as, in effect, Nan, when the factorial is used. But that won't work. If the equation to be parsed is -6 + n!, and the user enters n = 3, the result really is 0. But if the equation is just n! and the user enters n = -3, the returned result is also 0. There's no way to distinguish between the real and the bogus 0.

IP: Logged

Daniel Corbier
Administrator
posted 09-11-2006 12:24 PM     Click Here to See the Profile for Daniel Corbier     Edit/Delete Message Reply w/Quote
In the current version, if you check ucError right after doing (-3)!, you should receive a non-zero number which indicates that there was an error. As for non-integer args, as you’ve noticed, it simply rounds the argument to an integer, and then returns the factorial for that. One work-around in the current version might be to define your own callback function named Factorial() and use the algorithm or rules of your choice.

Problems like these are tackled from multiple angles in the next version. First you can define central error handlers that would automatically display an error message (or do whatever action you deem necessary when an error occurs), instead of having to check ucError after every calculation. And you’d be able to return a NaN. And there would be full support for defining functions and operators with distinct data types (as opposed to the generic numeric type currently used). That way, you can define a factorial that works with integers only, and have it return an error otherwise (or define an overloaded factorial operator that returns NaN for certain args). A very important feature in the upcoming beta is that it now has no built-in functions or operators at all. It does have a set of common routines that you can pick from to form a default set of operators and functions. You can define some, none, or all of them. You can later undefine, or redefine those as necessary. And of course you can add your own new functions, operators, etc… For factorial and the like, the end-user could also easily define a recursive factorial function.

You may have noticed that it’s been almost a year since the last beta. I took a chance earlier this year, and started over completely from scratch, without pasting even a line of code over from the older math parser DLLs. In terms of the flexibility I envisioned, the old approach had reached a kind of dead end. Starting over has created a delay in the short term, but at this stage I have already reached a point where starting over has paid off very well by making subsequent development much easier. The new simplified approach replaces the layer of complexity and quirks that were part of the old approach. It will still take some time for the final version to be ready, but in the upcoming beta (I can’t say when that will be ready except for hopefully very soon), you should notice that uCalc FMP overall will be significantly more easy for users to implement, while performing in some cases significantly faster as well.

Disclaimers
* There is no guarantee as to the date of release for the next version.
* There is no guarantee as to which features will make it in the next version.
* There is no guarantee as to the suitability of the beta versions for purposes other than beta testing.
* All parts of the beta version are subject to change without notice, and without regard to compatibility from one beta version to the next.
* Hints by me about the next version given via e-mail, online forums, or other method of contact should not be considered as promises of any kind.
* A license for the beta cannot be purchased. While the next version remains in beta, only a license for version 2.0 can currently be purchased.

------------------
Daniel Corbier
UCalc FMP author

IP: Logged

All times are Eastern Time (US)

next newest topic | next oldest topic

Administrative Options: Close Topic | Archive/Move | Delete Topic
Post New Topic  
Hop to:

Contact Us | uCalc

Copyright © 2000-2007 by Daniel Corbier

Powered by: Ultimate Bulletin Board, Version 5.42a
© Infopop Corporation (formerly Madrona Park, Inc.), 1998 - 1999.