CS240 Lab03 Factorial Calculation
Lab Exercise:
In this lab, we'd like you to explore the capacity and precision difference among three basic data types: int, float, double.
Write a program that reads a positive real number n from user and print out the factorial result n! in three versions: integer version, float version and double version.
Generally, n! has mathematical meaning only when n is an
integer. In this lab, we extend the notation of n! to positive numbers:
n!=n*(n-1)*(n-2)*(n-3)*...*(n-p) where 1=<n-p<2. Any number between (0,1)
has the factorial result of 1.
For example,
4.5!=4.5*3.5*2.5*1.5=59.0625
6!=6*5*4*3*2*1=720
0.31!=1
The input can be any positive real numbers such as 0.451, 5, 20.51. You can assume the input is always valid. Only the use of getchar() is allowed to read the input. Using other functions such as atof(), scanf() will result in an automatic zero. You have to convert the characters manually to a double for further calculation.
After reading the real number, three versions of factorial
function are to be called to calculate the results. The prototypes of three
functions are:
int fac_int(int p)
float fac_float(float p) double
fac_double(double p)
You have to implement these three functions. Pay attention that any
intermediate results in the functions above should have the same type as the
argument. For example, all the intermediate results in
fac_float should be float.
No overflow problem has to be considered.
After calculations, print out the results in 3 lines (integer result, float result and double result). The float result and double result should be printed out in the precision of the sixth digit after the decimal point.
You can download the skeleton of the code here.
Sample Input and Output:
lore 15 % lab03.out
5
Int: 120
Float: 120.000000
Double: 120.000000
lore 16 % lab03.out
4.5
Int: 24
Float: 59.062500
Double: 59.062500
lore 17 % lab03.out
7.4152
Int: 5040
Float: 13276.550781
Double: 13276.543216
lore 18 % lab03.out
15
Int: 2004310016
Float: 1307674279936.000000
Double: 1307674368000.000000
lore 19 % lab03.out
20
Int: -2102132736
Float: 2432902298041581568.000000
Double: 2432902008176640000.000000
lore 20 % lab03.out
50.2
Int: 0
Float: Inf
Double: 72607892116462395120975832400058450157483579696933402156340346880.000000
When you are satisfied that your program works correctly (or you run out of time), please do the following.
turnin -c cs240=XXXX -p lab03 lab03
where XXXX represents your lab section number.
The turnin section is as follows:
| Section | Time | TA |
| 0201 | Thursday 15:30-17:20 | Dan Zhang |
| 0301 | Friday 09:30-11:20 | Suli Xi |
| 0401 | Friday 13:30-15:20 | Youhan Fang |
| 0501 | Thursday 09:30-11:20 | J. C. Chin |
Make sure turnin reports that your project was submitted for grading. You can check the files you have submitted by running the following command:
turnin -c cs240=XXXX -v -p lab03
| 2 points | A working Makefile is provided. |
| 6 points | Program gets user input correctly. |
| 3 points | Implement Int version correctly. |
| 3 points | Implement Float version correctly. |
| 3 points | Implement Double version correctly. |
| 3 points | Print out the result correctly. |