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

Does "float" always occupy 32 bits

As far as I know floating point variables, that are declared as float
follow IEEE format representation (which is 32-bit in size). But
chapter1-page no 9 of the book "The C programming language" states that
"THE RANGE OF BOTH int AND float DEPENDS ON THE MACHINE YOU ARE
USING".
Does this mean "float" variables have different sizes on different
machines?

Sep 4 '06 #1
16 11163
chandanlinster wrote:
As far as I know floating point variables, that are declared as float
follow IEEE format representation (which is 32-bit in size). But
chapter1-page no 9 of the book "The C programming language" states that
"THE RANGE OF BOTH int AND float DEPENDS ON THE MACHINE YOU ARE
USING".
Does this mean "float" variables have different sizes on different
machines?
Yes.

Although the IEEE single-precision type is the most common implementation of
float, this is not required. The standard only requires a minimum range and
minimum precision. Implementations can optionally signal that they implement
IEC 60559, a revision of IEEE 754.

In practice, most programs do not depend on the particular size of a float;
the ones that assume are usually those who also assume an int is 32 bits, so
floats and ints can be stored interchangeably. Needless to say, these are
not portable assumptions, and rarely appropriate, let alone necessary.

S.
Sep 4 '06 #2
"chandanlinster" <ch************@gmail.comwrote in message
news:11**********************@e3g2000cwe.googlegro ups.com...
As far as I know floating point variables, that are declared as float
follow IEEE format representation (which is 32-bit in size).
This is not guaranteed.
But
chapter1-page no 9 of the book "The C programming language" states that
"THE RANGE OF BOTH int AND float DEPENDS ON THE MACHINE YOU ARE
USING".
Does this mean "float" variables have different sizes on different
machines?
Yes.

Philip

Sep 4 '06 #3
Hi Guys.

I have a AMD-64 turion running debian Linux with gcc & here is what I
got..

char:1
short:2
long:8
float:4
double:8
long double:16

Here are my compiler options..

Using built-in specs.
Target: x86_64-linux-gnu
Configured with: ../src/configure -v
--enable-languages=c,c++,java,fortran,objc,obj-c++,ada,treelang
--prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib
--without-included-gettext --enable-threads=posix --enable-nls
--program-suffix=-4.1 --enable-__cxa_atexit --enable-clocale=gnu
--enable-libstdcxx-debug --enable-java-awt=gtk --enable-gtk-cairo
--with-java-home=/usr/lib/jvm/java-1.4.2-gcj-4.1-1.4.2.0/jre
--enable-mpfr --enable-checking=release x86_64-linux-gnu
Thread model: posix
gcc version 4.1.2 20060613 (prerelease) (Debian 4.1.1-5)

Cheers
/R

Philip Potter wrote:
"chandanlinster" <ch************@gmail.comwrote in message
news:11**********************@e3g2000cwe.googlegro ups.com...
As far as I know floating point variables, that are declared as float
follow IEEE format representation (which is 32-bit in size).

This is not guaranteed.
But
chapter1-page no 9 of the book "The C programming language" states that
"THE RANGE OF BOTH int AND float DEPENDS ON THE MACHINE YOU ARE
USING".
Does this mean "float" variables have different sizes on different
machines?

Yes.

Philip
Sep 4 '06 #4
Thanks everybody for providing the solutions.

Sep 4 '06 #5
"chandanlinster" <ch************@gmail.comwrites:
As far as I know floating point variables, that are declared as float
follow IEEE format representation (which is 32-bit in size). But
chapter1-page no 9 of the book "The C programming language" states that
"THE RANGE OF BOTH int AND float DEPENDS ON THE MACHINE YOU ARE
USING".
Does this mean "float" variables have different sizes on different
machines?
Potentially, yes.

I've never heard of a C implementation where float is not 32 bits
(i.e., where sizeof(float) * CHAR_BIT != 32), but such implementations
could easily exist.

In any case, there's no real reason or need to assume that float is 32
bits. The compiler knows how big a float is so you don't need to
worry about it.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Sep 4 '06 #6
Keith Thompson <ks***@mib.orgwrites:
"chandanlinster" <ch************@gmail.comwrites:
>As far as I know floating point variables, that are declared as float
follow IEEE format representation (which is 32-bit in size). But
chapter1-page no 9 of the book "The C programming language" states that
"THE RANGE OF BOTH int AND float DEPENDS ON THE MACHINE YOU ARE
USING".
Does this mean "float" variables have different sizes on different
machines?

Potentially, yes.

I've never heard of a C implementation where float is not 32 bits
(i.e., where sizeof(float) * CHAR_BIT != 32), but such implementations
could easily exist.
I don't think that float could be much smaller than 32 bits. By
my calculations, a floating point number represented in the
format that the Standard expects would need approximately 28 bits
to have the required range and precision.
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Sep 4 '06 #7
Ben Pfaff <bl*@cs.stanford.eduwrites:
Keith Thompson <ks***@mib.orgwrites:
>"chandanlinster" <ch************@gmail.comwrites:
>>As far as I know floating point variables, that are declared as float
follow IEEE format representation (which is 32-bit in size). But
chapter1-page no 9 of the book "The C programming language" states that
"THE RANGE OF BOTH int AND float DEPENDS ON THE MACHINE YOU ARE
USING".
Does this mean "float" variables have different sizes on different
machines?

Potentially, yes.

I've never heard of a C implementation where float is not 32 bits
(i.e., where sizeof(float) * CHAR_BIT != 32), but such implementations
could easily exist.

I don't think that float could be much smaller than 32 bits. By
my calculations, a floating point number represented in the
format that the Standard expects would need approximately 28 bits
to have the required range and precision.
Sure, but float could easily be larger than 32 bits.

And, in fact, contrary to what I wrote above, I've worked on machines
where float is 64 bits (and double is 64 bits, and long double is 128
bits). These were Cray vector machines, where the native word size is
64 bits.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Sep 5 '06 #8
Keith Thompson <ks***@mib.orgwrites:
Ben Pfaff <bl*@cs.stanford.eduwrites:
>Keith Thompson <ks***@mib.orgwrites:
>>I've never heard of a C implementation where float is not 32 bits
(i.e., where sizeof(float) * CHAR_BIT != 32), but such implementations
could easily exist.

I don't think that float could be much smaller than 32 bits. By
my calculations, a floating point number represented in the
format that the Standard expects would need approximately 28 bits
to have the required range and precision.

Sure, but float could easily be larger than 32 bits.
Yes, I should have added that. I didn't mean to sound contradictory.
And, in fact, contrary to what I wrote above, I've worked on machines
where float is 64 bits (and double is 64 bits, and long double is 128
bits). These were Cray vector machines, where the native word size is
64 bits.
Right.
--
"I should killfile you where you stand, worthless human." --Kaz
Sep 5 '06 #9
In article <ln************@nuthaus.mib.orgKeith Thompson <ks***@mib.orgwrites:
"chandanlinster" <ch************@gmail.comwrites:
....
Does this mean "float" variables have different sizes on different
machines?

Potentially, yes.

I've never heard of a C implementation where float is not 32 bits
(i.e., where sizeof(float) * CHAR_BIT != 32), but such implementations
could easily exist.
I have used them. sizeof(float) == 8.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Sep 5 '06 #10
Keith Thompson wrote:
"chandanlinster" <ch************@gmail.comwrites:
>As far as I know floating point variables, that are declared as float
follow IEEE format representation (which is 32-bit in size). But
chapter1-page no 9 of the book "The C programming language" states that
"THE RANGE OF BOTH int AND float DEPENDS ON THE MACHINE YOU ARE
USING".
Does this mean "float" variables have different sizes on different
machines?

Potentially, yes.

I've never heard of a C implementation where float is not 32 bits
(i.e., where sizeof(float) * CHAR_BIT != 32), but such implementations
could easily exist.

In any case, there's no real reason or need to assume that float is 32
bits. The compiler knows how big a float is so you don't need to
worry about it.
The Hi-Tech compiler for the PIC gives you the option of 24 or 32 bit
floats. I am not sure what ANSI says about that.
Sep 6 '06 #11
Neil <Ne*******@worldnet.att.netwrites:
Keith Thompson wrote:
>"chandanlinster" <ch************@gmail.comwrites:
>>As far as I know floating point variables, that are declared as float
follow IEEE format representation (which is 32-bit in size). But
chapter1-page no 9 of the book "The C programming language" states that
"THE RANGE OF BOTH int AND float DEPENDS ON THE MACHINE YOU ARE
USING".
Does this mean "float" variables have different sizes on different
machines?
Potentially, yes.
I've never heard of a C implementation where float is not 32 bits
(i.e., where sizeof(float) * CHAR_BIT != 32), but such implementations
could easily exist.
In any case, there's no real reason or need to assume that float is
32
bits. The compiler knows how big a float is so you don't need to
worry about it.

The Hi-Tech compiler for the PIC gives you the option of 24 or 32 bit
floats. I am not sure what ANSI says about that.
It doesn't directly say anything, but I don't believe it's possible to
meet the standard's requirements for type float in 24 bits. By my
calculations you need at least 27 bits (1 sign bit, 20 mantissa bits,
6 exponent bits).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Sep 6 '06 #12

"Keith Thompson" <ks***@mib.orgwrote in message
news:ln************@nuthaus.mib.org...
Neil <Ne*******@worldnet.att.netwrites:
>Keith Thompson wrote:
>>"chandanlinster" <ch************@gmail.comwrites:
As far as I know floating point variables, that are declared as float
follow IEEE format representation (which is 32-bit in size). But
chapter1-page no 9 of the book "The C programming language" states that
"THE RANGE OF BOTH int AND float DEPENDS ON THE MACHINE YOU ARE
USING".
Does this mean "float" variables have different sizes on different
machines?
Potentially, yes.
I've never heard of a C implementation where float is not 32 bits
(i.e., where sizeof(float) * CHAR_BIT != 32), but such implementations
could easily exist.
In any case, there's no real reason or need to assume that float is
32
bits. The compiler knows how big a float is so you don't need to
worry about it.

The Hi-Tech compiler for the PIC gives you the option of 24 or 32 bit
floats. I am not sure what ANSI says about that.

It doesn't directly say anything, but I don't believe it's possible to
meet the standard's requirements for type float in 24 bits. By my
calculations you need at least 27 bits (1 sign bit, 20 mantissa bits,
6 exponent bits).
Easy.Your 24 bits index into an array of double-precision 64 bit values.
Since the user isn't guaranteed more than 64 K of memory, you've done it.
--
www.personal.leeds.ac.uk/~bgy1mm
freeware games to download.

Sep 6 '06 #13
Keith Thompson wrote:
Neil <Ne*******@worldnet.att.netwrites:
[ snip ]
>The Hi-Tech compiler for the PIC gives you the option of 24 or 32 bit
floats. I am not sure what ANSI says about that.

It doesn't directly say anything, but I don't believe it's possible to
meet the standard's requirements for type float in 24 bits. By my
calculations you need at least 27 bits (1 sign bit, 20 mantissa bits,
6 exponent bits).
Without regard to the Standard's requirements..

IEEE 754 has an extra bit. Float on my Sun and x86 iron is 32 bits and
includes a sign, 8 exponent bits and a 24-bit mantissa. They squeeze 33
bits into 32 by assuming the high order bit of a normalized mantissa is
always 1. This means we can use the bit position to hold the low order
bit of the exponent.

Same system for double, of course. Sign + 11 exponent bits + 53 mantissa
bits == 65. It fits, of course, in 64 bits.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Sep 6 '06 #14
Malcolm wrote:
"Keith Thompson" <ks***@mib.orgwrote in message
.... snip ...
>>
It doesn't directly say anything, but I don't believe it's
possible to meet the standard's requirements for type float in
24 bits. By my calculations you need at least 27 bits (1 sign
bit, 20 mantissa bits, 6 exponent bits).

