|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
overflow detection without try-catchI want to detect if multiplying two integers or two longs will cause an
overflow without using a try-catch block. I know how to do it with try-catch, but the performance hit is too much. I want an efficient test so I can program like if CausesOverflow(x,y) then ' do one thing else ' do something else endif I am using VB and FW 1.1. I'll be upgrading from 1.1 soon.
Show quote
Hide quote
"AMercer" <AMer***@discussions.microsoft.com> schrieb: Pseudo code:>I want to detect if multiplying two integers or two longs will cause an > overflow without using a try-catch block. I know how to do it with > try-catch, but the performance hit is too much. I want an efficient test > so > I can program like > > if CausesOverflow(x,y) then > ' do one thing > else > ' do something else > endif > > I am using VB and FW 1.1. I'll be upgrading from 1.1 soon. \\\ If y > Double.MaxValue / x Then MsgBox("Overflow") End If /// -- M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://dotnet.mvps.org/dotnet/faqs/> > If y > Double.MaxValue / x Then Thanks Herfried, very sharp. I've adapted it to long and integer which is > MsgBox("Overflow") > End If my problem area. Thanks also to Andrew Morton, but Herfried's approach seems better. AMercer wrote:
>> If y > Double.MaxValue / x Then Don't forget you also have to check for x > Double.MaxValue / y and also for>> MsgBox("Overflow") >> End If > > Thanks Herfried, very sharp. I've adapted it to long and integer > which is my problem area. Thanks also to Andrew Morton, but > Herfried's approach seems better. the case where either x or y (but not both) is negative and then reverse the inequality check. Or use the Math.Abs() values. And check for zero before attempting to divide by it. Andrew AMercer wrote:
> I want to detect if multiplying two integers or two longs will cause if log(a)+log(b)>precalculatedLogOfBiggestNumber then> an overflow without using a try-catch block. I know how to do it with > try-catch, but the performance hit is too much. I want an efficient > test so ' too big else ' OK end if As for efficiency, you'll have to test it. Andrew Andrew Morton wrote:
> AMercer wrote: Oops!>> I want to detect if multiplying two integers or two longs will cause >> an overflow without using a try-catch block. I know how to do it >> with try-catch, but the performance hit is too much. I want an >> efficient test so > > if log(a)+log(b)>precalculatedLogOfBiggestNumber then > ' too big > else > ' OK > end if if log(abs(a))+log(abs(b))>precalculatedLogOfBiggestNumber then ' too big else ' OK end if Andrew
How can I create a Shortcut (with parameters) on a user-desktop with vb.net?
how to run the execuatable before windows shutting down Redirecting standard output Posting login data with HttpWebRequest VB Program - CHM help integration Permanent Rectangle on Form. Getting nodes from an XML-document How to re-start application? Button with dropdown style like on a toolbar Use VB.Net to write to Save Excel File |
|||||||||||||||||||||||