Giter Club home page Giter Club logo

math-nv's Introduction

Perl fails to assign many floating point values correctly ... and by "many", I mean millions.
(Perhaps there are some platforms where this is not the case, but I don't know of any.)

The errors are not huge (generally off-by-one or off-by-two errors), and many people are not concerned
about them.

Anyway, this module just provides a few functions for examining the values, and an nv() function that
assigns the values via the C compiler/libc.
On perl builds that have an nvtype of "double", the nv() function is essentially the same as the
POSIX::strtod() function.
Unfortunately, the POSIX module doesn't provide a strtold() function (prior to 5.21), and can therefore
not be used for the same purpose on builds of perl where the nvtype is "long double". The nv() function
does, however, also work on "long double" builds of perl.
It also works on perls (5.21.4 and later) where $config{nvtype} can be '__float128'.

Then there's always the possibility of bugs in C's strtod()/strtold() functions. My mingw.org port
of gcc-4.7.0 suffers a bug in strtod(), though it's the only compiler I've found to be buggy in this
regard. It's a bug that doesn't strike very often, but it's enough to destroy one's confidence that
strtod() is behaving correctly. (Just my luck !!)

If you want to be really paranoid about it, the surest way of assigning the correct floating point
value in perl is to have the mpfr library assign the value. That is, instead of doing:

my $nv = 1e-298;

Do something like:

use Math::MPFR qw(:mpfr);
Rmpfr_set_default_prec(Math::MPFR::_DBL_MANT_DIG());    # if nvtype is double
# alternatively:
# Rmpfr_set_default_prec(Math::MPFR::_LDBL_MANT_DIG()); # if nvtype is long double
# or
# Rmpfr_set_default_prec(Math::MPFR::_FLT128_MANT_DIG()); # if nvtype is __float128
my $nv = Rmpfr_get_NV(Math::MPFR->new('1e-298'), MPFR_RNDN);

Cheers,
Rob

math-nv's People

Contributors

sisyphus avatar

Watchers

 avatar  avatar

Forkers

szabgab

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.