C++
  Home arrow C++ arrow Page 4 - First Steps in (C) Programming, continued
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C++

First Steps in (C) Programming, continued
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 12
    2005-11-23

    Table of Contents:
  • First Steps in (C) Programming, continued
  • Variables and Memory
  • Integer Constants
  • Floating-Point Variables
  • More on Format Specifiers
  • More Complex Expressions
  • Defining Constants
  • Try It Out: The Right Types of Variables

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    First Steps in (C) Programming, continued - Floating-Point Variables


    (Page 4 of 8 )

    There are three different types of floating-point variables, as shown in Table 2-5.

    Table 2-5. Floating-Point Variable Types

     

    Number

     

    Keyword

    of Bytes

    Range of Values

    float

    4

    ±3.4E38 (7 decimal digits precision)

    double

    8

    ±1.7E308 (15 decimal digits precision)

    long double

    10

    ±1.2E4932 (19 decimal digits precision)

    These are typical values for the number of bytes occupied and the ranges of values that are supported. Like the integer types, the memory occupied and the range of values are dependent on the machine and the compiler. The typelong doubleis sometimes exactly the same as typedoublewith some compilers. Note that the number of decimal digits of precision is only an approximation because floating-point values will be stored internally in binary form.

    A floating-point variable is declared in a similar way to an integer variable. You just use the keyword for the floating-point type that you want to use:

    float Radius;
    double Biggest;

    If you need to store numbers with up to seven digits of accuracy (a range of 10-38 to 10+38), then you should use variables of typefloat. Values of typefloatare known as single precision floating-point numbers. This type will occupy 4 bytes in memory, as you can see from the table. Using variables of typedoublewill allow you to store double precision floating-point values. Each variable of typedouble will occupy 8 bytes in memory and give you 15-digit precision with a range of 10-308 to 10+308. Variables of typedouble suffice for the majority of requirements, but some specialized applications require even more accuracy and range. Thelong doubletype provides the exceptional range and precision shown in the table.

    To write a constant of typefloat, you append anfto the number to distinguish it from typedouble. You could have initialized the last two variables with these statements:

    float Radius = 2.5f;
    double Biggest = 123E30;

    The variableRadiushas the initial value 2.5, and the variableBiggestis initialized to the number that corresponds to 123 followed by 30 zeroes. Any number that you write containing a decimal point is of typedoubleunless you append theFto make it typefloat. When you specify an exponent value withEore, the constant need not contain a decimal point. For instance,1E3f is of typefloatand3E8is of typedouble.

    To specify along doubleconstant, you need to append an upper- or lowercase letterLto the number, for example:

    long double huge = 1234567.89123L;

    Division Using Floating-Point Values

    As you’ve seen, division operations with integer operands always produce an integer result. Unless the left operand of a division is an exact multiple of the right operand, the result will be inherently inaccurate. Of course, the way integer division works is an advantage if you’re distributing cookies to children, but it isn’t particularly useful when you want to cut a 10-foot plank into four equal pieces. This is a job for floating-point values.

    Division operations with floating-point values will give you an exact result—at least, a result that is as exact as it can be with a fixed number of digits of precision. The next example illustrates how division operations work with variables of typefloat.

    ..........................................................................................

    Try It Out: Division with Values of Type float

    Here’s a simple example that just divides one floating-point value by another and displays the result:

    /* Program 2.7 Division with float values */
    #include <stdio.h>
    void main()
    {
      float plank_length = 10.0f; /* In feet */ 
      float piece_count = 4.0f;   /* Number of equal pieces  */ 
      float piece_length = 0.0f;  /* Length of a piece in feet */
      piece_length = plank_length/piece_ccount;
      printf("A plank %f feet long can be cut into %f pieces %f feet long.",
                      plank_length, piece_count, piece_length);
    }

    This program should produce the following output:

    ----------------------------------------------------------------------A plank 10.000000 feet long can be cut into 4.000000 pieces 2.500000 feet long.
    --------------------------------------------

    HOW IT WORKS

    You shouldn’t have any trouble understanding how you chop the plank into equal pieces. Note that you use a new format specifier for values of typefloatin theprintf()statement:

    printf("A plank %f feet long can be cut into %f pieces %f feet long.",
                                  plank_length, piece_count, piece_length);

    You use the format specifier%fto display floating-point values. In general, the format specifier that you use must correspond to the type of value that you’re outputting. If you output a value of typefloatwith the specifier%dthat’s intended for use with integer values, you’ll get rubbish. This is because thefloatvalue will be interpreted as an integer, which it isn’t. Similarly, if you use%fwith a value of an integer type, then you’ll also get rubbish as output.

    ..........................................................................................

    More C++ Articles
    More By Apress Publishing


       · This article is an excerpt from the book "Beginning C", published by Apress. We hope...
     

    Buy this book now. This article is excerpted from chapter two of the book Beginning C, written by Ivor Horton (Apress, 2004; ISBN: 1590592530). Check it out at your favorite bookstore today. Buy this book now.

    C++ ARTICLES

    - More Tricks to Gain Speed in Programming Con...
    - Easy and Efficient Programming for Contests
    - Preparing For Programming Contests
    - Programming Contests: Why Bother?
    - Polymorphism in C++
    - Overview of Virtual Functions
    - Inheritance in C++
    - Extending the Basic Streams in C++
    - Using Stringstreams in C++
    - Custom Stream Manipulation in C++
    - General Stream Manipulation in C++
    - Serialize Your Class into Streams in C++
    - Advanced File Handling with Streams in C++
    - File Handling and Streams in C++
    - The STL String Class







    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 12 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek