473,388 Members | 1,376 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,388 software developers and data experts.

Number of bits in C99 float types?

Has anyone got a mechanism for finding the number of bits in floats,
doubles, and long doubles? I need this to communicate with some
hardware.

I guess I could try to deduce this from float.h, but that looks
difficult.

I don't mind something which is system-specific; the older gccs had
various defines in cpu_limits.h and related files, but I can't find
anything in later gccs.

Thanks -

Dom
Mar 17 '08 #1
16 2866
On Mon, 17 Mar 2008 20:13:21 +0000, Dom Fulton wrote:
Has anyone got a mechanism for finding the number of bits in floats,
doubles, and long doubles? I need this to communicate with some
hardware.

I guess I could try to deduce this from float.h, but that looks
difficult.

I don't mind something which is system-specific; the older gccs had
various defines in cpu_limits.h and related files, but I can't find
anything in later gccs.
If you're already looking for something system-specific, why can't you
simply put the number of bits as a constant in your code? What do you
want to do with the number of bits, that it's useful at all to be able to
deal with different numbers?

That said, every object consists of sizeof(object) * CHAR_BIT bits. Some
of those bits may not be useful in determining the value, depending on
the system and compiler. Most likely, on your system, this will work just
fine for floating point types.
Mar 17 '08 #2
I've got to support several different systems - Windows, SunOS, and
various Linuxes. I guess I could just find out what the answer is for
the various platforms, and hard-code it in, but that gives me a bad
feeling...

sizeof doesn't work; it's rounded up for alignment reasons. On most
systems, float/double/long double will be 32/64/80 bits, but sizeof
generally returns 96 or 128 for 80-bit floats.

Thanks -

Dom

Mar 17 '08 #3
Dom Fulton wrote:
I've got to support several different systems - Windows, SunOS, and
various Linuxes. I guess I could just find out what the answer is for
the various platforms, and hard-code it in, but that gives me a bad
feeling...

sizeof doesn't work; it's rounded up for alignment reasons. On most
systems, float/double/long double will be 32/64/80 bits, but sizeof
generally returns 96 or 128 for 80-bit floats.

Thanks -

Dom
You will find all you need here.
http://docs.sun.com/app/docs/doc/801...uc?l=ru&a=view
The processor is important, not the OS/ There you will find all docs
for x86 and SPARC. The power pc is IEEE too, without any support for
long double.
--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
Mar 17 '08 #4
On Mon, 17 Mar 2008 21:56:45 +0100, jacob navia wrote:
Dom Fulton wrote:
>I've got to support several different systems - Windows, SunOS, and
various Linuxes. I guess I could just find out what the answer is for
the various platforms, and hard-code it in, but that gives me a bad
feeling...

sizeof doesn't work; it's rounded up for alignment reasons. On most
systems, float/double/long double will be 32/64/80 bits, but sizeof
generally returns 96 or 128 for 80-bit floats.
[...]
The
processor is important, not the OS/
The processor and the OS are both important. The processor determines
which floating point representations are available, and the OS (along
with the compiler, I suppose) decides how those map them to C's types.
Mar 17 '08 #5
Dom Fulton wrote:
I've got to support several different systems - Windows, SunOS, and
various Linuxes. I guess I could just find out what the answer is for
the various platforms, and hard-code it in, but that gives me a bad
feeling...

sizeof doesn't work; it's rounded up for alignment reasons. On most
systems, float/double/long double will be 32/64/80 bits, but sizeof
generally returns 96 or 128 for 80-bit floats.
If sizeof(long double) * CHAR_BIT == 96 on some system,
then 96 *is* the number of bits in a long double, not some
kind of "rounded up" result. If you are attaching some other
meaning to "number of bits," you need to explain what you mean.
It might be

- The number of significand bits (variation: count or
omit a "hidden" bit),

- The number of bits that participate in the value
(sign, exponent, and significand),

- Something else altogether.

It might also help if you described what you intend to
do with the answer once you have it. So far, you've said
only that you need to "communicate with some hardware," a
description that's pretty low on the specificity scale.

