Welcome to the most active Linux Forum on the web.
 Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org how big is a float in C?
 Programming This forum is for all programming questions. The question does not have to be directly related to Linux and any language is fair game.

Notices

 04-03-2005, 01:30 PM #1 SciYro Senior Member   Registered: Oct 2003 Location: hopefully not here Distribution: Gentoo Posts: 2,038 Rep: how big is a float in C? how big a number can a float hold in C?, sorry for the newb question, bit i searched everywhere, but all i get is C code, and no info on the limits of this type.
04-03-2005, 02:12 PM   #2
ruidh
LQ Newbie

Registered: Jan 2005
Posts: 24

Rep:
Re: how big is a float in C?

Quote:
 Originally posted by SciYro how big a number can a float hold in C?, sorry for the newb question, bit i searched everywhere, but all i get is C code, and no info on the limits of this type.
It's implementation defined. Just like int.

In gcc on most Linux kernels, a float is 32 bits and a double is 64 bits. A 4 byte float can store values between 1W37 to 1E-37 (and zero) with about 6 decimal digits of precision. An 8 byte double can go up to 1E-307 to 1E308 with about 15 decimal digits of precision. Remember that floats and doubles are not precise representations like ints.

 04-03-2005, 04:06 PM #3 SciYro Senior Member   Registered: Oct 2003 Location: hopefully not here Distribution: Gentoo Posts: 2,038 Original Poster Rep: thanks. there not precise?, i thought the point of them was to be precise.
 04-03-2005, 04:55 PM #4 puffinman Member   Registered: Jan 2005 Location: Atlanta, GA Distribution: Gentoo, Slackware Posts: 217 Rep: ints are exact representations of integers. floats and doubles are approximations of real numbers, which are often irrational and thus cannot be expressed with a finite number of decimal places. An int would be even less precise in describing an irrational number. For example, pi is an irrational number. The closest an int can get to pi is 3. A float would give you 3.141593. A double would give you 3.141592653589793. None of these representations are exact, but each one is more precise.
 04-03-2005, 05:30 PM #5 SciYro Senior Member   Registered: Oct 2003 Location: hopefully not here Distribution: Gentoo Posts: 2,038 Original Poster Rep: thanks
 04-03-2005, 06:09 PM #6 dave_starsky Member   Registered: Oct 2003 Location: UK, Manchester Distribution: Gentoo (2.6.10-r4) & Ubuntu Posts: 145 Rep: Also, some numbers which are nice and easy to write in decimal are not as a floating point number in binary, so you can lose some precision on numbers you wouldn't think.
 04-03-2005, 06:54 PM #7 SciYro Senior Member   Registered: Oct 2003 Location: hopefully not here Distribution: Gentoo Posts: 2,038 Original Poster Rep: could i lose precision on a finite number, no bigger then XXX,XXX,XXX.XXX, XXX ? tho, i think i only need to get to XXX,XXX.XXX,XXX (but can never be to careful)
 04-03-2005, 08:07 PM #8 Dave Kelly Member   Registered: Aug 2004 Location: Todd Mission Texas Distribution: Linspire Posts: 213 Rep: Have you read through the 'math.h' in /usr/include. Lots of good imformation there.
04-03-2005, 09:10 PM   #9
ruidh
LQ Newbie

Registered: Jan 2005
Posts: 24

Rep:
Quote:
 Originally posted by SciYro could i lose precision on a finite number, no bigger then XXX,XXX,XXX.XXX, XXX ? tho, i think i only need to get to XXX,XXX.XXX,XXX (but can never be to careful)
Is there some confusion over commas and decimal places there?

Anytime you have something to the right of the decimal place, you may lose precision. Floats are sometimes difficult to use because they don't work the way you might expect. Fractional powers of two and integral multiples of fractional powers of two are perfectly representable. But useful numbers like 0.01 and 0.001 do not have exact float or double representations. You may add up a bunch of terms which should nicely add to the integer 2 and find that your program thinks that the result is 1.99999999999999.

Floats work well when you have scientific problems where you are mostly multiplying and you know how to estimate the loss of precision with each operation. They don't work as well for problems using mainly addition unless you are prepared to accept loss of precision in the answer.

If you want to store currencies, you really want to be using integral types representing the number of hundredths or thousandths and fix up the numbers on multiplication, division and display. This is called a "fixed point" representation.

 04-03-2005, 11:09 PM #10 SciYro Senior Member   Registered: Oct 2003 Location: hopefully not here Distribution: Gentoo Posts: 2,038 Original Poster Rep: yea, looks like ill have to use 2 ints then thanks all
 04-04-2005, 08:40 AM #11 deiussum Member   Registered: Aug 2003 Location: Santa Clara, CA Distribution: Slackware Posts: 895 Rep: Here's a small program that can demonstrate loss of precision in floats. f1 and f2 below SHOULD be the same, but aren't. At least not on my machine (Intel P4) using g++ version 3.3.3 under Cygwin. Code: ```#include using namespace std; int main() { float f1 = 0; float f2 = 0; int i = 1000; for (int c=0;c
 04-04-2005, 09:24 AM #12 jim mcnamara Member   Registered: May 2002 Posts: 964 Rep: in limits.h you will find definitions of how precise variables are for your implementation. FLT_DIG is the number of "correct" places or digits in a float and DBL_DIG digits in a double POSIX requires that FLT_DIG be at least 6.