Compare/Convert Float to Text
-
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.
You need to compare the difference to some epsilon. Check float.h for
DBL_EPSILON
orFLT_EPSILON
."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
-
You need to compare the difference to some epsilon. Check float.h for
DBL_EPSILON
orFLT_EPSILON
."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
thx all for your support.. nvm it i solved it ..stupid ME ... just added this method: bool classname::isStringEquals(float fNewNumber,float fOldNumber) { CString value1; CString value2; value1.Format( _T("%.4f"), fNewNumber); value2.Format( _T("%.4f"), fOldNumber); if(value1==value2) return true; else return false; cheers from canada
-
thx all for your support.. nvm it i solved it ..stupid ME ... just added this method: bool classname::isStringEquals(float fNewNumber,float fOldNumber) { CString value1; CString value2; value1.Format( _T("%.4f"), fNewNumber); value2.Format( _T("%.4f"), fOldNumber); if(value1==value2) return true; else return false; cheers from canada
bad answer, bad solution. You are rounding off values and killing performance. Read this : What Every Computer Scientist Should Know About Floating-Point Arithmetic[^] Max.
Watched code never compiles.
-
thx all for your support.. nvm it i solved it ..stupid ME ... just added this method: bool classname::isStringEquals(float fNewNumber,float fOldNumber) { CString value1; CString value2; value1.Format( _T("%.4f"), fNewNumber); value2.Format( _T("%.4f"), fOldNumber); if(value1==value2) return true; else return false; cheers from canada
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!
-
thx all for your support.. nvm it i solved it ..stupid ME ... just added this method: bool classname::isStringEquals(float fNewNumber,float fOldNumber) { CString value1; CString value2; value1.Format( _T("%.4f"), fNewNumber); value2.Format( _T("%.4f"), fOldNumber); if(value1==value2) return true; else return false; cheers from canada
-
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.
-
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.
using strings for numeric information when you don't need strings is never the right approach. For numerical problems, use a numeric solution. Here are some possibilities: 1. you might put an upper limit on the deviation, similar to:
float is=0.999999877543; float closeTo=1.0; bool closeEnough=abs(is-closeTo)<0.01;
that would be close enough within 1% (since closeTo itself is one). 2. you might opt for a relative deviation (assuming the ideal value isn't zero):
float is=12.999999877543; float closeTo=13.0; bool closeEnough=abs((is-closeTo)/closeTo)<1.0e-6;
that would be close enough when within 1 ppm (pars pro million) :)
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.
-
bad answer, bad solution. You are rounding off values and killing performance. Read this : What Every Computer Scientist Should Know About Floating-Point Arithmetic[^] Max.
Watched code never compiles.
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.
-
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'm not a great one for going mental about code's performance but that's just horrible on so many levels. Why not do something like:
bool near_enough_equals( double a, double b )
{
return (b - a) < ( (b + a) / 2000.0 );
}Cheers, Ash
I am dealing with float and there is not way i can use DOUBLE for that particular code and my value might be negative so it might return negative value using your code and also b will be always less than a so negative :doh: thx for reply cheers from canada
-
Why use strings? Just round the values.
double a = 1.00003;
double b = 0.99997;
a *= 100;
b *= 100;
a += 0.5;
b += 0.5;if (::floor(a) == ::floor(b))
{
// Wooha!
} -
using strings for numeric information when you don't need strings is never the right approach. For numerical problems, use a numeric solution. Here are some possibilities: 1. you might put an upper limit on the deviation, similar to:
float is=0.999999877543; float closeTo=1.0; bool closeEnough=abs(is-closeTo)<0.01;
that would be close enough within 1% (since closeTo itself is one). 2. you might opt for a relative deviation (assuming the ideal value isn't zero):
float is=12.999999877543; float closeTo=13.0; bool closeEnough=abs((is-closeTo)/closeTo)<1.0e-6;
that would be close enough when within 1 ppm (pars pro million) :)
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.
-
as i mentioned in previous post those value can be changed so and it may vary from negative to zero and zero to +2.707 E06 so not sure if that would work.
of course it can work. Whatever you can achieve with strings can be done without strings. FYI: electronic circuit simulators use a combination of absolute and relative tolerance, so they would do a convergence test similar to the following:
float previous=...;
float current=...;
float threshold=0.1e-3; // an absolute tolerance of 0.1 millivolt for small voltages
if (abs(previous)>1) threshold+=abs(previous)*100.0e-6; // a relative tolerance of 100 ppm
bool converged = abs(current-previous) < threshold;in reality the two magic constants are variables, as the user is allowed to choose their value. :)
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.
-
thx all for your support.. nvm it i solved it ..stupid ME ... just added this method: bool classname::isStringEquals(float fNewNumber,float fOldNumber) { CString value1; CString value2; value1.Format( _T("%.4f"), fNewNumber); value2.Format( _T("%.4f"), fOldNumber); if(value1==value2) return true; else return false; cheers from canada
mohit`12 wrote:
if(value1==value2) return true; else return false;
Ok, setting aside your real problem, why not simply code this as: return (value1 == value2); Call me anal if you like, but that's so much simpler and easier to read.
Once you agree to clans, tribes, governments...you've opted for socialism. The rest is just details.
-
I didn't get your code why would i use epsilon ? and it has to be FLOAT thx for reply. cheers from canada
mohit`12 wrote:
I didn't get your code why would i use epsilon ?
You yourself brought up the problem of floating point numbers being not perfect - if you read some of the links given you, you will realise that testing for equality is basically a waste of time. Digital computers simply do not do perfect representations of non-integers. Unless you're very lucky, and it's 1/(2^n). Two numbers can be reeeeeeeally close, and not equal. The simple fact you asked your original question shows that you know this already.
mohit`12 wrote:
and it has to be FLOAT
So change
double
tofloat
. Or whateverFLOA
Ts your boat. Not the hardest of challenges! Iain. ps, My code will also cope (as it should) with negative floating values.I am one of "those foreigners coming over here and stealing our jobs". Yay me!
-
floor will convert 0.099999787 to 0.10 ? and it has to be float value will it work ? thx cheers from canada.
-
I am dealing with float and there is not way i can use DOUBLE for that particular code and my value might be negative so it might return negative value using your code and also b will be always less than a so negative :doh: thx for reply cheers from canada
Floats are converted to doubles before any serious arithmetic is done on them. The only reason you'd use a float is if you really had to save the 4 bytes (on most implementations). You actually introduce more arithmetic errors by saving everything as a float between calculations than doing everything as a double. As for the negative problem, use std::fabs or something on the values before processing them:
bool are_these_two_near_enough( double a, double b )
{
a = std::fabs( a ); b = fabs( b );return std::fabs( a - b ) < ( ( a + b ) / 2000.0 );
}
and if you're really worried about the difference between float and double use this as the function signature:
template< typename T >
bool are_these_two_floating_point_values_near_enough( T a, T b )Edited as I didn't have all the HTML escapes properly sorted
modified on Tuesday, August 24, 2010 5:54 AM
-
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.