![]() ![]() |
![]() ![]() ![]() ![]() ![]()
|
next newest topic | next oldest topic |
Author | Topic: strange behavior with factorials |
bcrain Member |
![]() ![]() ![]() ![]() Using FM Parser 2.0, I was surprised to get this result when I parsed and calculated a simple factorial: 3.4! = 6 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 |
![]() ![]() ![]() ![]() 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 ------------------ IP: Logged |
All times are Eastern Time (US) | next newest topic | next oldest topic |
![]() |
Copyright © 2000-2007 by Daniel Corbier
Powered by: Ultimate Bulletin Board, Version 5.42a
© Infopop Corporation (formerly Madrona Park, Inc.), 1998 - 1999.