storing a float value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • saurabhamiable
    New Member
    • Oct 2007
    • 5

    storing a float value

    I would like to know that why float datayppe never stores exact value that is assigned to them?
    for eg-> A piece of code
    main()
    {
    float a=.07;
    if (a<0.7)
    printf("mismatc h");
    else
    printf("C is precise");
    }
    Output: mismatch
  • debasisdas
    Recognized Expert Expert
    • Dec 2006
    • 8119

    #2
    Question moved to C Forum.

    Comment

    • rhitam30111985
      New Member
      • Aug 2007
      • 112

      #3
      Originally posted by saurabhamiable
      I would like to know that why float datayppe never stores exact value that is assigned to them?
      for eg-> A piece of code
      main()
      {
      float a=.07;
      if (a<0.7)
      printf("mismatc h");
      else
      printf("C is precise");
      }
      Output: mismatch
      you have assigned 0.07 to a, and comparing with 0.7... obviously there is a mismatch

      Comment

      • Meetee
        Recognized Expert Contributor
        • Dec 2006
        • 928

        #4
        0.07<0.7, it's a normal logic. So if(a<0.7) fulfills the condition and output will be mismatch!!

        Regards

        Comment

        • mattmao
          New Member
          • Aug 2007
          • 121

          #5
          Originally posted by saurabhamiable
          I would like to know that why float datayppe never stores exact value that is assigned to them?
          for eg-> A piece of code
          main()
          {
          float a=.07;
          if (a<0.7)
          printf("mismatc h");
          else
          printf("C is precise");
          }
          Output: mismatch
          If you want to make your code "interactiv e" with the user, google for "c scanf"

          You can change your code to:
          Code:
          main()
             {
                   float a;
                  
                   printf("input a float value\n");
                   scanf("%f", &a);
          
                   if (a<0.7)
                    printf("mismatch");
                    else
                     printf("C is precise");
            }

          Comment

          • saurabhamiable
            New Member
            • Oct 2007
            • 5

            #6
            Originally posted by rhitam30111985
            you have assigned 0.07 to a, and comparing with 0.7... obviously there is a mismatch

            actually i typed it wrongly, the value assigned to a is 0.7, then there is a mismatch. Kindly let me know.

            Comment

            • Savage
              Recognized Expert Top Contributor
              • Feb 2007
              • 1759

              #7
              Originally posted by saurabhamiable
              actually i typed it wrongly, the value assigned to a is 0.7, then there is a mismatch. Kindly let me know.
              You cannot compare floats with < operator,becaus e of float point accuracy.Epsilo n is your friend.See this ..

              Savage

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by saurabhamiable
                actually i typed it wrongly, the value assigned to a is 0.7, then there is a mismatch. Kindly let me know.
                What Every Computer Scientist Should Know About Floating-Point Arithmetic.

                kind regards,

                Jos

                Comment

                Working...