What you should do to transfer the data (via serial or internet) is to transfer the exact bytes of the data, rather than trying to convert them to something else. So take the address of the number, cast that to an unsigned char* and send the four bytes thus pointed at. This will ensure that your receiver will get the exact data that you send.
That is not correct. The problem occurs when assigning 0.04 because the binary representation of floating point values can be slightly inexact. In your case flTest will be set to the value 0.0399999991. The following multiplication by 100 will not add another error so that flResult becomes 3.99999991.
Because there is no rounding when casting floating point values to integers, the result is 3. To avoid this, you can add 0.5 before casting:
ucResult = (unsigned char)(flResult + 0.5f);
Note that the above is for positive numbers only. With negative numbers, 0.5 must be subtracted.
First of all, apologies for not replying earlier !
WOW, I didn't think that a few lines of code could provoke so much discussion !!!
The technique that I am trying to employ is between a PC and an Arduino (via Com port - actually USB). I used it a few years ago to transfer floating point numbers, but in the OPPOSITE direction and it worked perfectly.
Anyway, thank you all for your comments - I am amazed that the "error" is actually in the assignment as pointed out by Jochen. In this particular application, the floating point numbers will be quite small, so I think that I'll have to go for the 4 byte transfer method to maintain accuracy - but I'll be doing a lot of testing !! Thank you all once again !
That's assuming that you have similar processors at both ends of the connection. While big-endian systems are rare these days, they're not non existent. Furthermore, be careful when going between 32-bit and 64-bit systems. On my x86 linux boxes, a long is 4 bytes, whereas on a x86_64 they're 8 bytes, x86 long-double is 12 bytes and x86_64 its 16.
I have a combobox in a mfc application. I created it at runtime with following code -
if (!m_sortBox.Create(WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST, rect, this, eSortBox))
Now the requirements, if user is selecting an item from combobox like , if combo box have focus & user is pressing up/down arrow key, it should NOT update the data.
If user is pressing enter key after selecting a item, it should update data.
So i didn't handled OnSelchange here.
For enter key requirement, I checked enter key event in preTranslateMsg & checking if combo box have focus, it should trigger the function, who will eventually update the data.
if (pMsg->message == WM_CHAR && pMsg->wParam == VK_RETURN)
CWnd pActiveWnd = CWnd::FromHandle(GetFocus()->GetSafeHwnd());
CWnd pcbSortBoxWnd = CWnd::FromHandle(m_sortBox.GetSafeHwnd());
//If sort combo box has focus and user press Enter key, it should trigger OnComboSelChange event
//Eventually it will update the data.
if (pcbSortBoxWnd == pActiveWnd)
I also handled ON_CBN_CLOSEUP(eSortBox, OnSortChange)
So that mouse functionality will work(because with mouse, data should get update)
Now my logic is working but its crashing in some cases.
Like - If I press Alt + Down arrow key, which will expand combobox, I select an item(with help of arrow keys) and press enter.
It must be "declared" as extern in the header file one time and "defined" one time as you said in the main.cpp.
It must not be defined again at any other file (source.cpp) unless extern is added
The dafault value must be placed in the main.cpp as you wrote
I do not like the solution very much because I have to be careful when changing the name of the variable to do in both sides.
It can be confusing if encountered for the first time, but if this is the agreed idiom in your team, it ensures that all definitions occur only once. This in turn eliminates all possibilities of mismatches between declarations and definitions.
If you have an important point to make, don't try to be subtle or clever. Use a pile driver. Hit the point once. Then come back and hit it again. Then hit it a third time - a tremendous whack.
The basis for the error is that building an EXE constitutes compiling and linking.
Compilation - Every CPP file, also called a translation unit or compilation unit, is compiled separately to form OBJ files.
Linking - All the OBJ files are linked together to become the EXE.
In your case, during compilation, every OBJ file will contain a symbol called int variable.
This means the compilation of all units are successful.
But when the linker takes all the OBJ files to create an EXE, it finds the same symbol in all translation units and so it becomes ambiguous to it as to which symbol to use.
Having said this, there are two things that you may want to do with global variables.
You may want to use a global variable that is shared between all translation units.
This is the answer that you've gotten so far.
The global variable declaration must happen only once so that the linker only finds one symbol for the variable and links all other extern references to that symbol.
You may want to use the same name for the global variable in each translation unit.
For this to happen, declare the global variable as static - static int variable;
This forces the compilation process to not share the symbol among other translation units.