LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 04-03-2005, 01:30 PM   #1
SciYro
Senior Member
 
Registered: Oct 2003
Location: hopefully not here
Distribution: Gentoo
Posts: 2,038

Rep: Reputation: 51
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.
 
Old 04-03-2005, 02:12 PM   #2
ruidh
LQ Newbie
 
Registered: Jan 2005
Posts: 24

Rep: Reputation: 15
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.
 
Old 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: Reputation: 51
thanks.

there not precise?, i thought the point of them was to be precise.
 
Old 04-03-2005, 04:55 PM   #4
puffinman
Member
 
Registered: Jan 2005
Location: Atlanta, GA
Distribution: Gentoo, Slackware
Posts: 217

Rep: Reputation: 31
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.
 
Old 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: Reputation: 51
thanks
 
Old 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: Reputation: 16
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.
 
Old 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: Reputation: 51
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)
 
Old 04-03-2005, 08:07 PM   #8
Dave Kelly
Member
 
Registered: Aug 2004
Location: Todd Mission Texas
Distribution: Linspire
Posts: 215

Rep: Reputation: 31
Have you read through the 'math.h' in /usr/include. Lots of good imformation there.
 
Old 04-03-2005, 09:10 PM   #9
ruidh
LQ Newbie
 
Registered: Jan 2005
Posts: 24

Rep: Reputation: 15
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.
 
Old 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: Reputation: 51
yea, looks like ill have to use 2 ints then

thanks all
 
Old 04-04-2005, 08:40 AM   #11
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
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 <iostream>

using namespace std;

int main()
{
    float f1 = 0;
    float f2 = 0;
    int i = 1000;

    for (int c=0;c<i;c++)
    {
        f1 += 0.1;
    }

    f2 = 0.1 * i;

    cout << "f1: " << f1 << " f2: " << f2 << endl;

    return 0;
}
Code:
Results:
f1: 99.999 f2: 100
 
Old 04-04-2005, 09:24 AM   #12
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Big O, Big Omega, and Big Theta oulevon Programming 7 05-26-2010 07:18 AM
[C++] char * to float Ossar Programming 2 08-03-2005 10:28 AM
count digits of a float || convert float to string nadroj Programming 6 07-11-2005 04:52 PM
How to use a float in a script. philipina Programming 4 03-18-2004 08:06 AM
C newbie float ? bluesky Programming 7 09-03-2003 12:51 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration