length of double???

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kgkgkg
    New Member
    • Apr 2009
    • 5

    length of double???

    I'm sort of new to c++ but i do have years experience in other languages. So if this is a rediculous question and im just not seeing the obvious answer, don't reserve yourselves, bring on the bluntness

    So here my question...

    Like you may know, there are functions for finding the length of a string (myString.lengt h()), but is it possible to find the length of a double?? More specifically if i have myDouble = 4.53 then i guess the length would be 3.

    Example:

    Code:
    void myFunction(string inputString)
    {
    double tmpDbl;
    
    //convert string to float
    tmpDbl = atof(inputString.c_str());
    
    this->myDouble = tmpDbl;
    
    //I NEED HELP HERE!!!
    
    this->myLength = ??? (the length of tmpDbl)
    
    }
  • whodgson
    Contributor
    • Jan 2007
    • 542

    #2
    As far as I know it is not 3.
    Use the sizeof() function and it will tell you the width in bits (probably 8 on a 32 bit system)
    hth's

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      A double number always takes eight bytes, no matter whether you store a simple 1.0 or 1.23456789; the number is stored in the IEEE754 format and has its peculiarties. In the first article of this group is a link to an article that explains everything about floating point format numbers and their inaccuracies and everything.

      kind regards,

      Jos

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        Are you asking about the physical length of a double? As already reported, the physical length (which is more properly termed the 'size') is not affected by the value of the double variable.

        Are you asking about a logical length of a double -- something like the number of significant digits? That's an interesting question. I can think of situations where keeping track of the number of significant digits would be helpful. I can't think of any way to extract that information from a double variable. Take your example: you assign the value 4.53 to a double variable, but due to floating-point imprecision the value actually stored in your variable is 4.5299999879. How can you recognize that the number of significant digits of that actual value is 3?

        Comment

        • kgkgkg
          New Member
          • Apr 2009
          • 5

          #5
          First, I want to thank you all, you all are awesome and I'll take all of your advice and keep track of the amount of bytes.

          Second, about the question that i asked, just for reference, and to clarify a bit on what i was asking:

          I know that in c# and vb.net i can take a string variable and measure it's length or size (don't know if I'm using the right terminology). for example

          Code:
          C#
          
          int stringLength;
          string myString = "hello kyle!";
          
          stringLength = myString.Length();
          So stringLength should now hold the value of 11, because the Length() function counts all the ascii characters that myString is made up of.

          So now if i had the same example as above, but instead of using a String, used a Double.

          Code:
          C#
          
          int dblLength;
          double myDbl = "12345";
          
          dblLength = myDbl.Length();
          I know that there isn't a member function for double that gives it's length back (the count of ascii characters), but i want to check if there were any other functions that any of you know of that would count up the characters in myDbl.

          --Kyle

          Comment

          • donbock
            Recognized Expert Top Contributor
            • Mar 2008
            • 2427

            #6
            Originally posted by kgkgkg
            <snip>

            So now if i had the same example as above, but instead of using a String, used a Double.

            Code:
            C#
            
            int dblLength;
            double myDbl = "12345";
            
            dblLength = myDbl.Length();
            I know that there isn't a member function for double that gives it's length back (the count of ascii characters), but i want to check if there were any other functions that any of you know of that would count up the characters in myDbl.
            Notice that the value you assign to myDbl is enclosed in double quotes. That makes it a string. I don't know about C#, but C does not let you assign a string value to a double variable. However, you could do something like this:
            Code:
            #include <stdio.h>
            int valueLength;
            double valueFP;
            const char * const valueString = "12345";
            valueFP = strtod(valueString, NULL);
            valueLength = strlen(valueString);
            but it would only work with literal constants, not the results of computation. I don't really see much value in jumping through all these hoops.

            How would you use dblLength if you could get it?

            Comment

            • kgkgkg
              New Member
              • Apr 2009
              • 5

              #7
              I didn't mean to put double quotes around the value, what I meant to do was

              Code:
              myDbl = 12345;
              sorry about that. That's what happens when i copy past code, I butcher it, I always forget to change something.

              But the scope: It would be in a class that would be able to take a string value or a double in the constructor, which would convert the string value to a double, and that is how it would be stored internally. But after posting this and talking about it with you guys, I've come to the conclusion that it would just be easier to store the values internally as a string. The advantages being that i could get the length of the string, then i could always just convert to a double when needed for output.

              Thank you for all of your input!

              --kyle

              Comment

              Working...