newbie C question
-
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?
-
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?
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]
-
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]
verb "use" :)
Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_
-
verb "use" :)
Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_
i don't see it then ;P
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
i don't see it then ;P
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
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
-
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
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.
-
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
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[^]
-
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
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