Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
No such thing as a small change.
 
PerlMonks

IEEE 754 80-bit Extended double (long double) to 64-bit double unpack

by BrowserUk (Apostle)
 | Log in | Create a new user | The Monastery Gates | Super Search | 
 | Seekers of Perl Wisdom | Meditations | PerlMonks Discussion | 
 | Obfuscation | Reviews | Cool Uses For Perl | Perl News | Q&A | Tutorials | 
 | Poetry | Recent Threads | Newest Nodes | Donate | What's New | 

on Nov 30, 2006 at 12:57 UTC ( #586923=snippet: print w/ replies, xml ) Need Help??

Description:

This accepts a 10-byte binary encoded IEEE 754 80-bit real and unpacks it into a IEEE 64-bit real (Perl NV).

Testing is limited to Intel P4 (Does anyone else do 80-bit FP math?) and Perl 5.8.6/7 under WinXP.

The test data below shows a selected range of values and compares the output from the generating program's vprintf equivalent, and Perl's printf on the converted results. The values are selected to highlight the range and accuracy limitations of both formats at all four! ends of the scale.

It shows correct handling of underflow and overflow by conversion to [+|-]1.#INF as appropriate.

I believe it will handle #NaN (& #QNaN) correctly, but this has not been tested, as I haven't worked out how to generate these in the writer.

A version that doesn't go through ASCII encoded binary has so far eluded me due to the limitations of 32-bit integers for the bit twiddling.

Copyright (©) 2006. BrowserUk (BrowserUk -> cpan org).
sub IEEE80toIEEE64 {
    my( $discard, $mantissa, $hidden, $exponent, $sign ) =
        unpack 'a11 a52 a1 a15 a1', unpack 'b80', $_[ 0 ];
    $exponent = unpack( 'v', pack 'b15', $exponent ) - 16383 + 1023;
    $exponent = 32767, $mantissa = '0' x 52
        if $exponent < 0 or $exponent > 2047;
    $exponent = unpack 'b11', pack 'v', $exponent;
    return unpack 'd', pack 'b64', $mantissa . $exponent . $sign;
}

realtest x.bin                  C:\dmd\test>\test\80bit.pl
-1.0935598595647423806e-4950    -1.#INF
 1.0935598595647423806e-4950     1.#INF
-1.2345678901234567887e-4932    -1.#INF
 1.2345678901234567887e-4932     1.#INF
-1.2345678901234567887e-309     -1.#INF
 1.2345678901234567887e-309      1.#INF
-1.2345678901234567912e-206     -1.2345678901234567e-206
 1.2345678901234567912e-206      1.2345678901234567e-206
-1.2345678901234567937e-103     -1.2345678901234568e-103
 1.2345678901234567937e-103      1.2345678901234568e-103
-1.234567890123456796           -1.2345678901234567
 1.234567890123456796            1.2345678901234567
-1.2345678901234567983e+103     -1.2345678901234567e+103
 1.2345678901234567983e+103      1.2345678901234567e+103
-1.2345678901234568006e+206     -1.2345678901234568e+206
 1.2345678901234568006e+206      1.2345678901234568e+206
-1.2345678901234568031e+309     -1.#INF
 1.2345678901234568031e+309      1.#INF
-1.2345678901234567889e+4930    -1.#INF
 1.2345678901234567889e+4930     1.#INF
-1.2345678901234567892e+4931    -1.#INF
 1.2345678901234567892e+4931     1.#INF
 0.005000                        0.0049999999999999992
-0.005000                       -0.0049999999999999992
 0.050000                        0.049999999999999996
-0.050000                       -0.049999999999999996
 0.500000                        0.5
-0.500000                       -0.5
 5.000000                        5
-5.000000                       -5
 50.000000                       50
-50.000000                      -50

Comment on IEEE 754 80-bit Extended double (long double) to 64-bit double unpack
Select or Download Code

Back to Snippets Section

Login:
Password
remember me
What's my password?
Create A New User

Node Status?
node history
Node Type: snippet [id://586923]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (24)
ikegami
GrandFather
Limbic~Region
CountZero
marto
jmcnamara
toolic
holli
Gavin
atcroft
kennethk
thezip
Eyck
Marshall
socketdave
superfrink
crashtest
pemungkah
ssandv
Rodster001
CColin
MikeDexter
johnvk
im2
As of 2010-02-09 21:37 GMT
Sections?
The Monastery Gates
Seekers of Perl Wisdom
Meditations
PerlMonks Discussion
Categorized Q&A
Tutorials
Obfuscated Code
Perl Poetry
Cool Uses for Perl
Perl News
Information?
PerlMonks FAQ
Guide to the Monastery
What's New at PerlMonks
Voting/Experience System
Tutorials
Reviews
Library
Perl FAQs
Other Info Sources
Find Nodes?
Nodes You Wrote
Super Search
List Nodes By Users
Newest Nodes
Recently Active Threads
Selected Best Nodes
Best Nodes
Worst Nodes
Saints in our Book
Leftovers?
The St. Larry Wall Shrine
Offering Plate
Awards
Craft
Snippets Section
Code Catacombs
Quests
Editor Requests
Buy PerlMonks Gear
PerlMonks Merchandise
Planet Perl
Perlsphere
Use Perl
Perl.com
Perl 5 Wiki
Perl Jobs
Perl Mongers
Perl Directory
Perl documentation
CPAN
Random Node
Voting Booth?

What level of existential comfort do you require?

Palace
Executive suite at the best hotel
Regular hotel in a decent part of town
Motel
Boarding house
Sleeping Bag on Couch in Basement
Any port in a storm
Camping under the freeway overpass
Jail
Other

Results (281 votes), past polls