Sisyphus >
Math-LongDouble >
Math::LongDouble

Module Version: 0.04
Math::LongDouble - perl interface to C's long double operations (for perls that don't already have that capability)

This module has bugs on perls built with a Microsoft compiler (eg ActivePerl) - even if the binaries installed onto the MSVC-built perl were built using MinGW on a MinGW-built perl such as Strawberry Perl (where no such problem exists). By some means that is still unclear, the 'long double' precision can apparently be reduced to 'double' precision whenever a Math::LongDouble object is raised to a power (or a square root taken) on MSVC-built perls. This bug manifests itself in causing some test failures in t/cmp.t and t/pow.t.

If your perl's NV is a 'long double', then there's no point in using this module. But if your perl's NV is a 'double', then this module provides you with a way of performing arithmetic operations with long double precision. use Math::LongDouble qw(:all); my $arg = 32.1; my $ld1 = Math::LongDouble->new($arg);# Stringify $arg, then assign # using C's strtold() my $ld2 = NVtoLD($arg); # Assign the NV 32.1 to $ld2.

The following operations are overloaded: + - * / ** += -= *= /= **= != == <= >= <=> < > ++ -- = abs bool ! int print sqrt log exp sin cos atan2 Arguments to the overloaded operations must be Math::LongDouble objects. $ld = $ld + 3.1; # currently an error. Do instead: $ld = $ld + Math::LongDouble->new('3.1');

The following create and assign a new Math::LongDouble. $ld = Math::LongDouble->new($arg); Returns a Math::LongDouble object to which the numeric value of $arg has been assigned. If no arg is supplied then $ld will be NaN. $ld = UVtoLD($arg); Returns a Math::LongDouble object to which the numeric (unsigned integer) value of $arg has been assigned. $ld = IVtoLD($arg); Returns a Math::LongDouble object to which the numeric (signed integer) value of $arg has been assigned. $ld = NVtoLD($arg); Returns a Math::LongDouble object to which the numeric (floating point) value of $arg has been assigned. $ld2 = LDtoLD($ld1); Returns a Math::LongDouble object that is a copy of the Math::LongDouble object provided as the argument. Courtesy of overloading, this is in effect no different to doing: $ld2 = $ld1; $ld = STRtoLD($str); Returns a Math::LongDouble object that has the value of the string $str.

$ld = InfLD($sign); If $sign < 0, returns a Math::LongDouble object set to negative infinity; else returns a Math::LongDouble object set to positive infinity. $ld = NaNLD($sign); If $sign < 0, returns a Math::longDouble object set to negative NaN; else returns a Math::LongDouble object set to positive NaN. It may be problematical as to whether a NaN with the correct sign has been returned ... but, either way, it should return a NaN. $ld = ZeroLD($sign); If $sign < 0, returns a Math::LongDouble object set to negative zero; else returns a Math::LongDouble object set to zero. $ld = UnityLD($sign); If $sign < 0, returns a Math::LongDouble object set to negative one; else returns a Math::LongDouble object set to one. ld_set_prec($precision); Sets the precision of stringified values to $precision decimal digits. $precision = ld_get_prec(); Returns the precision (in decimal digits) that will be used when stringifying values (by printing them, or calling LDtoSTR).

The following functions provide ways of seeing the value of Math::LongDouble objects. $nv = LDtoNV($ld); This function returns the value of the Math::LongDouble object to a perl scalar (NV). It may not translate the value accurately. $string = LDtoSTR($ld); Returns the value of the Math::LongDouble object as a string. The returned string will contain the same as is displayed by "print $ld", except that print() will strip the trailing zeroes in the mantissa (significand) whereas LDtoSTR won't. By default, provides 18 decimal digits of precision. This can be altered by specifying the desired precision (in decimal digits) in a call to ld_set_prec. $string = LDtoSTRP($ld, $precision); Same as LDtoSTR, but takes an additional arg that specifies the precision (in decimal digits) of the stringified return value.

$bool = is_NaNLD($ld); Returns 1 if $ld is a Math::LongDouble NaN. Else returns 0 $int = is_InfLD($ld) If the Math::LongDouble object $ld is -inf, returns -1. If it is +inf, returns 1. Otherwise returns 0. $int = is_ZeroLD($ld); If the Math::LongDouble object $ld is -0, returns -1. If it is zero, returns 1. Otherwise returns 0. $int = cmp_NV($ld, $nv); $nv can be any perl number - ie NV, UV or IV. If the Math::LongDouble object $ld < $nv returns -1. If it is > $nv, returns 1. Otherwise returns 0.

This program is free software; you may redistribute it and/or modify it under the same terms as Perl itself. Copyright 2012, 2013 Sisyphus

Sisyphus <sisyphus at(@) cpan dot (.) org>

syntax highlighting: