Tell what is encoded in a float, given its HEX bytes

dc -e"16i?dsH0sq2d17^ss8^dse2/1-stdlsle*/2*2B+an[[ FP Indef.]n]sQ[dls2//2%_2*53+an[NaN]ndle4*1-ls2/*=Q2Q]sN[1sqdls%0<N[oo]n]sMdls/le%dsdle1-=M[[]pq]sPlq1=P[r+0]s0ldd1r0=0lHls%rls*+sS2r^Alt4*^*lS*2lt^/ls/dsSZlt4*-1-sFlsZ1+klSdArZ1-^/dn0=P[e]nlFp"
It handles all possible combination of the hex bytes, including NaNs, Infinities, Normalized and Subnormal Numbers... This crazy DC stuff spent me a few days to write, optimize, polish and squeeze so that it works within the tight 255 character bound... You can modify it easily for other IEEE754 numbers, say, half, double, double-extended, quadruple (I hope someone will find this useful and submit more dc code to commandlinefu!)
Sample Output
$> echo "7F7FFFFF" | dc -e"<this stuff>"
+3.40282346e38
$> echo "7F800000" | dc -e"<this stuff>"
+oo
$> echo "7FC00000" | dc -e"<this stuff>"
+QNaN
$> echo "FFC00000" | dc -e"<this stuff>"
-QNaN FP Indef.

1
By: billyauhk
2011-07-27 18:55:20

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: