new#

Build a Math::GMP object from a string or integer scalar.

Synopsis#

my $x = Math::GMP->new(123);
my $y = Math::GMP->new('123456789012345678901234567890');
my $z = Math::GMP->new('abcd', 36);         # base 36

What you get back#

A blessed reference of class Math::GMP. You can feed it to any overloaded operator, or to the named methods on this page.

The first argument is read as a string. A leading + is stripped, and embedded spaces and underscores are removed - so '1_000_000' and ' 1 000 000 ' both parse. When no base is given every character must be a digit, -, x or a hex letter, which admits the usual 0x-prefixed hex form; any other character dies with Argument to Math::GMP->new is not a string representing an integer. A string that passes this check but is not a valid number, such as 'abc' or '1-2', gives 0. With an explicit base (2 to 62) the usual GMP digit-set applies.

Edge cases#

  • Missing or false first argument gives Math::GMP of 0.

  • Floating-point input is not special-cased here; a stringified value with a decimal point or an exponent sign fails the character check and dies.

  • Base 0 (the default) means “guess from the prefix”: 0x for hex, 0 for octal, otherwise decimal.