|
(int) (r+0.5f) is the way to go if you are rounding to the nearest integer with ?.5 always rounding up.
The second version avoids overflow problems. My question would be why would someone think adding 100000 would improve the rounding function. When dealing with floating point numbers, you really have to be careful when adding and subtracting values.
(NOTE: this only works for positive numbers, negative numbers must use -0.5f and not 0.5f)
Tim Smith
I'm going to patent thought. I have yet to see any prior art.
-- modified at 10:26 Friday 9th September, 2005
|
|
|
|
|
Oh dear god, I just realized what they are doing.
(int) (f) does a truncation of the floating point number. In otherwords, it rounds to zero.
2.5 -> 2
0.3 -> 0
-4.5 -> -4
The "(int) (r+0.5f)" trick only works for positive values.
2.3 -> 2
2.7 -> 3
-0.2 -> 0
-0.7 -> 0 (WRONG - should be -1)
By doing 100000.5f, they AVOID (not remove) the negative problem.
Example, but we use 10.5f instead of 100000.5f
-0.2 + 10.5f = 10.3f -> 10 -> 0
-0.7 + 10.5f = 9.8f -> 9 -> -1
But dear god, WHAT A HACK.
The proper way of doing it is:
int i = (int) (r + (r > 0.0f ? 0.5f : -0.5f));
This is why Christian wants people to use math.h because it is easy to create things that sort of work, but often they don't always work.
Tim Smith
I'm going to patent thought. I have yet to see any prior art.
|
|
|
|
|
please, somebody could publish a simple example on SNMP in C++?
Thanks!
Sorry for my english
|
|
|
|
|
Search CP articles for SNMP. There are several examples.
"One must learn from the bite of the fire to leave it alone." - Native American Proverb
|
|
|
|
|
sebastianos wrote:
somebody could publish a simple example on SNMP in C++?
here is list :-
SNMP ARTICLE[^]
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
|
|
|
|
|
Friends,
I want to develop a ActiveX control container,and save data in flat(uncompound) document.
My document CxxDoc is derived from COleDocument,and the constructor as following:
CxxDoc::CxxDoc()
{
// EnableCompoundFile();
// ^^^^^^^^^^^^^^^^^^^^^^^^^^I need a flat (uncompound) file.so i delete it.
...
}
Firstly, Is it OK????Can i save data in flat document?
Secondly,my client item is CxxClientItem,
class CxxClientItem : public COleClientItem
The WriteItem and ReadItem function (copied from the sample program of ActiceX control test container in Visual C++) as follows. They can work correctly in compound document. Please tell me how to change them from compound document to flat(uncompound) document. I'm a new and I really need your help.
void CxxClientItem::WriteItem( CArchive& ar )
{
USES_CONVERSION;
IStoragePtr pStorage;
IStreamPtr pStream;
IPersistStreamInitPtr pPSI;
TCHAR szItemName[64];
HRESULT hResult;
IPersistStoragePtr pPersistStorage;
CxxDoc* pDoc;
CLSID clsid;
(void)ar;
pDoc = GetDocument();
ASSERT_VALID( pDoc );
ASSERT( pDoc->m_lpRootStg != NULL );
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^It is wrong in uncompound document
ASSERT( pDoc->m_bCompoundFile );
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^It is wrong in uncompound document
ASSERT( m_lpStorage != NULL );
ASSERT( !ar.m_bForceFlat );
GetItemName( szItemName );
if( !pDoc->m_bSameAsLoad )
{
hResult = pDoc->m_lpRootStg->CreateStorage( T2COLE( szItemName ),STGM_CREATE|STGM_READWRITE|STGM_TRANSACTED|STGM_SHARE_EXCLUSIVE, 0, 0, &pStorage );
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^It is wrong in uncompound document
if( FAILED( hResult ) )
{
AfxThrowOleException( hResult );
}
m_lpNewStorage = pStorage;
m_lpNewStorage->AddRef();
m_bNeedCommit = TRUE;
}
else
{
pStorage = m_lpStorage;
}
......
}
void CxxClientItem::ReadItem( CArchive& ar )
{
USES_CONVERSION;
BOOL tUsedStream;
IStoragePtr pStorage;
IPersistStreamInitPtr pPSI;
IStreamPtr pStream;
TCHAR szItemName[64];
HRESULT hResult;
BOOL tQuickActivated;
IPersistStoragePtr pPersistStorage;
CLSID clsid;
(void)ar;
CxxDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
ASSERT(pDoc->m_lpRootStg != NULL);
// ^^^^^^^^^^^^^^^^^^^^^^^^^^It is wrong in uncompound document
ASSERT(pDoc->m_bCompoundFile);
// ^^^^^^^^^^^^^^^^^^^^^^^^^^It is wrong in uncompound document
ASSERT(m_lpStorage == NULL);
ASSERT(m_lpLockBytes == NULL);
ASSERT( !ar.m_bForceFlat );
// get item name
GetItemName( szItemName );
// open storage for this item
hResult = pDoc->m_lpRootStg->OpenStorage( T2COLE( szItemName ), NULL,
STGM_READWRITE|STGM_TRANSACTED|STGM_SHARE_EXCLUSIVE, 0, 0, &pStorage );
// ^^^^^^^^^^^^^^^^^^^^^^^^^^It is wrong in uncompound document
if( hResult != S_OK )
{
TRACE1( "Warning: unable to open child storage %s.\n", szItemName );
// upon failure throw file exception (item will be cleaned up)
AfxThrowOleException( hResult );
}
ASSERT(pStorage != NULL);
// remember the storage
m_lpStorage = pStorage;
ASSERT(m_lpStorage != NULL);
m_lpStorage->AddRef();
......
}
|
|
|
|
|
Hi folks,
how can I get the ascii code (0-255) from the wParam of the WM_KEYDOWN message (virtual-key code) ?
Thanks in advance,
Achim Klein
We can do no great things, only small things with great love. - Mother Theresa
|
|
|
|
|
According to MSN[^], that's all that's in there. So I expect you just need to cast the value to throw off the extra bits you don't need.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
Hi Christian,
thanks for your instant feedback.
I've already tried to copy the bits according to this article, but unfortunately it doesn't work.
For example:
- the the Num-Pad keys returns lowercase characters
- the '+' and '-' keys haven't the normal ascii code
- all alphabetical keys from the keyboard are uppercase characters
It's a bit confusing...
We can do no great things, only small things with great love. - Mother Theresa
|
|
|
|
|
Have you tried WM_CHAR ?
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
It's a dialog based application.
I've set a breakpoint in the WM_CHAR handler but it seems that the handler never gets called.
We can do no great things, only small things with great love. - Mother Theresa
|
|
|
|
|
I created a VC6 dialog project and put this in PreTranslateMessage:
if (pMsg->message == WM_KEYDOWN)
{
CString s;
s.Format("%d %c", pMsg->wParam, pMsg->wParam);
AfxMessageBox(s);
}
I got the same result as you. I replaced WM_KEYDOWN with WM_CHAR and got upper and lowercase and correct values for the keypad.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
Yeah it works !
Thanks a bomb !
It's 3:23 a.m here in germany, and now that it works I can finally go to bed.
Thanks a lot !
We can do no great things, only small things with great love. - Mother Theresa
|
|
|
|
|
No worries - glad to help
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
|
I check the WM_KEYDOWN message in the PreTranslateMessage() handler...
We can do no great things, only small things with great love. - Mother Theresa
|
|
|
|
|
|
WM_KEYDOWN does NOT return ASCII characters. It returns virtual-key codes for the keys which happen to be ASCII characters for some of the keys.
The TranslateMessage function looks for WM_KEYDOWN messages and if it can convert the data in that message to a character-code (ASCII codefor ASCII keyboards???) then it generates a WM_CHAR message.
http://blog.ngedit.com/2005/06/13/whats-broken-in-the-wm_keydownwm_char-input-model/[^]
Tim Smith
I'm going to patent thought. I have yet to see any prior art.
|
|
|
|
|
|
i really need help writing this program:
In this assignment, you will creating a simple interactive guessing game, which is based on random numbers. The game is simple: two dice are rolled; you need to predict whether the sum of the next roll will be higher or lower than the previous roll. For example, you may have the first die roll a 2 and the second die roll a 3. The sum of this roll is 5. Statistically speaking, you should "predict" higher because the average roll results in 6. If the roll is higher than or equal to 5 and you predicted it to be higher, then you get to continue. If the roll is lower (or equal to) and you predicted lower, you get to continue. Basically a wrong prediction will end the game.
Background: This program could potentially play forever (though it's doubtful), so obviously you need a loop of some kind - but choose carefully.
Requirements: Basically the program should behave as closely to the example below as possible; the grade breakdown is roughly as follows:
For a 'C': Get the program to roll the dice once, ask if the next roll will be higher or lower, roll the dice again and say if it was correct or not.
For a 'B': Get the program to repeatedly do this until they guess wrong
For an 'A': Get the program to track the number of times the user guessed correctly
can some1 plz plz help me
|
|
|
|
|
Shantise2003 wrote:
In this assignment, you will creating a simple interactive guessing game, which is based on random numbers.
I'm happy to do this for you, I'll have it done in about half an hour. I'll charge $20. When you get your next assignment, you'll have no idea how to do it, because it will build on the knowledge you should have build with this program. So, my price will double. By the time you graduate, you'll know nothing, and I'll have bought a house.
OR, you could start on this project yourself, with reference to your course notes and whatever books you have. Then, if you get stuck and ask specific questions here, I'll help you gladly, for free.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
|
Hi again. YOu emailed me as follows:
ok i have started the project on my own so after i put the basic skeleton i put (int userchoice = ??) i dont know what to put there so can you help me?
erm... I meant to do more than get the wizard to generate a skeleton. Why not post your code ? If you're yet to write any, look into cout and cin.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
let me throw the die
V
|
|
|
|
|
hi, all
the program I am working on returns a 7 digits value for cycle number, it should be 1-99, i am trying to find out where it gets this value, i set some breakpoints, it has the right value 02 at the beginning, at the end of the onPaint() function of dlgMain.cpp, it will go to next function void CMainWnd::OnOK(), the 1243994 as value show up, it looks like some sort of memory location to me, but seems like between these two function are all disassembly stuff, i couldn't really read them.....looking at call stack, still can't find it stop at which file to get this value either...
anybody has any idea? thanks for your time.
|
|
|
|