Click here to Skip to main content
15,906,625 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Compare/Convert Float to Text Pin
Iain Clarke, Warrior Programmer23-Aug-10 7:57
Iain Clarke, Warrior Programmer23-Aug-10 7:57 
GeneralRe: Compare/Convert Float to Text Pin
mohit`1223-Aug-10 9:16
mohit`1223-Aug-10 9:16 
GeneralRe: Compare/Convert Float to Text Pin
Iain Clarke, Warrior Programmer23-Aug-10 9:36
Iain Clarke, Warrior Programmer23-Aug-10 9:36 
GeneralRe: Compare/Convert Float to Text Pin
hairy_hats24-Aug-10 2:50
hairy_hats24-Aug-10 2:50 
GeneralRe: Compare/Convert Float to Text Pin
Iain Clarke, Warrior Programmer24-Aug-10 3:03
Iain Clarke, Warrior Programmer24-Aug-10 3:03 
GeneralRe: Compare/Convert Float to Text PinPopular
Aescleal23-Aug-10 7:58
Aescleal23-Aug-10 7:58 
GeneralRe: Compare/Convert Float to Text Pin
mohit`1223-Aug-10 9:17
mohit`1223-Aug-10 9:17 
GeneralRe: Compare/Convert Float to Text [modified] Pin
Aescleal23-Aug-10 10:21
Aescleal23-Aug-10 10:21 
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

GeneralRe: Compare/Convert Float to Text Pin
Tim Craig23-Aug-10 9:30
Tim Craig23-Aug-10 9:30 
AnswerRe: Compare/Convert Float to Text Pin
Niklas L23-Aug-10 8:00
Niklas L23-Aug-10 8:00 
GeneralRe: Compare/Convert Float to Text Pin
mohit`1223-Aug-10 9:18
mohit`1223-Aug-10 9:18 
GeneralRe: Compare/Convert Float to Text Pin
Niklas L23-Aug-10 10:08
Niklas L23-Aug-10 10:08 
GeneralRe: Compare/Convert Float to Text Pin
fasked23-Aug-10 18:48
fasked23-Aug-10 18:48 
QuestionRe: Compare/Convert Float to Text Pin
Niklas L23-Aug-10 19:37
Niklas L23-Aug-10 19:37 
AnswerRe: Compare/Convert Float to Text [modified] Pin
fasked23-Aug-10 19:40
fasked23-Aug-10 19:40 
GeneralRe: Compare/Convert Float to Text Pin
Niklas L23-Aug-10 20:23
Niklas L23-Aug-10 20:23 
AnswerRe: Compare/Convert Float to Text PinPopular
Luc Pattyn23-Aug-10 8:42
sitebuilderLuc Pattyn23-Aug-10 8:42 
GeneralRe: Compare/Convert Float to Text Pin
mohit`1223-Aug-10 9:20
mohit`1223-Aug-10 9:20 
GeneralRe: Compare/Convert Float to Text Pin
Luc Pattyn23-Aug-10 9:28
sitebuilderLuc Pattyn23-Aug-10 9:28 
AnswerRe: Compare/Convert Float to Text Pin
Jeff Archer24-Aug-10 4:02
Jeff Archer24-Aug-10 4:02 
AnswerRe: Compare/Convert Float to Text Pin
Luc Pattyn24-Aug-10 4:57
sitebuilderLuc Pattyn24-Aug-10 4:57 
AnswerRe: Compare/Convert Float to Text Pin
glwentworth24-Aug-10 6:18
professionalglwentworth24-Aug-10 6:18 
QuestionVS2010 debugger can not see some local variables? Pin
Chesnokov Yuriy23-Aug-10 6:56
professionalChesnokov Yuriy23-Aug-10 6:56 
AnswerRe: VS2010 debugger can not see some local variables? Pin
Richard MacCutchan23-Aug-10 7:18
mveRichard MacCutchan23-Aug-10 7:18 
GeneralRe: VS2010 debugger can not see some local variables? Pin
Chesnokov Yuriy23-Aug-10 20:24
professionalChesnokov Yuriy23-Aug-10 20:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.