You are here:About>Computing & Technology>C / C++ / C#> C> C Tutorials> All about Precision in Floats
About.comC / C++ / C#

C Tutorial Lesson Two - Handling Numbers

From David Bolton,
Your Guide to C / C++ / C#.
FREE Newsletter. Sign Up Now!

Precision Arithmetic

Double Trouble

There's no long float, but there is a double type that is twice as big as float.
  • Float: Occupies 4 bytes. Range 1.175494351×10−38 to ±3.4028235×1038
  • Double: Occupies 8 bytes. Range 2.2250738585072020×10−308 to ±1.7976931348623157×10308
Unless you're doing scientific programming with very large or small numbers, you'll only use doubles for greater precision. Floats are good for 6 digits of accuracy but doubles offer 15.

Precision

Consider 567.8976523. It appears a valid float value. But if we print it out with this code below you can see lack of precision appearing. The number has 10 digits but is being stored in a float variable with only six digits of precision.

float value= 567.8976523;
printf( value) ;

The function printf() outputs text to the screen. We'll look at it in detail later on in this lesson.

It prints as 567.897644042969. Quite a difference! Now move the decimal point two to the left so the value is 5.678976523 and rerun the program. This time it outputs 5.67897653579712. This is more accurate but still different.

If you count the digits that are the same in both sets of numbers, you can see that after 7 or 8 digits, the numbers differ. I've added a space and padded with 0s to highlight the differences.

  • 567.8976 52300000
  • 567.8976 44042969
  • 5.6789765 2300000
  • 5.6789765 3579712

Rerun both examples as doubles and it will print both exactly as defined.

On the next page : Learn about computer Arithmetic.

  1. All About Numbers in C
  2. More About Ints
  3. Precision Arithmetic
  4. Learn about Arithmetic Operations
  5. Specifying Output Formats in Printf
  6. Learn how to use Format Specifiers

<< Previous | Next >>

 All Topics | Email Article | Print this Page | |
Advertising Info | News & Events | Work at About | SiteMap | Reprints | HelpOur Story | Be a Guide
User Agreement | Ethics Policy | Patent Info. | Privacy Policy©2008 About, Inc., A part of The New York Times Company. All rights reserved.