473,725 Members | 2,251 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

float? double?

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

Aug 18 '06 #1
60 7208
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Erick-wrote:
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??
That's a fairly open-ended question.

However, considering that C floatingpoint math is conducted in
double-precision (with conversions to and from single precision only to
store or retrieve values from variables), and that (for certain types
of function) single-precision floatingpoint values ("float") are
promoted to double-precision values ("double") automagically, I'd have
to say that the only benefit float has over double is that float may
take less storage space than double.

For my money, unless space is an issue, it's probably better to stick
with double as the default floatingpoint format for your code.

Just my 2cents worth
- --
Lew Pitcher
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (MingW32) - WinPT 0.11.12

iD8DBQFE5hHGagV FX4UWr64RAmY2AK CBaT3TGdQdV9z8B HRXDcEgbNw5dACc CN8A
Ljp+/QJFHbOg99yloCUp 00c=
=lP4v
-----END PGP SIGNATURE-----

Aug 18 '06 #2
In article <11************ *********@m79g2 000cwm.googlegr oups.com>,
Erick-<er*********@ya hoo.eswrote:
>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??

Which is best, a pickup truck, or a half-tonne truck?
float and double are defined in terms of minimum precision allowed
for each. On any given platform, it is not required that there is
*any* difference between the two: if the float data type meets the
minimum requirements that C imposes on the double data type,
then the two could be exactly the same.

Traditionally, float was faster than double but offered less
precision. double never offers -less- precision than float, but
these days, it is not uncommon to find computers on which double
is as fast (or faster than) float. Also, float never occupies -more-
permanent storage than does double, and sometimes memory size
is the biggest factor (but these days you usually just go out and
buy more memory if you need it.)

There are also still computers which do not implement either float or
double in hardware, so from a speed perspective, sometimes both
are significantly worse than using integer arithmetic instead. But
there are also numerous computers these days on which integer arithmetic
is slower than double -- computers being sold into markets where
(say) 95% of the operations requiring speed are likely to be
floating point operations, so the development resources are spent
primarily on accelarating floating point. Thus, from a speed
perspective, you cannot trust that floating point with be either
slower or faster than integer arithmetic. But there -are- times
when using integer arithmetic can be absolutely crucial for
preserving required accuracy.

All of which is to say, "it depends" ;-)
Size, speed, precision: for any given task, any of them might be
the key factor. Speed is particularily variable: a CPU upgrade
without changing anything else might completely alter the speed factors.
--
If you lie to the compiler, it will get its revenge. -- Henry Spencer
Aug 18 '06 #3
In article <11************ **********@m79g 2000cwm.googleg roups.com>,
Lew Pitcher <lp******@sympa tico.cawrote:
>However, considering that C floatingpoint math is conducted in
double-precision (with conversions to and from single precision only to
store or retrieve values from variables),
That's incorrect, at least for C89 (I haven't checked C99.)

The "usual arithmetic conversions" choses long double if necessary,
then double if necessary, then:

Otherwise, if either operand has type float, the other operand
is converted to float.

The descriptions of operations such as binary + refer to the
usual arithmetic conversions, but definitely do NOT say that
float is promoted to double for the purposes of the calculation.
--
If you lie to the compiler, it will get its revenge. -- Henry Spencer
Aug 18 '06 #4
Erick-wrote:
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??
float would be used when better performance is required (faster, or less
storage used). double, on most current platforms, gives more range and
precision, and generally requires less care about numerical issues.
Aug 18 '06 #5
Tim Prince wrote:
Erick-wrote:
>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??
float would be used when better performance is required (faster, or less
storage used).
Be certain that this is true for your platform before making such a
decision. It's entirely possible that your FPU datapath is more
efficient with doubles than with a shorter format. It's also possible
that float and double are the same datatype.

I would suggest the possibility that for a given application, increased
precision or range could conceivably be a liability. And in projects
I've worked on, I've seen routines that were dependent on assumptions
about the internal representation of the data (bad, not my fault, not in
my power to fix).

Anyway, check before assuming that float is more efficient than double.
It's entirely possible that the assumptions are backwards from the
results.
double, on most current platforms, gives more range and
precision, and generally requires less care about numerical issues.
I don't know about "most current platforms" because every time I make an
assumption about that, I'm reminded that what I think is the predominate
platform may be far from correct... I would seriously consider checking
into whether the current ARM chips have double fpu registers, and how
the compilers for that platform define float and double...

