| 1 | ############################################################################## | ||||||||||
| 2 | # | ||||||||||
| 3 | # Data::Tools::Math perl module | ||||||||||
| 4 | # Copyright (c) 2013-2024 Vladi Belperchinov-Shabanski "Cade" | ||||||||||
| 5 | # <cade@noxrun.com> <cade@bis.bg> <cade@cpan.org> | ||||||||||
| 6 | # http://cade.noxrun.com/ | ||||||||||
| 7 | # | ||||||||||
| 8 | # GPL | ||||||||||
| 9 | # | ||||||||||
| 10 | ############################################################################## | ||||||||||
| 11 | package Data::Tools::Math; | ||||||||||
| 12 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 1 | ▶ | use Exporter; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 1 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 1 | ▶ | use Data::Tools; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 1 | ▶ | use Math::BigFloat; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | |||||||||||
| 18 | 1 | our $VERSION = '1.60'; | |||||||||
| 19 | |||||||||||
| 20 | 1 | our @ISA = qw( Exporter ); | |||||||||
| 21 | 1 | our @EXPORT = qw( | |||||||||
| 22 | |||||||||||
| 23 | num_round | ||||||||||
| 24 | num_round_trunc | ||||||||||
| 25 | num_pow | ||||||||||
| 26 | |||||||||||
| 27 | ); | ||||||||||
| 28 | |||||||||||
| 29 | 1 | our %EXPORT_TAGS = ( | |||||||||
| 30 | |||||||||||
| 31 | 'all' => \@EXPORT, | ||||||||||
| 32 | 'none' => [], | ||||||||||
| 33 | |||||||||||
| 34 | ); | ||||||||||
| 35 | |||||||||||
| 36 | ############################################################################## | ||||||||||
| 37 | |||||||||||
| 38 | |||||||||||
| 39 | sub num_round | ||||||||||
| 40 | 10 | ▶ | { | ||||||||
|
Subroutine
num_round: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 41 | 10 | my $num = shift; # number | |||||||||
| 42 | 10 | my $dot = shift; # precision | |||||||||
| 43 | |||||||||||
| 44 | 10 | ▶ | return $num unless $dot >= 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 45 | 9 | my $bf = Math::BigFloat->new($num); | |||||||||
| 46 | 9 | $num = $bf->ffround(-abs($dot))->bstr(); | |||||||||
| 47 | 9 | return $num; | |||||||||
| 48 | } | ||||||||||
| 49 | |||||||||||
| 50 | sub num_round_trunc | ||||||||||
| 51 | 6 | ▶ | { | ||||||||
|
Subroutine
num_round_trunc: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 52 | 6 | my $num = shift; # number | |||||||||
| 53 | 6 | my $dot = shift; # precision | |||||||||
| 54 | |||||||||||
| 55 | 6 | ▶ | return $num unless $dot >= 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 56 | 5 | my $bf = Math::BigFloat->new($num); | |||||||||
| 57 | # NOTE: round mode is given to ffround() here, setting it on the object | ||||||||||
| 58 | # changes Math::BigFloat's global default and leaks into num_round() | ||||||||||
| 59 | 5 | $num = $bf->ffround(-abs($dot),'trunc')->bstr(); | |||||||||
| 60 | 5 | return $num; | |||||||||
| 61 | } | ||||||||||
| 62 | |||||||||||
| 63 | sub num_pow | ||||||||||
| 64 | 6 | ▶ | { | ||||||||
|
Subroutine
num_pow: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 65 | 6 | my $num = shift; # number | |||||||||
| 66 | 6 | my $exp = shift; # exponent | |||||||||
| 67 | |||||||||||
| 68 | 6 | my $bf = Math::BigFloat->new($num); | |||||||||
| 69 | 6 | $num = $bf->bpow($exp)->bstr(); | |||||||||
| 70 | 6 | return $num; | |||||||||
| 71 | } | ||||||||||
| 72 | |||||||||||
| 73 | ############################################################################## | ||||||||||
| 74 | |||||||||||
| 75 | =pod | ||||||||||
| 76 | |||||||||||
| 77 | |||||||||||
| 78 | =head1 NAME | ||||||||||
| 79 | |||||||||||
| 80 | Data::Tools::Math provides set of basic functions for mathematics. | ||||||||||
| 81 | |||||||||||
| 82 | =head1 SYNOPSIS | ||||||||||
| 83 | |||||||||||
| 84 | use Data::Tools::Math qw( :all ); # import all functions | ||||||||||
| 85 | use Data::Tools::Math; # the same as :all :) | ||||||||||
| 86 | use Data::Tools::Math qw( :none ); # do not import anything | ||||||||||
| 87 | |||||||||||
| 88 | # -------------------------------------------------------------------------- | ||||||||||
| 89 | |||||||||||
| 90 | |||||||||||
| 91 | # -------------------------------------------------------------------------- | ||||||||||
| 92 | |||||||||||
| 93 | =head1 FUNCTIONS | ||||||||||
| 94 | |||||||||||
| 95 | =head2 num_round( $number, $precision ) | ||||||||||
| 96 | |||||||||||
| 97 | Rounds $number to $precisioun places after the decimal point. | ||||||||||
| 98 | |||||||||||
| 99 | =head2 num_round_trunc( $number, $precision ) | ||||||||||
| 100 | |||||||||||
| 101 | Same as num_round() but just truncates after the $precision places. | ||||||||||
| 102 | |||||||||||
| 103 | =head2 num_pow( $number, $exponent ) | ||||||||||
| 104 | |||||||||||
| 105 | Returns power of $number by $exponent ( $num ** $exp ) | ||||||||||
| 106 | |||||||||||
| 107 | =head1 REQUIRED MODULES | ||||||||||
| 108 | |||||||||||
| 109 | Data::Tools::Time uses: | ||||||||||
| 110 | |||||||||||
| 111 | * Math::BigFloat | ||||||||||
| 112 | |||||||||||
| 113 | =head1 GITHUB REPOSITORY | ||||||||||
| 114 | |||||||||||
| 115 | git@github.com:cade-vs/perl-data-tools.git | ||||||||||
| 116 | |||||||||||
| 117 | git clone git://github.com/cade-vs/perl-data-tools.git | ||||||||||
| 118 | |||||||||||
| 119 | =head1 AUTHOR | ||||||||||
| 120 | |||||||||||
| 121 | Vladi Belperchinov-Shabanski "Cade" | ||||||||||
| 122 | <cade@noxrun.com> <cade@bis.bg> <cade@cpan.org> | ||||||||||
| 123 | http://cade.noxrun.com/ | ||||||||||
| 124 | |||||||||||
| 125 | |||||||||||
| 126 | =cut | ||||||||||
| 127 | |||||||||||
| 128 | ############################################################################## | ||||||||||
| 129 | 1 | 1; | |||||||||