|
NAMENumber::WithError - Numbers with error propagation and scientific roundingSYNOPSISuse Number::WithError; my $num = Number::WithError->new(5.647, 0.31); print $num . "\n"; # prints '5.65e+00 +/- 3.1e-01' # (I.e. it automatically does scientific rounding) my $another = $num * 3; print $another . "\n"; # propagates the error assuming gaussian errors # prints '1.69e+01 +/- 9.3e-01' # trigonometric functions also work: print sin($another) . "\n"; # prints '-9.4e-01 +/- 3.1e-01' my $third = $another ** $num; print $third. "\n"; # propagates both errors into one. # prints '8.7e+06 +/- 8.1e+06' # shortcut for the constructor: use Number::WithError 'witherror'; $num = witherror('0.00032678', ['2.5e-5', '3e-5'], 5e-6); # can deal with any number of errors, even with asymmetric errors print $num . "\n"; # prints '3.268e-04 + 2.5e-05 - 3.00e-05 +/- 5.0e-06' # Note: It may be annyoing that they don't all have the same # exponent, but they *do* all have the sam significant digit! DESCRIPTIONThis class is a container class for numbers with a number of associated symmetric and asymmetric errors. It overloads practically all common arithmetic operations and trigonometric functions to propagate the errors. It can do proper scientific rounding (as explained in more detail below in the documentation of the "significant_digit()" method).You can use Math::BigFloat objects as the internal representation of numbers in order to support arbitrary precision calculations. Errors are propagated using Gaussian error propagation. With a notable exception, the test suite covers way over ninety percent of the code. The remaining holes are mostly difficult-to-test corner cases and sanity tests. The comparison routines are the exception for which there will be more extensive tests in a future release. OVERLOADED INTERFACEThis module uses overload to enable the use of the ordinary Perl arithmetic operators on objects. All overloaded operations are also availlable via methods. Here is a list of overloaded operators and the equivalent methods. The assignment forms of arithmetic operators (e.g. "+=") are availlable if their normal counterpart is overloaded.
Here's a list of overloadable operations that aren't overloaded in the context of this module: << >> x . & ^ | atan2 int CONSTRUCTORSAll constructors accept Math::BigFloat objects in place of numbers.newThis is the basic constructor for "Number::WithError" objects.New objects can be created in one of two ways:
Note that trailing zeros currently have no effect. (Yes, this is a BUG!) The constructor returns a new object of this class or undef if something went wrong. new_bigThis is an alternative constructor for "Number::WithError" objects. It works exactly like "new" except that it makes all internal numbers instances of "Math::BigFloat" for high precision calculations.The module does not load "Math::BigFloat" at compile time to avoid loading a big module that isn't needed all the time. Instead, this module makes use of the prefork pragma and loads "Math::BigFloat" when needed at run-time. witherrorThis constructor is not a method. It is a subroutine that can be exported to your namespace on demand. It works exactly as the "new()" method except it's a subroutine and shorter.I'm normally not for this kind of shortcut in object-oriented code, but if you have to create a large number of "Number::WithError" numbers, you'll appreciate it. Trust me. Note to authors of subclasses: If you inherit from this module, you'll need to implement your own "witherror()" because otherwise, it will still return objects of this class, not your subclass. witherror_bigThis is also not a method. It does the same as "witherror()". It can also be optionally be exported to your namespace.It uses the "new_big" constructor instead of the "new" constructor used by "witherror()". ARITHMETIC METHODSAll of these methods implement an arithmetic operation on the object and the method's first parameter.The methods aren't mutators. That means they don't modify the object itself, but return the result of the operation as a new object. All of the methods accept either a plain number, a "Number::WithError" object or anything that is understood by the constructors as argument, All errors are correctly propagated using Gaussian Error Propagation. The formulae used for this are mentioned in the individual methods' documentation. addAdds the object a and the argument b. Returns a new object c.Formula: "c = a + b" Error Propagation: "err_c = sqrt( err_a^2 + err_b^2 )" subtractSubtracts the argument b from the object a. Returns a new object c.Formula: "c = a - b" Error Propagation: "err_c = sqrt( err_a^2 + err_b^2 )" multiplyMultiplies the object a and the argument b. Returns a new object c.Formula: "c = a * b" Error Propagation: "err_c = sqrt( b^2 * err_a^2 + a^2 * err_b^2 )" divideDivides the object a by the argument b. Returns a new object c.Formula: "c = a / b" Error Propagation: "err-c = sqrt( err_a^2 / b^2 + a^2 * err_b^2 / b^4 )" exponentiateRaises the object a to the power of the argument b. Returns a new object c. Returns "undef" if a is negative because the error cannot be propagated in that case. (Can't take log of a negative value.)Also, please have a look at the error propagation formula below. Exponentiation and logarithms are operations that can become numerically unstable very easily. Formula: "c = a ^ b" Error Propagation: "err-c = sqrt( b^2 * a^(b-1) * err_a^2 + ln(a)^2 * a^b * err_b^2 )" METHODS FOR BUILTIN FUNCTIONSThese methods calculate functions of the object and return the result as a new object.sqrtCalculates the square root of the object a and returns the result as a new object c. Returns undef if a is negative.Formula: "c = sqrt(a)" Error Propagation: "err-c = sqrt( err-a^2 / (2*sqrt(a))^2 ) = abs( err-a / (2*sqrt(a)) )" logCalculates the natural logarithm of an object a. Returns a new object c. If a is negative, the function returns undef.Formula: "c = log(a)" Error Propagation: "err-c = sqrt( err-a^2 / a^2 ) = abs( err-a / a )" sinCalculates the sine of the object a and returns the result as a new object c.Formula: "c = sin(a)" Error Propagation: "err-c = sqrt( cos(a)^2 * err-a^2 ) = abs( cos(a) * err-a )" cosCalculates the cosine of the object a and returns the result as a new object c.Formula: "c = cos(a)" Error Propagation: "err-c = sqrt( sin(a)^2 * err-a^2 ) = abs( sin(a) * err-a )" tanCalculates the tangent of the object a and returns the result as a new object c.Formula: "c = tan(a)" Error Propagation: "err-c = sqrt( err-a^2 / cos(a)^4 ) = abs( err-a / cos(a)^2 )" Since there is no built-in "tan()" function, this operation is not available via the overloaded interface. absCalculates the absolute value of an object a. Leaves the errors untouched. Returns a new object c.Formula: "c = abs(a)" Error Propagation: "err-c = err-a" ROUNDING, STRINGIFICATION AND OUTPUT METHODSThis section documents methods dealing with the extraction of data from the object. The methods implement rounding of numbers, stringification of the object and extracting meta information like the significant digit.numberDetermines the significant digit using the "significant_digit()" method, rounds the number that the object "number()" is called on represents to that digit and returns the rounded number.Regardless of the internal representation of the number, this returns an unblessed string / an unblessed floating point number. To gain access to the raw number representation in the object, use the "raw_number" method. Either way, the number will be in scientific notation. That means the first non-zero digit comes before the decimal point and following the decimal point and any number of digits is an exponent in "eXXX" notation. raw_numberThis method returns the internal representation of the number in the object. It does not round as appropriate. It does not clone "Math::BigFloat" objects either. So make sure you do that if necessary!roundThis method determines the significant digit using the "significant_digit()" method. Then, it rounds the number represented by the object and all associated errors to that digit.Then, the method concatenates the number with its errors and returns the resulting string. In case of symmetric errors, the string "+/-" will be prepended to the error. In case of asymmetric errors, a "+" will be prepended to the first/upper error component and a "-" to the second/lower error component. Returns the previously described string. significant_digitThis method returns the significant digit of the number it is called on as an integer. If the number has no errors or all errors are "undef" or zero, this method returns "undef".The return value of this method is to be interpreted as follows: If this method returns "-5", the significant digit is "1 * 10**-5" or 0.00001. If it returns 3, the significant digit is "1 * 10**3" or 1000. If it returns 0, the significant digit is 1. The return value is computed by the following algorithm: The individual significant digit of a single error is: Take the exponent of the first non-zero digit in the error. The digit after this first non-zero digit is the significant one. This method returns the minimum of the individual significant digits of all errors. That means: 5 +/- 0.0132 + 0.5 - 1 Will yield a return value of "-3" since the first error has the lowest significant digit. This algorithm is also used for determining the significant digit for rounding. It is extremely important that you realize this isn't carved in stone. The way the significant digit is computed in the presence of errors is merely a convention. In this case, it stems from particle physics. It might well be that in your particular scientific community, there are other conventions. One, for example, is to use the second non-zero digit only if the first is a 1. errorThis method returns a reference to an array of errors of the object it is called on.Unlike the "raw_error()" method, this method takes proper care to copy all objects and references to defy action at a distance. The structure of the returned array reference is akin to that returned by "raw_error()". Furthermore, this method rounds all errors to the significant digit as determined by "significant_digit()". raw_errorReturns the internal representation of the errors of the current object. Note that (just like "raw_number()", this does not clone the data for safe use without action at a distance. Instead, it directly returns the internal reference to the error structure. The structure is an array of errors. Each error may either be a string or floating point number or a "Math::BigFloat" object or an array reference. In case of an array reference, it is an asymmetric error. The inner array contains two strings/numbers/"Math::BigFloat"s.Note that this practically breaks encapsulation and code relying on it might break with future releases. as_arrayThis method returns the information stored in the object as an array (i.e. a list in this context) which can be passed to the "new()" method to recreate the object.The first element of the return list will be the number itself. If the object uses "Math::BigFloat" for the internal representation, this element will be a copy of the internal object. Otherwise, it will be the internal representation of the number with full precision. Following the number will be all errors either as numbers, "Math::BigFloat" objects or arrays containing two asymmetric errors. (Either as numbers or objects as explained above.) The data returned by this method will be copied deeply before being returned. round_a_numberThis is a helper function which can round a number to the specified significant digit (defined as the return value of the "significant_digit" method):my $rounded = round_a_number(12.01234567, -3); # $rounded is now 1.2012e01 COMPARISONThis section lists methods that implement different comparisons between objects.numeric_cmpThis method implements a numeric comparison of two numbers. It compares the object it is called on to the first argument of the method. If the first argument is omitted or undefined, the method returns "undef".Numeric comparison means in this case that the represented numbers will be rounded and then compared. If you would like a comparison that takes care of errors, please have a look at the "full_cmp()" method. The method returns "-1" if the rounded number represented by the object is numerically less than the rounded number represented by the first argument. It returns 0 if they are equal and 1 if the object's rounded number is more than that of the argument. This method implements the overloaded numeric comparison operations. full_cmpThis method implements a full comparison of two objects. That means, it takes their numeric values, rounds them and compares them just like the "numeric_cmp()" method.If, however, the numbers are equal, this method iterates over the errors, rounds them and then compares them. If all errors are equal, this method returns 0. If an error is found to differ, the method returns 1 in case the object's error is larger and "-1" in case the argument's error is larger. Comparing an asymmetric error to a symmetric error is a special case. It can never be the same error, hence the method will not return 0. Instead, it guesses which error is larger by using the upper error bound of the asymmetric error. (Well, yes, not very useful.) SUPPORTBugs should be reported via the CPAN bug tracker at<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Number-WithError> For other issues, contact the author. SEE ALSOYou may use Math::BigFloat with this module. Also, it should be possible to use Math::Symbolic to calculate larger formulas. Just assign a "Number::WithError" object to the "Math::Symbolic" variables and it should work.You also possibly want to have a look at the prefork pragma. The test suite is implemented using the Test::LectroTest module. In order to keep the total test time in reasonable bounds, the default number of test attempts to falsify the test properties is kept at a low number of 100. You can enable more rigorous testing by setting the environment variable "PERL_TEST_ATTEMPTS" to a higher value. A value in the range of 1500 to 3000 is probably a good idea, but takes a long time to test. AUTHORSteffen Mueller <smueller@cpan.org>, <http://steffen-mueller.net/>COPYRIGHTCopyright 2006-2010 Steffen Mueller.This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Visit the GSP FreeBSD Man Page Interface. |