Now granted, in my tiny corner of the world, floats are 4 bytes and
doubles are 8 and long doubles 12.
Shoot me if I ever write anything that depends on that. (Wait, no, I
*have to* do that still, don't shoot.)
Aug 18 '06 #6
Erick-wrote:
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
double means DOUBLE PRECISION. Note the word precision here.

When you want more precision, use double precision, and if that
doesn't cut it use long double.

Precision means the number of significant digits you get
for the calculations. float gives you 6 digits precision,
(assuming IEEE 754) double gives you 15, and long double
more than that, using Intel/Amd implementation gives you 18.

If precision is not important (you are only interested
in a rough approximation) float is great.

Aug 18 '06 #7
jacob navia wrote:
double means DOUBLE PRECISION. Note the word precision here.

When you want more precision, use double precision, and if that
doesn't cut it use long double.

Precision means the number of significant digits you get
for the calculations. float gives you 6 digits precision,
(assuming IEEE 754) double gives you 15, and long double
more than that, using Intel/Amd implementation gives you 18.

I'm not up to date on the ISO specs, but I don't remember any
requirements like the ones you mention; only that long doubles be at
least as long as doubles, which themselves must be at least as long as
floats, and that there is a minimum range of -10^37 through 10^37.

It may be reasonable to assume IEEE754 on some (very common) platforms,
but is it strictly compliant to do so?
Aug 18 '06 #8
jmcgill wrote:
jacob navia wrote:
>double means DOUBLE PRECISION. Note the word precision here.

When you want more precision, use double precision, and if that
doesn't cut it use long double.

Precision means the number of significant digits you get
for the calculations. float gives you 6 digits precision,
(assuming IEEE 754) double gives you 15, and long double
more than that, using Intel/Amd implementation gives you 18.

I'm not up to date on the ISO specs, but I don't remember any
requirements like the ones you mention; only that long doubles be at
least as long as doubles, which themselves must be at least as long as
floats, and that there is a minimum range of -10^37 through 10^37.

It may be reasonable to assume IEEE754 on some (very common) platforms,
but is it strictly compliant to do so?
Do not confuse C99 standard (ISO) and IEEE 754 (floating point)
Aug 18 '06 #9
jacob navia wrote:
Do not confuse C99 standard (ISO) and IEEE 754 (floating point)
I wouldn't, or at least, I would be very explicit about my intentions if
doing so. But I've seen comments on this thread today that kind of
scare me. (By "kind of", I mean, I don't really give a damn, since none
of the posters work for me or are my students ;-)
Aug 18 '06 #10

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

Similar topics

8
9570
by: Jonathan Fielder | last post by:
Hi, I have a 32 bit integer value and I wish to find the single precision floating point value that is closest to but less than or equal to the integer. I also have a similar case where I need to find the single precision floating point value that is closest to but greater than or equal to the integer. I believe that if I simply cast to a float, it may be assigned the next higher or lower representable value, depending on...
9
2406
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; if(x == y) { printf("x can be represented precisely by a float\n"); }
13
3338
by: maadhuu | last post by:
hello , i would like to know as to why double is more efficient than float . thanking you, ranjan.
6
7272
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 methods deal with doubles and I'm having problems with casting them back to floats. For example: (double)0.1f = 0.10000000149011612 I don't need the accuracy of doubles, but the speed of floats would be benificial, but it seems I'm either...
5
2331
by: Kubik | last post by:
Hi! Let's see, we got: float var=4.6f; //as we know 414/4.6 shoud be equal to 90 but Math.Ceiling(414/var) gives us 91 but (414/var).ToString() prints '90'.
6
7609
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards, Karthi
116
35929
by: Dilip | last post by:
Recently in our code, I ran into a situation where were stuffing a float inside a double. The precision was extended automatically because of that. To make a long story short, this caused problems elsewhere in another part of the system where that figure was used for some calculation and some eventual truncation led to the system going haywire. So my question is, given this code: int main() { float f = 59.89F;
13
6185
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 ------------------ b += (float)(mode *val); On 32 bit(intel , vs 2003, C++), some watch variables are
3
10665
by: Arnie | last post by:
Folks, We ran into a pretty significant performance penalty when casting floats. We've identified a code workaround that we wanted to pass along but also was wondering if others had experience with this and if there is a better solution. -jeff
0
8889
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9401
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9257
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9179
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9116
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6011
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2637
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.