Floating Point Help?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • khooldude
    New Member
    • Mar 2007
    • 1

    #1

    Floating Point Help?

    I was wondering if there is a way in C to determine the lenght of a floating point value?

    EX:

    float x=1.234;

    I am wondering if there is a function where i plug in x and it will return the length to be 4?
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    The "length" as you are defining it will always be 7 because that is how much precision a float has.

    You may thing you have 1.234 but actually you have 1.233999 or 1.234001 or 1.234000.

    Whatever it is you are trying to solve you need to think in different terms.

    Comment

    • gpraghuram
      Recognized Expert Top Contributor
      • Mar 2007
      • 1275

      #3
      Hi,
      There is no function to find the length of the float value.
      You can do it this way
      Code:
      char arr[100];
      int fval=1.234
      sprintf(arr,"%f",fval);
      return (strlen(arr)-1);
      Thanks
      Raghuram

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        Originally posted by gpraghuram
        Hi,
        There is no function to find the length of the float value.
        You can do it this way
        Code:
        char arr[100];
        int fval=1.234
        sprintf(arr,"%f",fval);
        return (strlen(arr)-1);
        Thanks
        Raghuram
        Of course, since you are using a decimal number, it will have to be a float rather than an int :)

        Comment

        • bss
          New Member
          • Mar 2007
          • 2

          #5
          Originally posted by Ganon11
          Of course, since you are using a decimal number, it will have to be a float rather than an int :)

          can't we use sizeof() ?

          Comment

          • horace1
            Recognized Expert Top Contributor
            • Nov 2006
            • 1510

            #6
            As bss says sizeof(float) will give the number of bytes used to store a float.
            In addition the header file <float.h> gives the radix, precision, minumum, maximum, epsilon, etc. of floating point types, see
            http://www.cplusplus.c om/reference/clibrary/cfloat/
            this is useful to determine if a given implementation has sufficent precision for an application., etc

            Comment

            Working...