Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New to the CF scene
    Join Date
    Apr 2004
    Location
    Red Deer, AB
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Data Types in C++

    I'm taking an introductory C++ course right now but I'm having some difficulty with data types, specifically float and double.

    From my textbook, C++ Programming Today:

    Float: data type for variables containing up to five digits of decimal precision.

    Double: data type for variables containing up to ten digits of decimal precision.

    Alright, pretty straightforward. But on the next page there is a table comparing all of the data types. Here it says:

    Float contains a number with six to seven digits of decimal precision, ie 14.937453.

    Double contains a number with thirteen to fourteen digits of decimal precision, ie 3.14159265294753

    These statements seem contradictory to me, and it continues throughout the text. I've looked a bunch of stuff up on google trying to find an answer but none of the results were really helpful to me; the only useful information I got was that it seems the ranges vary depending on what compiler you're using. I'm using MS Visual C++ (course requirement).

    Can anyone clarify this for me, offer any suggestions or provide any useful links?

    Thanks.

  • #2
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I don't know myself... maybe your instructor would know?

    If I were you, the only reason I'd be curious to know the answer from my professor as he/she will be the one asking the question, and I'd like to know what answer he/she expects!

    Sadiq.

  • #3
    New to the CF scene
    Join Date
    Apr 2004
    Location
    Red Deer, AB
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The thing is I don't really have an instructor, it's an internet course .

    The reason I'm asking is because my text book later asks a question along the lines of:

    Given

    float x=3.0392039, y=483.33;

    what are the values that are stored in memory?

  • #4
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I don't have C++ installed on my computer.. but I used Java to produce the following output:
    Code:
    public class TestFloat {
     public static void main(String[] args) {
      float x=(float)3.0392039, y=(float)483.33;
      System.out.println(x+" "+y);
     }
    }
    
    /*
     output:
     3.039204 483.33
    */
    So anyway, try something similar in C++ and see what you come up with.

    Sadiq.

  • #5
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    The precision of float and double is not a definite number of decimals. The floating point numbers are constructed using two binary components, and depending on the actual numerical value being represented, those binary components can be converted into a variable number of decimals.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #6
    New to the CF scene
    Join Date
    Apr 2004
    Location
    Red Deer, AB
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up

    Sadiq,

    I actually tried that code (in c++) earlier:

    Code:
    float a = 4.0, b = 8.0, c= 1.5;
    	int x = 5, y = 7.5, z = 19.0;
    	float q, r;
    	int s, t;
    
    	q = y *a+a *c;
    	r = z%x+b/a;
    	s = x+z/y*c;
    	t = b/a * b*x + c;
    
    	cout<< a << b << c << x << y << z << q << r << s << t;
    The output doesn't help at all, when it displays the value for 'a' (float=4.0) it displays '4' (no decimal). But what's output on the screen doesn't necessarily indicate what's stored in memory...right? Thanks for your reply.

    liorean

    I apologize, I'm having a hard time understanding your reply (I'm fairly new to programming ) but I think that's the kind of answer I'm looking for. Could you perhaps elaborate on what you mean, maybe even an example? Thanks again.

  • #7
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    I just know the basics behind it myself, I never got further into how it is actually encoded. However, Google is your friend when it comes to finding this type of data.

    Easy to understand if you know how to think binary: <http://www.webpronews.com/webdevelop...ntFormats.html>

    More technical, but explains both single and double precision:
    <http://support.microsoft.com/default...NoWebContent=1>

    Even more technichal:
    <http://babbage.cs.qc.edu/courses/cs3...eferences.html>
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #8
    Regular Coder
    Join Date
    Dec 2003
    Posts
    367
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Here is a nice explanation about the floating point format.

    The actual precision of floats and doubles is machine dependent.

    shmoove

  • #9
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Yes, it is machine independent. However, the standard that almost all processor manufacturers use today is IEEE 754, which was outlined by the documents I pointed to. Both the x86 and the PPC uses IEEE 754 as their native floating point format, so there's your standard. Also, IEEE 754 does not vary depending on OS or processor architecture: Single precision and double precision are both defined to be a certain bit size in the spec.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #10
    New to the CF scene
    Join Date
    Apr 2004
    Location
    Red Deer, AB
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for your help and the links!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •