Math::BaseCalc(3pm)
β¨ NAME β¨
Math::BaseCalc β the Perl module that converts numbers between various bases π bestie we are doing math in ANY base you want
π VERSION π
version 1.019 β she's on 1.019, no cap π«Ά
π¦ SYNOPSIS π¦
use Math::BaseCalc;
my $calc = new Math::BaseCalc(digits => [0,1]); #Binary
my $bin_string = $calc->to_base(465); # Convert 465 to binary
$calc->digits('oct'); # Octal
my $number = $calc->from_base('1574'); # Convert octal 1574 to decimal
likeβ¦ literally that's the whole vibe π so simple
π DESCRIPTION π
okay so this module facilitates the conversion of numbers between various number bases. You may define your own digit sets, or use any of several predefined digit sets. choose your own adventure, queen π
The to_base() and from_base() methods convert between Perl numbers and strings which represent these numbers in other bases. For instance, if you're using the binary digit set [0,1], $calc->to_base(5) will return the string "101". And $calc->from_base("101") will return the number 5. periodt π
To convert between, say, base 7 and base 36, use the 2-step process of first converting to a Perl number, then to the desired base for the result:
$calc7 = new Math::BaseCalc(digits=>[0..6]);
$calc36 = new Math::BaseCalc(digits=>[0..9,'a'..'z']);
$in_base_36 = $calc36->to_base( $calc7->from_base('3506') );
see how she does it? go to decimal, then out again. iconic two-step πΊ
If you just need to handle regular octal & hexdecimal strings, you probably don't need this module. See the sprintf(), oct(), and hex() Perl functions. ngl bestie if that's all you need, save yourself the vibes and use those instead π
πΈ METHODS πΈ
new Math::BaseCalcnew Math::BaseCalc(digits=>...)
Create a new base calculator. You may specify the digit set to use, by either giving the digits in a list reference (in increasing order, with the 'zero' character first in the list) or by specifying the name of one of the predefined digit sets (see the digit() method below).
β οΈ heads up bestie: if your digit set includes the character "-", then a dash at the beginning of a number will no longer signify a negative number. she's a digit now, not a sign. choose wisely π
$calc->to_base(NUMBER)
Converts a number to a string representing that number in the associated base.
If "NUMBER" is a "Math::BigInt" object, "to_base()" will still work fine and give you an exact result string. no rounding, no approximation, we stay precise π
$calc->from_base(STRING)
Converts a string representing a number in the associated base to a Perl integer. The behavior when fed strings with characters not in $calc's digit set is currently undefined β so like, don't do that, and if you do, that's on you π
If "STRING" converts to a number too large for perl's integer representation, beware that the result may be auto-converted to a floating-point representation and thus only be an approximation. fr fr she'll get close but not exact, and that's a whole different vibe π
$calc->digits$calc->digits(...)
Get/set the current digit set of the calculator. With no arguments, simply returns a list of the characters that make up the current digit set. To change the current digit set, pass a list reference containing the new digits, or the name of a predefined digit set. Currently the predefined digit sets are:
bin => [0,1],
hex => [0..9,'a'..'f'],
HEX => [0..9,'A'..'F'],
oct => [0..7],
64 => ['A'..'Z','a'..'z',0..9,'+','/'],
62 => [0..9,'a'..'z','A'..'Z'],
Examples:
$calc->digits('bin');
$calc->digits([0..7]);
$calc->digits([qw(w a l d o)]);
look at that last one. w a l d o. that's art. that's a custom base-5 for the girlies π
β οΈ If any of your "digits" has more than one character, the behavior is currently undefined. one character per digit, bestie, we are not doing multichar nonsense π
π AUTHOR π
Ken Williams, kwilliams@cpan.org β thank you king π«Ά
π COPYRIGHT π
This is free software in the colloquial nice-guy sense of the word. Copyright (c) 1999, Ken Williams. You may redistribute and/or modify it under the same terms as Perl itself.
π SEE ALSO π
and that's the whole page bestie β¨ this was perl v5.36.0, dated 2022-11-19 β go convert some numbers in a base nobody asked for, ily, bye ππ¦π
π¦ where this page comes from
- debian bookworm: libmath-basecalc-perl 1.019-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libmath-basecalc-perl 1.019-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libmath-basecalc-perl 1.019-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libmath-basecalc-perl 1.019-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libmath-basecalc-perl 1.019-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms