473,385 Members | 1,813 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.

double vs float

In the post with heading "Learning C - Scanf or Getch, or Getchar not
working correctly after first loop" that appears in today's list in
comp.lang.c,

it has been mentioned in the answer to this post, that double should
be preferred to float when space is not a constraint.

Can someone explain why double should be preferred to float ?

Mar 4 '07 #1
7 14948
su**************@yahoo.com, India wrote:
In the post with heading "Learning C - Scanf or Getch, or Getchar not
working correctly after first loop" that appears in today's list in
comp.lang.c,

it has been mentioned in the answer to this post, that double should
be preferred to float when space is not a constraint.

Can someone explain why double should be preferred to float ?
Usually it's because double corresponds more closely with the system's
native floating point type than float. Also, it gives more range and
precision than float, which is an important point when using floating-
point types.

Mar 4 '07 #2
su**************@yahoo.com, India wrote:
Can someone explain why double should be preferred to float ?
IEEE floats have 24 bits of mantissa while IEEE double floats
have 51 bits of mantissa.

That means that calculations using doubles are less likely to
be affected by rounding and truncation problems than calculations
using floats.

Once very good read is "What Every Computer Scientist Should
Know About Floating-Point Arithmetic":

http://docs.sun.com/source/806-3568/ncg_goldberg.html

Erik
--
+-----------------------------------------------------------+
Erik de Castro Lopo
+-----------------------------------------------------------+
"The RIAA is obsessed to the point of comedy with the frustration
of having its rules broken, without considering whether such rules
might be standing in the way of increased revenues. Indeed,
Napster and Gnutella may turn out to be the two best music-marketing
gimmicks yet devised, if only the RIAA would take its head out of
its ass long enough to realise it."
-- Thomas C Greene on www.theregister.co.uk
Mar 4 '07 #3
On 3 Mar 2007 19:48:49 -0800, "santosh" <sa*********@gmail.comwrote
in comp.lang.c:
su**************@yahoo.com, India wrote:
In the post with heading "Learning C - Scanf or Getch, or Getchar not
working correctly after first loop" that appears in today's list in
comp.lang.c,

it has been mentioned in the answer to this post, that double should
be preferred to float when space is not a constraint.

Can someone explain why double should be preferred to float ?

Usually it's because double corresponds more closely with the system's
native floating point type than float. Also, it gives more range and
precision than float, which is an important point when using floating-
point types.
What system is it that has "a" native floating point type, only one
that is, that corresponds more closely to double than float?

Your answer makes no sense at all, for several reasons:

1. If a platform only has a single floating point type supported in
hardware, and it meets the requirements for C's double, there is
nothing at all preventing the C implementation from using it for float
as well.

2. I can't think of any hardware architecture off-hand that has
hardware floating point for something suitable for a double, such as
64 bits or more, that does not also have hardware support for a
narrower floating point type.

On the other hand, I know of quite a few platforms where the opposite
is true, including 32-bit controllers and DSPs, namely that they have
hardware 32-bit floating point, which meets the C requirements for
float, but not a wider hardware floating point type.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Mar 4 '07 #4
Jack Klein wrote:
On 3 Mar 2007 19:48:49 -0800, "santosh" <sa*********@gmail.comwrote
in comp.lang.c:
su**************@yahoo.com, India wrote:
In the post with heading "Learning C - Scanf or Getch, or Getchar not
working correctly after first loop" that appears in today's list in
comp.lang.c,
>
it has been mentioned in the answer to this post, that double should
be preferred to float when space is not a constraint.
>
Can someone explain why double should be preferred to float ?
Usually it's because double corresponds more closely with the system's
native floating point type than float. Also, it gives more range and
precision than float, which is an important point when using floating-
point types.
After reading your points I realise I may have been incorrect in my
assumptions.
What system is it that has "a" native floating point type, only one
that is, that corresponds more closely to double than float?
Well the IA32 architecture has an 80 bit hardware floating point type,
to which the C double would correspond more closely than a float.
Your answer makes no sense at all, for several reasons:

1. If a platform only has a single floating point type supported in
hardware, and it meets the requirements for C's double, there is
nothing at all preventing the C implementation from using it for float
as well.
Except for the fact that one of float's purpose is to reduce memory
requirements.
2. I can't think of any hardware architecture off-hand that has
hardware floating point for something suitable for a double, such as
64 bits or more, that does not also have hardware support for a
narrower floating point type.

On the other hand, I know of quite a few platforms where the opposite
is true, including 32-bit controllers and DSPs, namely that they have
hardware 32-bit floating point, which meets the C requirements for
float, but not a wider hardware floating point type.
I stand corrected.

Mar 4 '07 #5
<su**************@yahoo.comwrote in message
>
In the post with heading "Learning C - Scanf or Getch, or Getchar not
working correctly after first loop" that appears in today's list in
comp.lang.c,

it has been mentioned in the answer to this post, that double should
be preferred to float when space is not a constraint.

Can someone explain why double should be preferred to float ?
For the vast majority of calculations time and space to hold the variable
are non-issues. In real [pun] applications doubles virtually always have
sufficient precision, floats often need careful coding. For instance if you
are calculating the mean of a list of numbers it might well be important to
add the lowest first in single precision to reduce round-off errors, it is
most unlikely that this would be necessary in double precision.

Then the maths library functions use doubles.

The big exception for the programs I write is geometry routines. Here size
is an issue because we are often storing many millions of 3d points,
precision isn't very important - no one minds if a polygon is off by a pixel
as long as it joins to its neighbour without showing white - and speed is
often crucial. However it is real nuisance having two representations of
real numbers in the machine.

Mar 4 '07 #6
Malcolm McLean wrote:
<su**************@yahoo.comwrote
>Can someone explain why double should be preferred to float ?

[...]
The big exception for the programs I write is geometry routines. Here
size is an issue because we are often storing many millions of 3d
points, precision isn't very important - no one minds if a polygon is
off by a pixel as long as it joins to its neighbour without showing
white - and speed is often crucial.
All true, except for the "no one minds" part.

A great many 3D artists using commercial software will eventually
encounter the effects of limited precision. The most common effect is
a quantization of point positions that makes smooth polygonal surfaces
look like they're made of Lego bricks. Some highly optimized raytracers
are also very sensitive to roundoff error in the calculation of polygon
normals; a common symptom is cracks in raytraced shadows.

Most artists don't understand why this happens, or why the software
allows it to happen. I've been trying to explain it to them for a very
long time:

http://groups.google.com/group/comp....c1f01b8bf470b5

Float is still a good tradeoff in this case. An even better use of
float is in 2D image formats,

http://en.wikipedia.org/wiki/High_dynamic_range_imaging

where double would increase storage and transmission costs without any
visible benefit.

- Ernie http://home.comcast.net/~erniew
Mar 5 '07 #7
Jack Klein wrote:
>
On 3 Mar 2007 19:48:49 -0800, "santosh" <sa*********@gmail.comwrote
in comp.lang.c:
su**************@yahoo.com, India wrote:
In the post with heading "Learning C - Scanf or Getch, or Getchar not
working correctly after first loop" that appears in today's list in
comp.lang.c,
>
it has been mentioned in the answer to this post, that double should
be preferred to float when space is not a constraint.
>
Can someone explain why double should be preferred to float ?
Usually it's because double corresponds more closely with the system's
native floating point type than float. Also, it gives more range and
precision than float, which is an important point when using floating-
point types.

What system is it that has "a" native floating point type, only one
that is, that corresponds more closely to double than float?
Type float is subject to "the default argument promotions".
Type double, isn't.

--
pete
Mar 11 '07 #8

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

Similar topics

4
by: Michael Mair | last post by:
Hi there, actually, I have posted the same question in g.g.help. As there were no answers, I am still not sure whether this is a bug or only something open to the compiler that is seemingly...
22
by: bq | last post by:
Hello, Two questions related to floating point support: What C compilers for the wintel (MS Windows + x86) platform are C99 compliant as far as <math.h> and <tgmath.h> are concerned? What...
9
by: Sisyphus | last post by:
Hi, I have some software that does the following (in an attempt to determine whether the double x, can be represented just as accurately by a float): void test_it(double x) { float y = x;...
13
by: maadhuu | last post by:
hello , i would like to know as to why double is more efficient than float . thanking you, ranjan.
6
by: James Thurley | last post by:
According to the docs, floats are 32 bit and doubles are 64 bit. So using floats should be faster than using doubles on a 32 bit processor, and my tests confirm this. However, most of the Math...
44
by: Daniel | last post by:
I am grappling with the idea of double.Epsilon. I have written the following test: public void FuzzyDivisionTest() { double a = 0.33333d; double b = 1d / 3d; Assert.IsFalse(a == b,...
67
by: lcw1964 | last post by:
This may be in the category of bush-league rudimentary, but I am quite perplexed on this and diligent Googling has not provided me with a clear straight answer--perhaps I don't know how to ask the...
60
by: Erick-> | last post by:
hi all... I've readed some lines about the difference between float and double data types... but, in the real world, which is the best? when should we use float or double?? thanks Erick
2
by: tkirankumar | last post by:
Hi all, uname -a SunOS cbmrsd1a1 5.10 Generic_118833-17 sun4us sparc FJSV,GPUZC-M g++ -v Reading specs from /usr/local/lib/gcc-lib/sparc-sun-solaris2.10/3.3.2/specs Configured with:...
13
by: Shirsoft | last post by:
I have a 32 bit intel and 64 bit AMD machine. There is a rounding error in the 8th digit. Unfortunately because of the algorithm we use, the errors percolate into higher digits. C++ code is...
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
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...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.