Easy.Your 24 bits index into an array of double-precision 64 bit
values. Since the user isn't guaranteed more than 64 K of memory,
you've done it.
Harumph. C passes by value. How do you pass those beasts by value?

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Sep 7 '06 #15
CBFalconer <cb********@yahoo.comwrites:
Malcolm wrote:
>"Keith Thompson" <ks***@mib.orgwrote in message
... snip ...
>>>
It doesn't directly say anything, but I don't believe it's
possible to meet the standard's requirements for type float in
24 bits. By my calculations you need at least 27 bits (1 sign
bit, 20 mantissa bits, 6 exponent bits).

Easy.Your 24 bits index into an array of double-precision 64 bit
values. Since the user isn't guaranteed more than 64 K of memory,
you've done it.

Harumph. C passes by value. How do you pass those beasts by value?
They're only 24 bits; why would there be any problem passing them by
value?

So the idea is that a value of type "float" is a 24-bit index into a
table of, say, 32-bit floating-point objects. There are up to 2**32
possible values, but only 2**24 of them can exist within a single
execution of a program. Any operation that produces a new (32-bit)
value creates a new entry in the table and a new 24-bit index value.

This is a very silly idea, but it could satisfy *most* of the required
semantics of type float.

One thing that you couldn't do with this is write a value of type
"float" to a binary file, read it from another instance of the same
program, and get the same value. Maybe a conforming freestanding
implementation could get away this this. (Is there an embedded
version of the DS9K?)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Sep 7 '06 #16


"Joe Wright" <jo********@comcast.netwrote in message
news:3v******************************@comcast.com. ..
Keith Thompson wrote:
Neil <Ne*******@worldnet.att.netwrites:
[ snip ]
The Hi-Tech compiler for the PIC gives you the option of 24 or 32 bit
floats. I am not sure what ANSI says about that.
It doesn't directly say anything, but I don't believe it's possible to
meet the standard's requirements for type float in 24 bits. By my
calculations you need at least 27 bits (1 sign bit, 20 mantissa bits,
6 exponent bits).

Without regard to the Standard's requirements..

IEEE 754 has an extra bit. Float on my Sun and x86 iron is 32 bits and
includes a sign, 8 exponent bits and a 24-bit mantissa. They squeeze 33
bits into 32 by assuming the high order bit of a normalized mantissa is
always 1. This means we can use the bit position to hold the low order
bit of the exponent.

Same system for double, of course. Sign + 11 exponent bits + 53 mantissa
bits == 65. It fits, of course, in 64 bits.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Honeywell L66 has 36 bit floats.....

Sep 7 '06 #17

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

Similar topics

6
by: asmirnov1234567890 | last post by:
Hi my python 2.3.4 for windows refuse to execute line float("NaN"). It says: >>> float("NaN") Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: invalid literal for...
5
by: geskerrett | last post by:
We are working on a project to decipher a record structure of an old accounting system that originates from the late80's mid-90's. We have come across a number format that appears to be a "float"...
8
by: Manuel | last post by:
Anyone know why for openGL applications must be used GLfloat (and GLint, etc...) instead float, int, etc..? thx, Manuel
5
by: Peter Hansen | last post by:
I'm investigating a puzzling problem involving an attempt to generate a constant containing an (IEEE 754) "infinity" value. (I understand that special float values are a "platform-dependent...
29
by: candy_init | last post by:
Hi all, I just came across the following program: #include <stdio.h> int main() { float a = 12.5; printf("%d\n", a); printf("%d\n", *(int *)&a); return 0;
5
by: maya | last post by:
hi, here, http://www.mayacove.com/design/sd/test2.html <div class="entry"has a bottom-margin of 20px.. but it's ignored if divs inside it have "float" property.. (put borders around divs for...
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: 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: 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...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.