Re: Factorial

From: Wes Spikes (MornThdr_at_NOSPAMverizon.net)
Date: 05/22/04


Date: Sat, 22 May 2004 21:00:21 GMT

Ok, I'm trying to use it now (sorry for not trying it earlier --I'm a little
absent-minded), but x is undefined (I've made it a practice to use Option
Explicit), so I'm going to try and use it as an integer!

-Wes

"Rick Rothstein" <rickNOSPAMnews@NOSPAMcomcast.net> wrote in message
news:e8B#iptPEHA.2704@TK2MSFTNGP10.phx.gbl...
> > Oops, to avoid overflow, do this instead:
> >
> > Public Function Factorial(x As Integer) As Double
> > Factorial = 1
> > For I = 0 To x - 1
> > Factorial = Factorial * (x - I)
> > Next I
> > End Function
>
> Perhaps this combination of two posts I given in the past may be of some
> help or interest...
>
> Rick - MVP
>
> You could cast (you can't Dim) a Variant variable as a Decimal type
> (96-bit number) and get some 28 or 29 digits of accuracy depending if
> there is a decimal in the answer or not. Simply Dim a variable as
> Variant and CDec a number (any number will do) into it to make it the
> Decimal type. Thereafter, that variable will track 28/29 digits of
> accuracy. For example the following function will calculate factorials
> up to 29 digits of display before reverting to exponential display.
>
> Function BigFactorial(ByVal N As Integer) As Variant
> If N < 28 Then
> BigFactorial = CDec(1)
> Else
> BigFactorial = CDbl(1)
> End If
> For x = 1 To N
> BigFactorial = x * BigFactorial
> Next
> End Function
>
> However, you have to watch out for overflows with Decimal data types --
> once over 28/29 characters, they will produce an overflow error. So, if
> you tried to use the above function like this
>
> Debug.Print 10*BigFactorial(27)
>
> you would get an overflow error but
>
> Debug.Print 10*BigFactorial(28)
>
> would work fine (the difference being in the first case BigFactorial has
> a Decimal subtype and in the second case the subtype is a Double).
>
> More generally, if a Variant variable is assigned a value that was cast
> to Decimal, any calculation involving that variable will be "performed"
> as a Decimal; and then the result cast back to the variable receiving
> it. If the result is assigned back to the variable that was originally
> cast to Decimal, that variable continues to contain a Decimal type
> value. For example,
>
> X = CDec(135.6)
> X = X - 135
> X = X / 7
> Print X ==> 0.0857142857142857142857142857
>
> You have to be careful with this though . . . all VB functions return
> non-Decimal data.and assigning *that* back to the Variant that was cast
> as Decimal "collapses" it back to a less robust data type. For example,
> continuing the code above
>
> X = Sqr(X)
> Print X ==> 0.29277002188456
>



Relevant Pages

  • Re: 3GB address space
    ... I experimented a bit with integer arithmetic, intentionally causing an ... but the results seemed ok and there were no overflow error ... What should you guard against to make sure that your program is ... They probably cast ...
    (borland.public.delphi.language.basm)
  • Re: numeric type conversion question
    ... let me add another cast to list of cast examples i wanted to verify ... >I converted most of it to use decimal type to represent money. ... >also should i cast the int? ... >boxed objects do not allow casting to another type. ...
    (microsoft.public.dotnet.framework)
  • Re: Math.Sqrt() and Decimal
    ... You will probably have to cast the Decimal type to a double, ... > How can I use Math.Sqrtwith Decimal data? ... Prev by Date: ...
    (microsoft.public.dotnet.languages.csharp)