|
I don't think there is a way to ensure precision on the way the value is stored, unless the desired precision is small enough to convert it to an integer (so you would store 2.5 as 250 and know that you have to divide by 100 to get the actaul value), but you can implement the desired precision in your code checking, assuming the variance in the way the value is stored is less than the desired precision.
For example, if you are testing monetary values and you only need to be precise to 0.01 then equality is checked by something like fabs(a-b)<0.01 which is roughly equivalent to a==b. When checking for limits and you would ideally want, for example a<=100, just use a<100.01, etc.
|
|
|
|
|
Hi,
if you want to round up to a precision, try this one:
// Rounds a double to the nearest multiple of the passed seed.
double round(double Num, double Seed)
{
// get rational factor
double fac = Num / Seed;
// get natural factor
fac = (fac < 0) ? (static_cast<int>(fac - 0.5)) : (static_cast<int>(fac + 0.5));
// return multiple
return fac * Seed;
}
|
|
|
|
|
reason:
the FPU internally uses a higher precision. The "last digits" depend on how long the optimizer keeps a value in the FPU before writing it back to memory.
Builds are not even guaranteed to be "stable", i.e. the code generated may differ from build to build.
Always use an eps comparison ( fabs(a-b) < eps, where eps must be sufficiently small compared to the OOM of a and b)
Pandoras Gift #44: Hope. The one that keeps you on suffering. aber.. "Wie gesagt, der Scheiss is' Therapie" boost your code || Fold With Us! || sighist | doxygen
|
|
|
|
|
I'm trying to debug a problem where multiple applications (exe's) are loading a common dll. I have some debug output in the InitInstance() and ExitInstance() of the dll's CWinApp class. I'd really like to know which app is loading/unloading for each line of debug output (I'm writing lines to a file) but have not found a way to do this. The m_pszExeName and m_pszAppName variables are always the name of the dll - so that's no help. The m_lpCmdLine is always blank so that's no help either.
Is there a way to do this?
|
|
|
|
|
I don't know if that is feasible, it depends on the complexity of your project, but you could add the executable's name as an extra variable to some key calls to the dll and have the executables just provide their name to the dll for logging/debugging purposes.
|
|
|
|
|
This is not possible to found from the dll itself.
Dll's are loaded (mapped in process address space) by the loader,
so there could be a place to found which process load the dll.
But I can't provide more help for you, my knowledge is limited
to what I said.
|
|
|
|
|
Bummer. Unfortunately, not all of the app's that call my dll are written by my company. I guess I can add something to detect my app but the rest will have to remain unidentified.
Thanks anyway.
|
|
|
|
|
lynchspawn wrote:
I can add something to detect my app but the rest will have to remain unidentified.
Call the GetModuleFileName in your Code!
"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
|
|
|
|
|
::GetModuleFileName(GetModuleHandle(NULL), ...);
...cmk
Save the whales - collect the whole set
|
|
|
|
|
Even though MFC's DllMain() is calling your app's InitInstance() method, can you not use GetProcess() or GetProcessId() ?
"One must learn from the bite of the fire to leave it alone." - Native American Proverb
|
|
|
|
|
Hi
I am trying to learn MFC and Visual C++ while working on a project. I want to do a simple task in a function.
It is when the function is called I need to spawn a separate thread, pop up a message box in that thread and when the user presses OK or Cancel in that message Box, kill the spawned thread and pass control back to the main thread.
I am clueless how to do this. A simple code example will really help.
Thanks a Ton..
|
|
|
|
|
I could mail you a simple examlpe.
It popups message dialogs after a specified time span. Abortion is also included...
|
|
|
|
|
Here is a useful link: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmfc98/html/_mfc_afxbeginthread.asp
Try something like this to get you started:
INT MyThread( LPVOID ptr );
int main( int argc, char** argv )
{
CWinThread* T;
DWORD dwWait;
T = AfxBeginThread( MyThread, NULL );
dwWait = WaitForSingleObject( T->m_hThread, INFINITE );
return 0;
}
/* main() */
INT MyThread( LPVOID ptr )
{
AfxMessageBox( "Hello, world!" );
return 0;
}
|
|
|
|
|
create a worker thread..
UINT ClassName ::Thread1(LPVOID lp)
{
ClassName *cn = (ClassName*)lp;
cn->Thread1();
}
//// define the functionality inside the thread
void ClassName::Thread1()
{
while(1)
{
AfxMessageBox("In thread");
Sleep(millisec);
}
}
in the function,
Anyfun()
{
CWinThread *cwt;
cwt=AfxBeginThread(Thread1,this);
}
you can suspend the thread by cwt->SuspendThread();
resume by cwt->ResumeThread();
ExitInstance() will delete it
Regards,
V
|
|
|
|
|
to have a better look on a Thread... click on [View Thread] Below!!
jus fr fun,
V............v
|
|
|
|
|
Vivekuniq wrote:
click on [View Thread] Below!!
I have Clicked [View Thread] and................... I got nothing............:->
"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
|
|
|
|
|
This[^] article may help. Specifically, the section titled "Responding to user input while performing a long task".
/ravi
My new year's resolution: 2048 x 1536
Home | Music | Articles | Freeware | Trips
ravib(at)ravib(dot)com
|
|
|
|
|
Thanks for all the help. I could accomplish what I wanted to do with all your help.
Thank Again
|
|
|
|
|
The function call in the bold line always returns 0.
Isn't StretchBlt() able to transfer data between two memory DCs ?
CDC* ZoomBitmap(CBitmap* pBitmap, CWnd* pWindow)
{
// get source size
CSize srcSize = doGetSize(pBitmap);
// get destination size (client rect)
CSize dstSize = doGetZoomedSize(pBitmap, pWindow);
// create memory device contexts
CDC* pDC = pWindow->GetDC();
CDC* mem1 = new CDC; mem1->CreateCompatibleDC(pDC);
CDC* mem2 = new CDC; mem2->CreateCompatibleDC(pDC);
pWindow->ReleaseDC(pDC);
// select bitmap in original size
CBitmap* old = mem1->SelectObject(pBitmap);
// stretch bitmap
mem2->SetStretchBltMode(HALFTONE);
mem2->StretchBlt(0, 0, dstSize.cx, dstSize.cy, mem1, 0, 0, srcSize.cx, srcSize.cy, SRCCOPY);
// reselect old bitmap
mem1->SelectObject(old);
// delete temporary device context
delete mem1;
// use for future BitBlt() operations
return mem2;
}
|
|
|
|
|
|
Thank you Chris,
I added
CBitmap* test = new CBitmap;
test->CreateCompatibleBitmap(mem2, dstSize.cx, dstSize.cy);
mem2->SelectObject(test);
and it seems to work.
I still have to fix some color stuff but this is finally the starting point I've been searching for.
Thanks a lot.
|
|
|
|
|
1.)I have a data source that is continously modified, i.e items added, deleted, changed. I used a static global mutex object to actually allow only one thread to modify it. Now I have a list ctrl that shows that data, the refreshing is done in a different thread. Now in that thread:
DeleteAllItems();
for(unsigned int i=0; i<m_aryCall.Count(); i++)
{
..................................................
}
No what if I have a list of 10 elements and I deleted some of them when this thread is running. As the compiler will store the value of m_aryCall.Count() in some temporary variable and when its asks for 10th element, which is not there.
2.)In Modify Data Block a create a thread by new and run it, now the threads are running and the ModifyData is called many times when the previous threads is running. This actually creates memory leak as the thread is not deleting itself.
http://www.priyank.in/
|
|
|
|
|
I am not sure I understand 100% of what you are trying to do, but it seems to me that you have a mumber of threads modifying the data (and using a Global Mutex to make sure they do it one at a time) and a number of threads reading the data.
If that is the case just use the same Mutex for the reading threads also. That way your data is not being modified as another thread is tryting to read it.
|
|
|
|
|
Priyank Bolia wrote:
I used a static global mutex object to actually allow only one thread to modify it
The problem is, you like to directly modify the control from different threads...
You should only modify controls from the main thread (or the thread which created the control)... When you like to directly modify it from an other thread, it comes to unexpected situations like memory leaks or a deadlock of the app...
You should only modify the controls from different threads with the usage of PostMessage...
Communicate from the worker thread(s) with the main thread only with PostMessage and custom defined user messages to provide an save ui access...
like: http://www.codeproject.com/threads/usingworkerthreads.asp
Best regards
|
|
|
|
|
whatz the use of creating static methods??
class x
{
public :
int x;
x()
{
x=10;
}
static void mthod1()
{
cout<<x;
}
="" static="" void="" mthod2()
="" {
="" cout<<x;
="" }
}
void="" main()
{
="" x="" obj;
obj.mthod1();;---="">10
obj.mthod2();---->10
}
both r same..
what'd be the difference?? why its declared ststic?
|
|
|
|