473,385 Members | 1,806 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

components of a floating point value


I would like to portably extract the components of a floating point value:
sign, exponent, and significand. Based on the C floating point model
described in 5.2.4.2.2, I came up with the following code. There are
a couple of things to note about this code.

1) I convert the significand to an integer value in which the least
significant bit(s) of the integer correspond/s to the least significant
digit of the significand. This means that only as many low-order bits as
needed to encode DBL_MANT_DIG digits of the floating point radix are
significant in the integer value.

2) I assume that an unsigned long long int is large enough to represent
the significand. If this isn't true, the integer value used to represent
the significand will silently overflow.

I am interested in comments relating to the correctness/portability of the
code, pitfalls of the approach, tips on better/easier methods, etc.

#include <float.h>
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

static void extract_double_components (double s)
{
int sign;
double tmp;

tmp = copysign(1.0, s);

sign = +1;
if (tmp < 0.0) sign = -1;

if (isinf(s))
{
if (sign < 0)
printf("-Infinity\n");
else
printf("+Infinity\n");
}
else if (isnan(s))
{
printf("NaN\n");
}
else if (s == 0.0)
{
if (sign < 0)
printf("-0.0\n");
else
printf("+0.0\n");
}
else
{
unsigned idx;
long exponent;
unsigned long long significand;

tmp = fabs(s);

/* find the exponent */
exponent = 0;
if (tmp < 1.0)
{
while (tmp < 1.0)
{
tmp = tmp * FLT_RADIX;
--exponent;
}
}
else
{
while (tmp >= 1.0)
{
tmp = tmp / FLT_RADIX;
++exponent;
}
tmp = tmp * FLT_RADIX; /* return to the range 1..2 */
}

/* find the significand */
significand = 0;
for (idx = 0; idx < DBL_MANT_DIG; ++idx)
{
significand = significand * FLT_RADIX;
tmp = tmp * FLT_RADIX;

if (tmp >= FLT_RADIX)
{
++significand;
tmp = tmp - FLT_RADIX;
}
}

printf("sign:%c significand:%llx exponent:%ld\n",
sign > 0 ? '+' : '-', significand, exponent);
}
}

int main (int argc, char *argv[])
{
double d;

if (argc != 2) { printf("usage: %s <double>\n", argv[0]); return -1; }
d = strtod(argv[1], NULL);
extract_double_components(d);
return 0;
}

A few test runs:

[sheldon@wsxyz mcc]$ ./a.out -Inf
-Infinity
[sheldon@wsxyz mcc]$ ./a.out 3.0
sign:+ significand:18000000000000 exponent:2
[sheldon@wsxyz mcc]$ ./a.out 1.5
sign:+ significand:18000000000000 exponent:1
[sheldon@wsxyz mcc]$ ./a.out 0.75
sign:+ significand:18000000000000 exponent:-1
[sheldon@wsxyz mcc]$ ./a.out 3.141592654
sign:+ significand:1921fb54524550 exponent:2
[sheldon@wsxyz mcc]$ ./a.out 2e-320
sign:+ significand:1fa00000000000 exponent:-1063
[sheldon@wsxyz mcc]$ ./a.out 2e-322
sign:+ significand:14000000000000 exponent:-1069
[sheldon@wsxyz mcc]$ ./a.out 2e-324
+0.0
[sheldon@wsxyz mcc]$ ./a.out -0.0
-0.0

Nov 13 '05 #1
2 1848

"Sheldon Simms" <sh**********@yahoo.com> wrote in message
news:pa****************************@yahoo.com...

I would like to portably extract the components of a floating point value:
sign, exponent, and significand. Based on the C floating point model
described in 5.2.4.2.2, I came up with the following code. There are
a couple of things to note about this code.

1) I convert the significand to an integer value in which the least
significant bit(s) of the integer correspond/s to the least significant
digit of the significand. This means that only as many low-order bits as
needed to encode DBL_MANT_DIG digits of the floating point radix are
significant in the integer value.

2) I assume that an unsigned long long int is large enough to represent
the significand. If this isn't true, the integer value used to represent
the significand will silently overflow.


(snip)

There are machines with 128 bit floating point representations, with
something like 112 bits for mantissa.

You might consider whether your code works correctly for FLT_RADIX of 10 or
16. There is work on a standard for FLT_RADIX of 10, though with a slightly
more compact storage format than BCD. IBM has been making machines with
FLT_RADIX of 16 for almost 40 years now.

-- glen
Nov 13 '05 #2
On Thu, 13 Nov 2003 20:40:57 +0000, Glen Herrmannsfeldt wrote:

"Sheldon Simms" <sh**********@yahoo.com> wrote in message
news:pa****************************@yahoo.com...

2) I assume that an unsigned long long int is large enough to represent
the significand. If this isn't true, the integer value used to represent
the significand will silently overflow.
(snip)

There are machines with 128 bit floating point representations, with
something like 112 bits for mantissa.


I'm aware of this. That's why I mentioned that the code makes the
assumption that it does.
You might consider whether your code works correctly for FLT_RADIX of 10 or
16. There is work on a standard for FLT_RADIX of 10, though with a slightly
more compact storage format than BCD. IBM has been making machines with
FLT_RADIX of 16 for almost 40 years now.


I have tried to make it radix neutral by using the value FLT_RADIX
everywhere it makes sense (except in a comment, I just noticed). Do
you see a problem with the code?
Nov 13 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Roger Leigh | last post by:
Hello, I'm writing a fixed-precision floating point class, based on the ideas in the example fixed_pt class in the "Practical C++ Programming" book by Steve Oualline (O' Reilly). This uses a...
10
by: Vinny | last post by:
I have a few floating point questions/issues I wish to ask. An answer or discussion would be nice too :) Any links discussion the subject would be nice too.. 1. How do I generate an underflow...
5
by: Anton Noll | last post by:
We are using Visual Studio 2003.NET (C++) for the development of our software in the fields digital signal processing and numerical acoustics. One of our programs was working correctly if we are...
12
by: Dave Rahardja | last post by:
Does the C++ standard specify the behavior of floating point numbers during "exceptional" (exceptional with respect to floating point numbers, not exceptions) conditions? For example: double...
21
by: Allin Cottrell | last post by:
OK, I realize that what I am asking here is not likely to have a answer within the C standard. Nonetheless, it is not specific to any particular platform, so I'll hazard the question anyway. A...
7
by: Daniel Vallstrom | last post by:
I am having trouble with floating point addition because of the limited accuracy of floating types. Consider e.g. x0 += f(n) where x0 and f are of some floating type. Sometimes x0 is much larger...
3
by: Mark L Pappin | last post by:
<puts on Compiler Vendor hat> I've recently discovered that our compilers don't make any attempt to handle floating point overflow in add/subtract/ multiply/divide, with the result that...
6
by: aegis | last post by:
how can I take the fractional part of a floating point value such that I can store that value into an integer type? float foo = 6.180; now take the fractional part such that you can store 180...
13
by: tings | last post by:
An article states: "In floating point maths, where if you divide by a sufficiently large number sufficiently often, you will always be able to reach a value too small to distinguish from zero,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.