ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Welcome to LinuxQuestions.org, a friendly and active Linux Community.

You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!

Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.

If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.

Having a problem logging in? Please visit this page to clear all LQ-related cookies.

Introduction to Linux - A Hands on Guide

This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.

Click Here to receive this Complete Guide absolutely free.

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.

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.

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.

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.

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.

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;
}

LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.