.\" Automatically generated by Pod::Man 4.10 (Pod::Simple 3.35) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). .\" Fear. Run. Save yourself. No user-serviceable parts. . \" fudge factors for nroff and troff .if n \{\ . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] \fP .\} .if t \{\ . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff .if n \{\ . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} .if t \{\ . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' .ds 8 \h'\*(#H'\(*b\h'-\*(#H' .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "Number::Fraction 3pm" .TH Number::Fraction 3pm "2019-02-22" "perl v5.28.1" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Number::Fraction \- Perl extension to model fractions .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& use Number::Fraction; \& \& my $f1 = Number::Fraction\->new(1, 2); \& my $f2 = Number::Fraction\->new(\*(Aq1/2\*(Aq); \& my $f3 = Number::Fraction\->new($f1); # clone \& my $f4 = Number::Fraction\->new; # 0/1 .Ve .PP or .PP .Vb 1 \& use Number::Fraction \*(Aq:constants\*(Aq; \& \& my $f1 = \*(Aq1/2\*(Aq; \& my $f2 = $f1; \& \& my $one = $f1 + $f2; \& my $half = $one \- $f1; \& print $half; # prints \*(Aq1/2\*(Aq .Ve .SH "ABSTRACT" .IX Header "ABSTRACT" Number::Fraction is a Perl module which allows you to work with fractions in your Perl programs. .SH "DESCRIPTION" .IX Header "DESCRIPTION" Number::Fraction allows you to work with fractions (i.e. rational numbers) in your Perl programs in a very natural way. .PP It was originally written as a demonstration of the techniques of overloading. .PP If you use the module in your program in the usual way .PP .Vb 1 \& use Number::Fraction; .Ve .PP you can then create fraction objects using \f(CW\*(C`Number::Fraction\-\*(C'\fRnew> in a number of ways. .PP .Vb 1 \& my $f1 = Number::Fraction\->new(1, 2); .Ve .PP creates a fraction with a numerator of 1 and a denominator of 2. .PP .Vb 1 \& my $f2 = Number::Fraction\->new(\*(Aq1/2\*(Aq); .Ve .PP does the same thing but from a string constant. .PP .Vb 1 \& my $f3 = Number::Fraction\->new($f1); .Ve .PP makes \f(CW$f3\fR a copy of \f(CW$f1\fR .PP .Vb 1 \& my $f4 = Number::Fraction\->new; # 0/1 .Ve .PP creates a fraction with a denominator of 0 and a numerator of 1. .PP If you use the alterative syntax of .PP .Vb 1 \& use Number::Fraction \*(Aq:constants\*(Aq; .Ve .PP then Number::Fraction will automatically create fraction objects from string constants in your program. Any time your program contains a string constant of the form \f(CW\*(C`\ed+/\ed+\*(C'\fR then that will be automatically replaced with the equivalent fraction object. For example .PP .Vb 1 \& my $f1 = \*(Aq1/2\*(Aq; .Ve .PP Having created fraction objects you can manipulate them using most of the normal mathematical operations. .PP .Vb 2 \& my $one = $f1 + $f2; \& my $half = $one \- $f1; .Ve .PP Additionally, whenever a fraction object is evaluated in a string context, it will return a string in the format x/y. When a fraction object is evaluated in a numerical context, it will return a floating point representation of its value. .PP Fraction objects will always \*(L"normalise\*(R" themselves. That is, if you create a fraction of '2/4', it will silently be converted to '1/2'. .SS "Experimental Support for Exponentiation" .IX Subsection "Experimental Support for Exponentiation" Version 1.13 of Number::Fraction adds experimental support for exponentiation operations. If a Number::Fraction object is used as the left hand operand of an exponentiation expression then the value returned will be another Number::Fraction object \- if that makes sense. In all other cases, the expression returns a real number. .PP Currently this only works if the right hand operand is an integer (or a Number::Fraction object that has a denominator of 1). Later I hope to extend this so support so that a Number::Fraction object is returned whenever the result of the expression is a rational number. .PP For example: .PP .Vb 4 \& \*(Aq1/2\*(Aq ** 2 # Returns a Number::Fraction (\*(Aq1/4\*(Aq) \& \*(Aq2/1\*(Aq ** \*(Aq2/1\*(Aq Returns a Number::Fraction (\*(Aq4/1\*(Aq) \& \*(Aq2/1\*(Aq ** \*(Aq1/2\*(Aq Returns a real number (1.414213) \& 0.5 ** \*(Aq2/1\*(Aq Returns a real number (0.25) .Ve .SS "Version 2: Now With Added Moose" .IX Subsection "Version 2: Now With Added Moose" Version 2 of Number::Fraction has been reimplemented using Moose. You should see very little difference in the way that the class works. The only difference I can see is that \f(CW\*(C`new\*(C'\fR used to return \f(CW\*(C`undef\*(C'\fR if it couldn't create a valid object from its arguments, it now dies. If you aren't sure of the values that are being passed into the constructor, then you'll want to call it within an \&\f(CW\*(C`eval { ... }\*(C'\fR block (or using something equivalent like Try::Tiny). .SH "METHODS" .IX Header "METHODS" .SS "import" .IX Subsection "import" Called when module is \f(CW\*(C`use\*(C'\fRd. Use to optionally install constant handler. .SS "unimport" .IX Subsection "unimport" Be a good citizen and uninstall constant handler when caller uses \&\f(CW\*(C`no Number::Fraction\*(C'\fR. .SS "\s-1BUILDARGS\s0" .IX Subsection "BUILDARGS" Parameter massager for Number::Fraction object. Takes the following kinds of parameters: .IP "\(bu" 4 A single Number::Fraction object which is cloned. .IP "\(bu" 4 A string in the form 'x/y' where x and y are integers. x is used as the numerator and y is used as the denominator of the new object. .IP "\(bu" 4 Two integers which are used as the numerator and denominator of the new object. .IP "\(bu" 4 A single integer which is used as the numerator of the the new object. The denominator is set to 1. .IP "\(bu" 4 No arguments, in which case a numerator of 0 and a denominator of 1 are used. .PP Dies if a Number::Fraction object can't be created. .SS "\s-1BUILD\s0" .IX Subsection "BUILD" Object initialiser for Number::Fraction. Ensures that fractions are in a normalised format. .SS "to_string" .IX Subsection "to_string" Returns a string representation of the fraction in the form \&\*(L"numerator/denominator\*(R". .SS "to_num" .IX Subsection "to_num" Returns a numeric representation of the fraction by calculating the sum numerator/denominator. Normal caveats about the precision of floating point numbers apply. .SS "add" .IX Subsection "add" Add a value to a fraction object and return a new object representing the result of the calculation. .PP The first parameter is a fraction object. The second parameter is either another fraction object or a number. .SS "mult" .IX Subsection "mult" Multiply a fraction object by a value and return a new object representing the result of the calculation. .PP The first parameter is a fraction object. The second parameter is either another fraction object or a number. .SS "subtract" .IX Subsection "subtract" Subtract a value from a fraction object and return a new object representing the result of the calculation. .PP The first parameter is a fraction object. The second parameter is either another fraction object or a number. .SS "div" .IX Subsection "div" Divide a fraction object by a value and return a new object representing the result of the calculation. .PP The first parameter is a fraction object. The second parameter is either another fraction object or a number. .SS "exp" .IX Subsection "exp" Raise a Number::Fraction object to a power. .PP The first argument is a number fraction object. The second argument is another Number::Fraction object or a number. If the second argument is an integer or a Number::Fraction object containing an integer then the value returned is a Number::Fraction object, otherwise the value returned is a real number. .SS "abs" .IX Subsection "abs" Returns a copy of the given object with both the numerator and denominator changed to positive values. .SS "\s-1EXPORT\s0" .IX Subsection "EXPORT" None by default. .SH "SEE ALSO" .IX Header "SEE ALSO" perldoc overload .SH "AUTHOR" .IX Header "AUTHOR" Dave Cross, .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" Copyright 2002\-8 by Dave Cross .PP This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.