Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Need more precision.

by BrowserUk (Patriarch)
on Jun 09, 2015 at 20:22 UTC ( [id://1129700]=perlquestion: print w/replies, xml ) Need Help??

BrowserUk has asked for the wisdom of the Perl Monks concerning the following question:

I'm running out of precision using doubles.

My compiler has long doubles, but they map to doubles.

Arbitrary precision isn't an option. Nor is changing my compiler.

I don't need more range, nothing greater than 4.0.

All I need is multiply, add and subtract.

What are my options?


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked

Replies are listed 'Best First'.
Re: Need more precision.
by RichardK (Parson) on Jun 09, 2015 at 22:46 UTC

    Did you see this ? Quadruple-precision_floating-point_format

    It looks like there are a few libraries, at least one, QPFloat, with a windows version.

    I haven't used it myself, so I know nothing else about it ;)

      It looks like there are a few libraries, at least one, QPFloat, with a windows version.

      Sorry. Did you see one that specifically stated that MSVC was supported? (I'm looking again, but not seeing it?)


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
      In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked
Re: Need more precision.
by roboticus (Chancellor) on Jun 09, 2015 at 21:40 UTC

    BrowserUk:

    Update: I just read the article in more detail, and I don't think it's what you want.

    I've not used it, but the xmmintrin.h header file contains some functions to work with SSE numbers. You might look at it see if it suits. An article I tripped over seems to provide some explanation:http://www.codeproject.com/Articles/4522/Introduction-to-SSE-Programming. I only skimmed the article, so I don't know if it has enough detail to get you going.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      I just read the article in more detail, and I don't think it's what you want.

      Indeed. I wasted an afternoon trying to find some way to get more precision out of SSE/2 but they are about SIMD; performing multiple (2xdouble or 4xfloat) operations in parallel.

      I don't see any way to get more precision out of them. I thought for a while it might be possible to use two doubles as a kind limited kind of multi-precision, but there is no carry-over facility.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
      In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked
Re: Need more precision.
by Tux (Canon) on Jun 10, 2015 at 06:25 UTC

    As nobody mentioned this so far, I'll bite on "perl has not and my compiler has" part. Perl by default configures/compiles to "normal" doubles, but it has an option to use long doubles for NV types.

    Configure your perl with -Duselongdouble

    $ perl -V:uselongdouble\|nvsize nvsize='16'; uselongdouble='define'; $ perl -MData::Dumper -wE'say Dumper cos 1' $VAR1 = '0.540302305868139717';
    $ perl5.10.1 -V:uselongdouble\|nvsize nvsize='8'; uselongdouble='undef'; $ perl5.10.1 -MData::Dumper -wE'say Dumper cos 1' $VAR1 = '0.54030230586814'; $

    Enjoy, Have FUN! H.Merijn
      Configure your perl with -Duselongdouble

      On Windows, you can build in long double support only with perl-5.22.0 and later ... and even then only if you're using a MinGW port of the gcc compiler.

      Cheers,
      Rob

      My compiler (MSVC) "supports" the long double type, but "The long double type is identical to the double type."

      So it does me no good at all. Thanks for the thought though :)


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
      In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked
Re: Need more precision.
by Anonymous Monk on Jun 09, 2015 at 21:39 UTC

    Hmm, going down the fixed-point route, in 2011 you asked: Module for 128-bit integer math?, which resulted in Math::Int128... test stats in Windows look a bit spotty, but it looks like it's actively maintained and maybe worth a try?

      That was integer; this is float.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
      In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked

        Got that, but Fixed-point arithmetic "is essentially an integer that is scaled by an implicit specific factor determined by the type. For example, the value 1.23 can be represented as 1230 in a fixed-point data type with scaling factor of 1/1000". This should work since you said your min/max is -4.0 to 4.0, which could be represented in a 128-bit integer as, for example, 0 to 80000000000000000000000000000000000000.

      ... oh wait... are you asking about Perl or C?

Re: Need more precision.
by u65 (Chaplain) on Jun 09, 2015 at 20:56 UTC

    Why is arbitrary precision not an alternative? There are several CPAN modules offering such (e.g., Math::BigFloat).

      Let me clarify.

      I know about arbitrary precision. I mentioned it in the OP.

      So, if it was a viable option, there would be no need to ask the question, because I would just use that.

      I'm looking to see what alternatives there are; so you questioning why I don't want to use arbitrary precision does nothing to help me in my quest.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
      In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked
        Again I respectfully ask, what all constraints are you under, otherwise this seems to be a rabbit hunt.

      Because it's not an alternative. Availability isn't the reason.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
      In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked
        Perhaps you could enlighten us on all the constraints you have to labor under.
Re: Need more precision.
by Anonymous Monk on Jun 09, 2015 at 20:35 UTC

    Min and max?

    Just a few digits more, or even more than that?

    Which compiler?

      MIn & max: +-4.0

      At least 80-bits/18 digits, 128-bits/34 digits. would be perfect.

      MSVC.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
      In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked

        How about fixed point arithmetic? If 18 digits really is "good enough", then a signed 64 bit int will give you 18 digits (int part plus 17 decimal places). If you need more, does the compiler have a 128 bit int type? If not, you could code routines that cascade a signed 64 with an unsigned 64 to give you a signed 128.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1129700]
Approved by 2teez
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2024-04-24 12:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found