WxWidgets: Does a simple mathematical formula convey incorrect results?

I really like WxWidgets, and I started with C ++ programming. My sample program converts Celsius to Fahrenheit from a text form. Here is my main code:

//get "100" from textbox
wxString szCelsius = TextCtrl1->GetValue();
long lCelsius;

//attempt to cast into long
szCelsius.ToLong(&lCelsius, 10);

//formula that works in normal cases to get fahrenheit
long lFahrenheit = ((9.f/5.f) * lCelsius + 32);

//SOMEHOW this works:
//long lFahrenheit = ((9.f/5.f) * 100 + 32);

//display debug info, note it displays lCelsius as 100
wxString debuginfo;
debuginfo << _T("deg C: ")  << lCelsius << _T("\n");
//displays incorrectly as 211
debuginfo << _T("deg F: ") << lFahrenheit << _T("\n");
//this displays 100
std::cout << lCelsius;
//this fails though
assert(lCelsius == 100);

Now with debug information, the lCelcius 100 is as expected, but it returns Fahrenheit as 211 instead of 212! It is strange that this formula works fine in pure C, and when I replace lCelsiuswith 100, it works fine, although my debugging information clearly says that it is 100.

Do you see any obvious problem, or I just can't do such a simple thing? I'm not sure what Wx does to make it smaller than necessary.

EDIT: assert.h lCelsius == 100 , std:: cout lCelsius 100. - Wx, , "100"..

+3
2

1.8 ( 9/5) - , (1.1100110011001100110011001100...) - , 1/3 .

1,8 - 1,7999999523). 100, 180; 32 , 212.

, 211.999... 211.

, , 100 , , , (9.f/5.f) * 100 180.

C99 roundf() ( math.h), :

long lFahrenheit = roundf((9.f/5.f) * lCelsius + 32);
+3

, , .

, , (9.f / 5.f), .

, long , , ( ) , int.

+1

Source: https://habr.com/ru/post/1773802/


All Articles