|
This is the same with any data type, when 1 large number is added to another and the result is larger than the data type can hold, it wraps around. i.e.
ULONG size1 = 0xFFFFFF00;<br />
ULONG size2 = 0xFFFFFF00;<br />
size1 += size2;
The output is less than the input. I want to check for this problem and if it's there I would prefer the output to be the max value the data type can hold. What is the best way to approach this?
|
|
|
|
|
waldermort wrote: ULONG size1 = 0xFFFFFF00;
ULONG size2 = 0xFFFFFF00;
if you divede both size1 and size by a big number that make you sure that you can add and you still in the range of long.
size1 = size1/n;
size2 = size2/n;
waldermort wrote: size1 += size2; // == 0xFFFFFE00
adding in this line will not make a problem
then finally, when you want to output the final value , make the value the program do it time the number you devide by it:
ex: textbox1.text = Tostring(size2 * n) // n is the number you devided by it.
I hope it work properly with you.
regards;
** Ahmed Ismail **
|
|
|
|
|
That doesn't work, it produces exactly the same result as simply adding them:
ULONG max1 = 0xFFFFFF00;<br />
ULONG max2 = 0xFFFFFF00;<br />
max1 = max1/4;<br />
max2 = max2/4;<br />
max2 += max1;<br />
max2 *= 4;
The only way I have found is to cast them to a larger data type and check the value using an if else block
if ( ((UINT64)max1 + (UINT64)max2) > 0xFFFFFFFF )<br />
max2 = 0xFFFFFFFF;<br />
else<br />
max2 += max1;
But I'm sure there must be a more accurate way of doing it.
|
|
|
|
|
Ahmed Ismail Mohamed wrote: i think that you should make max2 *4 only when you output the resualt.
What you are saying makes no sense. Why divide and add when it is taking the result I want further away.
|
|
|
|
|
Hello
Well, if you have some assembly background you'd remember that there is a bit in the status register called overflow bit to check for this error. Anyway, I know how to check for overflow in Assembly and C#, but sorry, my C++ is too rusty to remember how to do it in C++ .
You are right, there is a better way to detect overflow, yet I will provide you with a simpler way of detecting it. Usualy the overflow result is smaller than any of the two operands. So, try this:
ULONG size1 = 0xFFFFFF00;
ULONG size2 = 0xFFFFFF00;
size1 += size2;
if( size1 < size2)
size1 = 0xFFFFFFFF;
Remember, maximum value of each type is totally OS dependant. Keep that in mind.
If someone got a better approach, please post. I'll try to remember how to check overflow in C++. If I got anything, I'll keep you posted.
Regards
|
|
|
|
|
That probably is a better way of doing it. Thinking about it, the output would always be smaller. If you remember the better way, please post it. I would love to know.
|
|
|
|
|
use the header <LIMITS.h>
with this, you can know the types max and min values, and some other informations for floating points types.
|
|
|
|
|
You could do something like this :
_int64 sum = (_int64)size1 + (_int64)size2;
if( sum > ULONG_MAX )
TRACE( "an overflow occurred\n" );
else
size1 = (ULONG)sum;
|
|
|
|
|
Try this:
ULONG size1 = 0xFFFFFF00;
ULONG size2 = 0xFFFFFF00;
ULONG sizeR = size1 + size2;
if (sizeR < max(size1, size2))
{
}
Steve
|
|
|
|
|
try this:
<br />
if (MAXVAL - size1 < size2)<br />
{<br />
}<br />
else<br />
{<br />
size1 += size2;<br />
}<br />
MAXVAL can be obtained from limits.h as toxcct said.
----------------------
Mayank Thakore
Learning C++ - since 1998
They didn't print my card right; so I resigned.
|
|
|
|
|
Hi,
how can i convert CString to wchar_t*? for example
CString m_strFilename;
how can i convert m_strFilename to wchar_t*?
i use L"" for string such that L"c:\filename.cpp", but is
there any function or macro do this?
Best Regards.
MJM
|
|
|
|
|
MultiByteToWideChar() is the function you want, but why not just use CStringW ?
|
|
|
|
|
thanks , I use CStringW !
|
|
|
|
|
See
<br />
USES_CONVERSION;<br />
CString str;<br />
str="Test";<br />
<br />
wchar_t* m_wchar = T2W(str.GetBuffer());<br />
str.ReleaseBuffer();<br />
<br />
|
|
|
|
|
Which is almost exactly the same as what I told him. It's ok to use the conversion macros if you are doing a lot of converting, but for a single time it's pointless to add the extra header required.
|
|
|
|
|
|
Hi,
I have a client application in which i first do a Create() and then Connect() to connect to my server application.
The problem is that even if i dont start the server application, i get the Connect() return value as WSAEWOULDBLOCK and no other error value.
How do i determine correctly whether the server is actually available or not ? Am i doing anything wrong ?
Thanks in advance.
*** Who said nothing is impossible? I have been doing it for a long time ***
|
|
|
|
|
hi there
I'm a beginner in .net enviroment. I try to make a simple database application like this
http://www.functionx.com/vcnet/data...ridbinding3.htm
it's work's slowly. i'm waiting 5 seconds to make a simple select interogation. i have 3 entry's in that table
why that. what to do??
|
|
|
|
|
Did you see examples in codeproject about databse
|
|
|
|
|
Hi,
I wish to draw a horizontal line in a dialog to delimit two sets of controls. But i cannot find any such thing on the tools panel.
Can anyone please help me out with that ?
Thanks in advance.
*** Who said nothing is impossible? I have been doing it for a long time ***
|
|
|
|
|
What I do generally (but maybe it's not the cleanest solution) is to put a picture control and reduce its vertical size to the minimum. Then I open the property dialog (right click on the control -> property) and I modify the style to be sunken. Like that you have a nice line.
|
|
|
|
|
Thanks Cedric. that's a pretty nice trick.
Thanks a lot.
*** Who said nothing is impossible? I have been doing it for a long time ***
|
|
|
|
|
CoolASL wrote: draw a horizontal line in a dialog
You can also do this:
HDC hdc=::GetDC (m_hWnd);
::MoveToEx (hdc,10,10,NULL);
::LineTo (hdc,100,10);
::ReleaseDC (m_hWnd,hdc);
Somethings seem HARD to do, until we know how to do them.
_AnShUmAn_
|
|
|
|
|
Hi,
You better use group control
you can group the set of controls with this.
|
|
|
|
|
thanks man.
*** Who said nothing is impossible? I have been doing it for a long time ***
|
|
|
|