factorial
Factorial of input
Syntax
Description
f = factorial( returns
the product of all positive integers less than or equal to n)n,
where n is a nonnegative integer value. If n is
an array, then f contains the factorial of each
value of n. The data type and size of f is
the same as that of n.
The factorial of n is commonly written in
math notation using the exclamation point character as n!.
Note that n! is not a valid MATLAB® syntax
for calculating the factorial of n.
Examples
Input Arguments
Tips
Limitations
For double-precision inputs, the result is exact when
nis less than or equal to21. Larger values ofnproduce a result that has the correct order of magnitude and is accurate for the first 15 digits. This is because double-precision numbers are only accurate up to 15 digits.For single-precision inputs, the result is exact when
nis less than or equal to13. Larger values ofnproduce a result that has the correct order of magnitude and is accurate for the first 8 digits. This is because single-precision numbers are only accurate up to 8 digits.
Saturation
The table below describes the saturation behavior of each data type when used with the
factorialfunction. The values in the last column indicate the saturation point; that is, the first positive integer whose actual factorial is larger than the maximum representable value in the middle column. Forsingleanddouble, all values larger than the maximum value are returned asInf. For the integer data types, the saturation value is equal to the maximum value in the middle column.Data type Maximum Value Factorial Saturation Threshold doublerealmaxfactorial(171)singlerealmax('single')factorial(single(35))uint64264-1 factorial(uint64(21))int64263-1 factorial(int64(21))uint32232-1 factorial(uint32(13))int32231-1 factorial(int32(13))uint16216-1 factorial(uint16(9))int16215-1 factorial(int16(8))uint828-1 factorial(uint8(6))int827-1 factorial(int8(6))
Extended Capabilities
Version History
Introduced before R2006a