--
Er*********@sun.com
Mar 17 '08 #6
Eric Sosman wrote:
Dom Fulton wrote:
>I've got to support several different systems - Windows, SunOS, and
various Linuxes. I guess I could just find out what the answer is for
the various platforms, and hard-code it in, but that gives me a bad
feeling...

sizeof doesn't work; it's rounded up for alignment reasons. On most
systems, float/double/long double will be 32/64/80 bits, but sizeof
generally returns 96 or 128 for 80-bit floats.

If sizeof(long double) * CHAR_BIT == 96 on some system,
then 96 *is* the number of bits in a long double, not some
kind of "rounded up" result.
No. In the x86 the long double is 10 bytes, but it
is rounded up into 12 (lcc-win) or even 16, just for
alignment reasons.
--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
Mar 17 '08 #7
jacob navia wrote:
Eric Sosman wrote:
>Dom Fulton wrote:
>>I've got to support several different systems - Windows, SunOS, and
various Linuxes. I guess I could just find out what the answer is
for the various platforms, and hard-code it in, but that gives me a
bad feeling...

sizeof doesn't work; it's rounded up for alignment reasons. On most
systems, float/double/long double will be 32/64/80 bits, but sizeof
generally returns 96 or 128 for 80-bit floats.

If sizeof(long double) * CHAR_BIT == 96 on some system,
then 96 *is* the number of bits in a long double, not some
kind of "rounded up" result.

No. In the x86 the long double is 10 bytes,
ITYM the FPU registers.
but it is rounded up into 12 (lcc-win) or even 16, just for
alignment reasons.
Mar 17 '08 #8
On Mar 17, 4:29 pm, jacob navia <ja...@nospam.comwrote:
Eric Sosman wrote:
Dom Fulton wrote:
I've got to support several different systems - Windows, SunOS, and
various Linuxes. I guess I could just find out what the answer is for
the various platforms, and hard-code it in, but that gives me a bad
feeling...
sizeof doesn't work; it's rounded up for alignment reasons. On most
systems, float/double/long double will be 32/64/80 bits, but sizeof
generally returns 96 or 128 for 80-bit floats.
If sizeof(long double) * CHAR_BIT == 96 on some system,
then 96 *is* the number of bits in a long double, not some
kind of "rounded up" result.

No. In the x86 the long double is 10 bytes, but it
is rounded up into 12 (lcc-win) or even 16, just for
alignment reasons.
Except when you use an MS compiler on windows.

Yevgen
Mar 18 '08 #9
On Mar 17, 6:16*pm, ymunt...@gmail.com wrote:
On Mar 17, 4:29 pm, jacob navia <ja...@nospam.comwrote:


Eric Sosman wrote:
Dom Fulton wrote:
>I've got to support several different systems - Windows, SunOS, and
>various Linuxes. I guess I could just find out what the answer is for
>the various platforms, and hard-code it in, but that gives me a bad
>feeling...
>sizeof doesn't work; it's rounded up for alignment reasons. On most
>systems, float/double/long double will be 32/64/80 bits, but sizeof
>generally returns 96 or 128 for 80-bit floats.
* * If sizeof(long double) * CHAR_BIT == 96 on some system,
then 96 *is* the number of bits in a long double, not some
kind of "rounded up" result.
No. In the x86 the long double is 10 bytes, but it
is rounded up into 12 (lcc-win) or even 16, just for
alignment reasons.

Except when you use an MS compiler on windows.
The compiler in question does not change the size of the hardware long
double (but one could effectively argue that the C compiler
implementation formally defines what the size of a type for the
compiler is).

With the MS compilers, you can use 80 bits internally:
http://msdn2.microsoft.com/en-us/lib...24(VS.60).aspx says:
"The Microsoft run-time library sets the default internal precision of
the math coprocessor (or emulator) to 64 bits. This default applies
only to the internal precision at which all intermediate calculations
are performed; it does not apply to the size of arguments, return
values, or variables. You can override this default and set the chip
(or emulator) back to 80-bit precision by linking your program with
LIB/FP10.OBJ. On the linker command line, FP10.OBJ must appear before
LIBC.LIB, LIBCMT.LIB, or MSVCRT.LIB."

