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. newbie C question

newbie C question

Scheduled Pinned Locked Moved C / C++ / MFC
question
8 Posts 6 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.
  • J Offline
    J Offline
    johnny alpaca
    wrote on last edited by
    #1

    Can you > and < to find the greater of two floating point (float) numbers in C? Or do I have to do something else like write a function?

    T 1 Reply Last reply
    0
    • J johnny alpaca

      Can you > and < to find the greater of two floating point (float) numbers in C? Or do I have to do something else like write a function?

      T Offline
      T Offline
      toxcct
      wrote on last edited by
      #2

      johnny alpaca wrote:

      Can you > and < to find the greater of two floating point (float) numbers in C?

      what ? what is the verb in your sentence ? :~ if you're asking "can I compare two floats using < and > operators", the short answer is : yes. the long one would say to be careful when comparing floats/doubles because of decimal precision... but that makes a question come to mind : why didn't you just test it ?

      [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

      _ 1 Reply Last reply
      0
      • T toxcct

        johnny alpaca wrote:

        Can you > and < to find the greater of two floating point (float) numbers in C?

        what ? what is the verb in your sentence ? :~ if you're asking "can I compare two floats using < and > operators", the short answer is : yes. the long one would say to be careful when comparing floats/doubles because of decimal precision... but that makes a question come to mind : why didn't you just test it ?

        [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

        _ Offline
        _ Offline
        _AnsHUMAN_
        wrote on last edited by
        #3

        verb "use" :)

        Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_

        T 1 Reply Last reply
        0
        • _ _AnsHUMAN_

          verb "use" :)

          Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_

          T Offline
          T Offline
          toxcct
          wrote on last edited by
          #4

          i don't see it then ;P

          [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

          J 1 Reply Last reply
          0
          • T toxcct

            i don't see it then ;P

            [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

            J Offline
            J Offline
            johnny alpaca
            wrote on last edited by
            #5

            Sorry for the bad english. Thanks I thought so. It worked on my PC but just wanted to confirm that it could be done without any problems just like integers. Also what about testing equality of two floating point numbers ? They could be tested for almost equality as I understand it

            K M D 3 Replies Last reply
            0
            • J johnny alpaca

              Sorry for the bad english. Thanks I thought so. It worked on my PC but just wanted to confirm that it could be done without any problems just like integers. Also what about testing equality of two floating point numbers ? They could be tested for almost equality as I understand it

              K Offline
              K Offline
              Kwanalouie
              wrote on last edited by
              #6

              You can get into trouble checking the equality of floating point numbers. As a decimal example, if you did an operation such as adding 1 + 1. x = 1.0 + 1.0; the internal respresentation might end up being x = 1.999999999999999999999999999999999 instead of 2.0000000000000000000000000000 if you then checked if (x == 2.0) that would be false instead of true.

              1 Reply Last reply
              0
              • J johnny alpaca

                Sorry for the bad english. Thanks I thought so. It worked on my PC but just wanted to confirm that it could be done without any problems just like integers. Also what about testing equality of two floating point numbers ? They could be tested for almost equality as I understand it

                M Offline
                M Offline
                Maximilien
                wrote on last edited by
                #7

                johnny alpaca wrote:

                They could be tested for almost equality as I understand it

                Yes, you are right; floating points are not well represented in binary forms, so that a value like 1.4 can be stored as 1.39999999 or 1.400000001 ( for example) so if yo need to compare floating point numbers, you have to check them against a pre-defined precision; they are identical "up to" a certain precision for example:

                double x;
                double y;
                double epsilon = 0.00001;

                //they are equal if the difference between them is smaller than epsilon.
                bool bEqual = fabs( x - y ) < epsilon;

                this is a good read : http://docs.sun.com/source/806-3568/ncg_goldberg.html[^]

                1 Reply Last reply
                0
                • J johnny alpaca

                  Sorry for the bad english. Thanks I thought so. It worked on my PC but just wanted to confirm that it could be done without any problems just like integers. Also what about testing equality of two floating point numbers ? They could be tested for almost equality as I understand it

                  D Offline
                  D Offline
                  David Crow
                  wrote on last edited by
                  #8

                  johnny alpaca wrote:

                  They could be tested for almost equality as I understand it

                  Yes, with the difference being epsilon.

                  "Love people and use things, not love things and use people." - Unknown

                  "The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch

                  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