Compare/Convert Float to Text
-
a = 0.09999;
a *= 100; // a = 9.999
a += 0.5; // a = 10.499
floor(a); // a = 10.0b = 0.10001;
b *= 100; // b = 10.001
b += 0.5; // b = 10.051
floor(b); // b = 10.0 -
... 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.
-
float a = 1.555;
float b = floor(a); // b = 1
float c = ceil(a); // c = 2a = 0.09999;
b = floor(a); // b = 0
c = ceil(a); // c = 1modified on Tuesday, August 24, 2010 1:56 AM
-
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!
I think you want
fabs
for C/C++.
-
I think you want
fabs
for C/C++.
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!
-
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.
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[^]
-
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.
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.
-
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[^]
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.
-
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.
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
}