However, you do not get the full width when printing out the answers,
since the values get truncated to 8 byte doubles.

At any rate, giving me only 64 bits of my 80 bit floating point
registers stinks.
Mar 18 '08 #10
On Mar 17, 8:41 pm, user923005 <dcor...@connx.comwrote:
On Mar 17, 6:16 pm, ymunt...@gmail.com wrote:
On Mar 17, 4:29 pm, jacob navia <ja...@nospam.comwrote:
Eric Sosman wrote:
Dom Fulton wrote:
I've got to support several different systems - Windows, SunOS, and
various Linuxes. I guess I could just find out what the answer is for
the various platforms, and hard-code it in, but that gives me a bad
feeling...
sizeof doesn't work; it's rounded up for alignment reasons. On most
systems, float/double/long double will be 32/64/80 bits, but sizeof
generally returns 96 or 128 for 80-bit floats.
If sizeof(long double) * CHAR_BIT == 96 on some system,
then 96 *is* the number of bits in a long double, not some
kind of "rounded up" result.
No. In the x86 the long double is 10 bytes, but it
is rounded up into 12 (lcc-win) or even 16, just for
alignment reasons.
Except when you use an MS compiler on windows.

The compiler in question does not change the size of the hardware long
double (but one could effectively argue that the C compiler
implementation formally defines what the size of a type for the
compiler is).
I didn't say anything about hardware, I referred to

"If sizeof(long double) * CHAR_BIT == 96 on some system"
....
"No. In the x86 the long double is 10 bytes, but it
is rounded up into 12 (lcc-win) or even 16,"

I take that meant that on x86 sizeof(long double) will
always be at least 10 (or even 12?), and usually 12 or
even 16. Not true if you are using an MS compiler, that's it.
With the MS compilers, you can use 80 bits internally:
http://msdn2.microsoft.com/en-us/lib...24(VS.60).aspx says:
[snip]
However, you do not get the full width when printing out the answers,
since the values get truncated to 8 byte doubles.
All values, so it's not just printing answers, it's
the answers themselves in the first place. You don't
care much if there were some bits if you can't touch
those bits :)

Yevgen
Mar 18 '08 #11
Thanks for the input, everyone. It's not quite what I was looking for,
so perhaps I can restate the problem.

I need to export floating-point data from system A to system B. To do
this, I need to know the characteristics of the data format on system
A; in particular, I need to know if it conforms to IEC60559, and which
IEC60559 type it is. I would have exactly the same problem if I needed
to do bit manipulation of the FP data.

The problem is that C (or, to be more precise, the compiler in
combination with libc) exposes 3 FP types, which may be a subset, or a
superset, or even unrelated to the FP support from the underlying
hardware. 'sizeof' gives me no useful information about the 3 exposed
types.

My best guess so far is:

1) if __STDC_IEC_559__ is not defined, then I can find out nothing
useful about the types;

2) if __STC_IEC_559__ is defined then (from pp442/445 of the C99
spec):

3) 'float' must be a 32-bit IEC60559 single

4) 'double' must be a 64-bit IEC60559 double

5) 'long double' may be anything. It can be a non-IEC extended format,
an IEC double, an IEC 80-bit, or an IEC 128-bit. C appears to have no
way to tell me what this type is. I can deduce its behaviour from the
constants in float.h, but I have no guarantee that I can correctly
determine the actual long double size from these constants.

Any ideas? The gcc system includes did historically have a define
which gave the size of a long double, but this seems to be gone now.

Thanks

-Dom
Mar 18 '08 #12
Dom Fulton <we****@yahoo.comwrites:
Thanks for the input, everyone. It's not quite what I was looking for,
so perhaps I can restate the problem.

I need to export floating-point data from system A to system B. To do
this, I need to know the characteristics of the data format on system
A;
You are still a bit too close to your problem to explain all the
corners of it. There is no obvious need to know this details you ask
for to export data -- it is done all time simply by converting to
decimal and back.

Your problem may require either more speed or more accuracy (or both)
than this method is capable of. If accuracy is the only problem
(i.e. you want to loose no more bits than you have to due any
difference in formats been A and B) then I think you can find a way
using frexp and scalbn (repeatedly) to construct a representation that
is portable but loses no information.

If speed is the primary problem I suspect you will probably need
another route -- and it won't be portable!.

--
Ben.
Mar 18 '08 #13
Dom Fulton wrote:
Thanks for the input, everyone. It's not quite what I was looking for,
so perhaps I can restate the problem.

I need to export floating-point data from system A to system B. To do
this, I need to know the characteristics of the data format on system
A; in particular, I need to know if it conforms to IEC60559, and which
IEC60559 type it is. I would have exactly the same problem if I needed
to do bit manipulation of the FP data.

The problem is that C (or, to be more precise, the compiler in
combination with libc) exposes 3 FP types, which may be a subset, or a
superset, or even unrelated to the FP support from the underlying
hardware. 'sizeof' gives me no useful information about the 3 exposed
types.
No, but float.h gives you compile time access to a lot of constants. You
can test these constants and appropriately declare typedefs for your
own floating types. All conforming implementations are guaranteed to
support the range 1e-37 -1e37 for float, double and long double.

I don't think your problem (moving floating values from one system to
another) requires you to know the number of bits in the floating point
types. I think conditional compiling with the values in float.h should
be enough to declare types appropriately to move the data, as long as
the destination system has at least one floating type that can hold the
largest value possible in the source system.

<snip>

Mar 18 '08 #14
On Tue, 18 Mar 2008 09:40:17 -0700, Keith Thompson wrote:
Harald van Dijk <tr*****@gmail.comwrites:
>On Mon, 17 Mar 2008 21:56:45 +0100, jacob navia wrote:
>>Dom Fulton wrote:
I've got to support several different systems - Windows, SunOS, and
various Linuxes. I guess I could just find out what the answer is for
the various platforms, and hard-code it in, but that gives me a bad
feeling...

sizeof doesn't work; it's rounded up for alignment reasons. On most
systems, float/double/long double will be 32/64/80 bits, but sizeof
generally returns 96 or 128 for 80-bit floats.
[...]
The
processor is important, not the OS/

The processor and the OS are both important. The processor determines
which floating point representations are available, and the OS (along
with the compiler, I suppose) decides how those map them to C's types.

No, floating point representations are determined by the C
implementation, particularly by the compiler. (Typically the CPU and/or
OS imposes requirements that the compiler needs to follow, or can
violate only with some ugly contortions.)
I do not think this contradicts what I posted. Yes, it's the
implementation that decides the floating point representations, but the
processor and OS are a significant part of the implementation.

What I meant was that the OS may already provide, for example, a strtod
function. Regardless of all else, if the implementation is going to use
that strtod function, it must make sure that the representation of double
matches what strtod returns.
Mar 18 '08 #15
Harald van D?k wrote:
On Tue, 18 Mar 2008 09:40:17 -0700, Keith Thompson wrote:
>Harald van D?k <tr*****@gmail.comwrites:
>>On Mon, 17 Mar 2008 21:56:45 +0100, jacob navia wrote:
Dom Fulton wrote:
I've got to support several different systems - Windows, SunOS,
and various Linuxes. I guess I could just find out what the answer
is for the various platforms, and hard-code it in, but that gives
me a bad feeling...
>
sizeof doesn't work; it's rounded up for alignment reasons. On
most systems, float/double/long double will be 32/64/80 bits, but
sizeof generally returns 96 or 128 for 80-bit floats.
[...]
The
processor is important, not the OS/

The processor and the OS are both important. The processor
determines which floating point representations are available, and
the OS (along with the compiler, I suppose) decides how those map
them to C's types.

No, floating point representations are determined by the C
implementation, particularly by the compiler. (Typically the CPU
and/or OS imposes requirements that the compiler needs to follow, or
can violate only with some ugly contortions.)

I do not think this contradicts what I posted. Yes, it's the
implementation that decides the floating point representations, but
the processor and OS are a significant part of the implementation.

