Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Compare/Convert Float to Text

Compare/Convert Float to Text

Scheduled Pinned Locked Moved C / C++ / MFC
tutorial
30 Posts 14 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Niklas L

    a = 0.09999;
    a *= 100; // a = 9.999
    a += 0.5; // a = 10.499
    floor(a); // a = 10.0

    b = 0.10001;
    b *= 100; // b = 10.001
    b += 0.5; // b = 10.051
    floor(b); // b = 10.0

    home

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #21

    ... and use "ceil" to round up value for example,

    printf("ceil of 2.3 is %.1lf\n", ceil (2.3));

    output

    ceil of 2.3 is 3.0

    to use the floor/ceil should be included math.h Sorry for my poor english. Best Regards, Alexander S.

    N 1 Reply Last reply
    0
    • L Lost User

      ... and use "ceil" to round up value for example,

      printf("ceil of 2.3 is %.1lf\n", ceil (2.3));

      output

      ceil of 2.3 is 3.0

      to use the floor/ceil should be included math.h Sorry for my poor english. Best Regards, Alexander S.

      N Offline
      N Offline
      Niklas L
      wrote on last edited by
      #22

      ?

      home

      L 1 Reply Last reply
      0
      • N Niklas L

        ?

        home

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #23

        float a = 1.555;
        float b = floor(a); // b = 1
        float c = ceil(a); // c = 2

        a = 0.09999;
        b = floor(a); // b = 0
        c = ceil(a); // c = 1

        modified on Tuesday, August 24, 2010 1:56 AM

        N 1 Reply Last reply
        0
        • L Lost User

          float a = 1.555;
          float b = floor(a); // b = 1
          float c = ceil(a); // c = 2

          a = 0.09999;
          b = floor(a); // b = 0
          c = ceil(a); // c = 1

          modified on Tuesday, August 24, 2010 1:56 AM

          N Offline
          N Offline
          Niklas L
          wrote on last edited by
          #24

          fasked wrote:

          Why add 0.5?

          To turn floor() into a round()

          home

          1 Reply Last reply
          0
          • I Iain Clarke Warrior Programmer

            You're kinda doing it wrong - at least in general. You code may work fine for your requirements! Try:

            BOOL IsEqualish (double d1, double d2, double dEpsilon)
            {
            ASSERT(dEpsilon > 0.0);
            d1 -= d2;
            if (d1 < 0.0)
            d1 = -d1; // or just d1 = abs (d1);
            return (d1 <= dEpsilon)
            }

            or for consise:

            BOOL IsEqualish (double d1, double d2, double dEpsilon) { return abs (d1 - d2) <= dEpsilon; }

            Where dEpsilon is the error margin. For 4 dps, dEpsilon = 0.0001, and so on. Iain.

            I am one of "those foreigners coming over here and stealing our jobs". Yay me!

            H Offline
            H Offline
            hairy_hats
            wrote on last edited by
            #25

            I think you want

            fabs

            for C/C++.

            I 1 Reply Last reply
            0
            • H hairy_hats

              I think you want

              fabs

              for C/C++.

              I Offline
              I Offline
              Iain Clarke Warrior Programmer
              wrote on last edited by
              #26

              viaducting wrote:

              I think you want fabs

              You'd be right, too! That's what happens when you code in a text editor... Not to worry - the OP is convinced all I typed was rubbish anyway! But good to point out an error, in case it helps someone else. Iain.

              I am one of "those foreigners coming over here and stealing our jobs". Yay me!

              1 Reply Last reply
              0
              • M mohit 12

                I am trying to convert float to text. the reason i am doing this is because the float contains 0.09999987 and i want that variable as 0.10 and then compare it to other float which is initially 0.10000 if anyone have idea how to convert float to text let me know thx, cheers from Canada.

                J Offline
                J Offline
                Jeff Archer
                wrote on last edited by
                #27

                Here is a very good article on comparing floats. This method is much better than converting to text for the comparison. http://www.cygnus-software.com/papers/comparingfloats/Comparing%20floating%20point%20numbers.htm[^]

                L 1 Reply Last reply
                0
                • M mohit 12

                  How am i killing performance, well all i wanted to check is if the other float value is close or 0.10 and that code does check that i cannot use greater or less than since both float can be changed might be >0 or <0 , so rounding off values check if it's close to 0.10 or not because i do not have problem in my math if value if 0.0999978 or 0.10 both will give me same answer since it just checks value and then do not use them for any calculation, i will paste my code later on here and then you can have a look at it and you might be able to give me better suggestion, thx for reply tho cheers from canada.

                  E Offline
                  E Offline
                  englebart
                  wrote on last edited by
                  #28

                  Performance is killed because CString will new and delete memory to hold the chars for both instances. If this is in a highly used piece of code, you could fragment the heck out of your heap. The Epsilon isEqualish(float, float, float) approach is the best. An alternative would be to use a Fixed Point numeric class that would exactly represent a decimal value, but that might also have performance implications. It all depends on what you are doing. Skip the CString.Format even though it seems convenient.

                  1 Reply Last reply
                  0
                  • J Jeff Archer

                    Here is a very good article on comparing floats. This method is much better than converting to text for the comparison. http://www.cygnus-software.com/papers/comparingfloats/Comparing%20floating%20point%20numbers.htm[^]

                    L Offline
                    L Offline
                    Luc Pattyn
                    wrote on last edited by
                    #29

                    Nice link! :)

                    Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                    Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

                    1 Reply Last reply
                    0
                    • M mohit 12

                      I am trying to convert float to text. the reason i am doing this is because the float contains 0.09999987 and i want that variable as 0.10 and then compare it to other float which is initially 0.10000 if anyone have idea how to convert float to text let me know thx, cheers from Canada.

                      G Offline
                      G Offline
                      glwentworth
                      wrote on last edited by
                      #30

                      don't convert it to text just decide what the acceptable error is and compare ...

                      float error = 0.00000013;
                      float lowv = 0.1 - error;
                      float highv = 0.1 + error;

                      if ((lowv <= myfloat) && (highv >= myfloat)) {
                      // good value
                      } else {
                      // bad value
                      }

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups