Math::Ryu - perl interface to Ryu (float-to-string conversion).
This module uses the Ryu C implementation, which is included with this module's source. It is therefore unnecessary to download that Ryu code - but, should you wish to grab it, it's available at: https://github.com/ulfjack/ryu/tree/master/ryu
Convert an NV to a decimal string, such that no information is lost, yet keeping the string as short as possible. NOTE: NVs whose size is greater than 8 bytes are not currently catered for.
use Math::Ryu qw(:all); my $str = d2s(0.1); print $str; # 1E-1 $str = d2s(1.4 / 10); print $str; # 1.3999999999999999E-1 $str = d2s(sqrt 2); print $str; # 1.4142135623730951E0 # Also do the reverse of s2d() my $d = s2d('1E-1'); printf "%.17g\n", $d; # 0.10000000000000001
$nv = s2d($str); Converts the string ($str) to a vloating point value ($nv) and return $nv. $str = d2s($nv); $nv holds a floating point numeric value. $str is a string in floating point format, that accurately and succinctly represents the value held by $nv. $str, when correctly assigned back to a perl number, will always be equivalent to $nv. This in stark contrast to perl's interpolation which does not guarantee that the condition ("$nv" == $nv) is true, even for non-nan values of $nv. (Eg if $nv is sqrt 2.0 or 1.4/10 .) Also, $str will contain as few digits as possible, without compromising that accuracy. $str = d2fixed($nv, $digits); $str is a $digits-decimal fixed point representation of $nv. For example, d2sfixed(1 / 10000, 6) returns '0.000100'. $str = d2exp($nv, $digits); $str is a $digits-decimal floating point representation of $nv. For example, d2exp(1 / 10000, 6) returns '1.000000e-04'. $str = d2s_buffered($nv); # Not very useful Returns the same as d2s($nv). ($str, $len) = d2s_buffered_n($nv); # Not very useful $str is the same as returned by d2s($nv). $len is length($str). $str = d2fixed_buffered($nv, $digits); # Not very useful Returns the same as d2fixed($nv, $digits). ($str, $len) = d2fixed_buffered_n($nv, $digits); # Not very useful $str is the same as returned by d2fixed($nv, $digits). $len is length($str). $str = d2exp_buffered($nv, $digits); # Not very useful Returns the same as d2exp($nv, $digits). ($str, $len) = d2exp_buffered_n($nv, $digits); # Not very useful $str is the same as returned by d2exp($nv, $digits). $len is length($str).
Cover larger precision NV types (ie long double and __float128)
This perl module is free software; you may redistribute it and/or modify it under the same terms as Perl itself. However, the ".c" and ".h" files that ship with this distro are Copyright 2018 Ulf Adams, and can only be used under the the terms of version 2.0 the Apache License, or of version 1.0 of the Boost Software License. A copy of the Apache License (Apache_License.txt) is included with this distro. The Apache License can also be found at: http://www.apache.org/licenses/LICENSE-2.0 The Boost Software License can be found at: https://www.boost.org/LICENSE_1_0.txt This perl module is Copyright 2021 Sisyphus
Sisyphus <sisyphus at(@) cpan dot (.) org>
To install Math::Ryu, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Math::Ryu
CPAN shell
perl -MCPAN -e shell install Math::Ryu
For more information on module installation, please visit the detailed CPAN module installation guide.