What I meant was that the OS may already provide, for example, a
strtod function. Regardless of all else, if the implementation is
going to use that strtod function, it must make sure that the
representation of double matches what strtod returns.
I think the processor and the purpose of the language determine the
format of floating-point types much more than the OS. A compatibility
layer implemented by the compiler can always make sure that the OS is
given what it expects. Operating systems usually don't deal heavily
with floating-point types.

Mar 18 '08 #16
Harald van Dijk <tr*****@gmail.comwrites:
On Tue, 18 Mar 2008 09:40:17 -0700, Keith Thompson wrote:
>Harald van Dijk <tr*****@gmail.comwrites:
>>On Mon, 17 Mar 2008 21:56:45 +0100, jacob navia wrote:
Dom Fulton wrote:
I've got to support several different systems - Windows, SunOS, and
various Linuxes. I guess I could just find out what the answer is for
the various platforms, and hard-code it in, but that gives me a bad
feeling...
>
sizeof doesn't work; it's rounded up for alignment reasons. On most
systems, float/double/long double will be 32/64/80 bits, but sizeof
generally returns 96 or 128 for 80-bit floats.
[...]
The
processor is important, not the OS/

The processor and the OS are both important. The processor determines
which floating point representations are available, and the OS (along
with the compiler, I suppose) decides how those map them to C's types.

No, floating point representations are determined by the C
implementation, particularly by the compiler. (Typically the CPU and/or
OS imposes requirements that the compiler needs to follow, or can
violate only with some ugly contortions.)

I do not think this contradicts what I posted. Yes, it's the
implementation that decides the floating point representations, but the
processor and OS are a significant part of the implementation.

What I meant was that the OS may already provide, for example, a strtod
function. Regardless of all else, if the implementation is going to use
that strtod function, it must make sure that the representation of double
matches what strtod returns.
The strtod function is part of the C implementation, specifically of
the C runtime library. It may be provided by, or even be a part of,
the OS.

The C compiler and the C library must work together properly (for a
non-buggy implementation). If the library is provided by the OS, then
yes, a lot of the C compiler's decisions are mandated by what the OS
does.

In many cases, the CPU and the OS very strongly suggest a certain set
of floating-point types. The C implementation doesn't *have* to
follow that strong suggestion; it can do whatever it likes within the
requirements imposed by the standard. But very often there's no good
reason not to follow the OS's conventions (and a lot of extra work to
avoid them).

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 19 '08 #17

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

Similar topics

2
by: sarmin kho | last post by:
Hi Pythoners, When it is an integer number, what is the range of the integer number and long integer number?? do we have typecasting of 8bits or 16bits signed and unsigned number in python? ...
12
by: jose luis fernandez diaz | last post by:
Hi, My OS is: cronos:jdiaz:tmp>uname -a HP-UX cronos B.11.11 U 9000/800 820960681 unlimited-user license I compile in 64-bits mode the program below:
14
by: Ben | last post by:
Hi, I need to write some data types into an array of unsigned chars. These are basically "signals" within a message, so each signal will have a start bit and a length. The signals will also...
4
by: KC | last post by:
How can I represent a 22-digit integer in C? float is not large enough. Thanks!
1
by: Shreyas Kulkarni | last post by:
hi there, recently i have got a problem regarding calculation of sum of digits in a floating point or precision number. the weird behaviour of compiler/language is preventing me from calculating...
9
by: Alex Buell | last post by:
I just wrote the following and have a question: Why does my code gets it wrong with the class Simple? See last show_size<Simple> function call in main () as below: #include <iostream> #include...
14
by: evangeli | last post by:
Hello, How can I know the number of bits used for an int value? Is "sizeof(int) * CHAR_BIT" correct ? thanks
8
by: myfem.analyzer | last post by:
Hi, I saw a line of codes in the "Fast Inverse Square Root" like this: Float InvSqrt(float x) { ....... int i = *(int *) &x; //get bits for floating value ..... x = * (float *) &i; ...
14
by: mathieu | last post by:
hi there, I do not understand the syntax for ios_base::precision. Let say I have a floating point: const float f = 0.313244462; How do I write is to a stream ? const float f =...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.