I'm very new to C programming. I have to print the values of tan(0) to tan(pi/2) in steps of pi/20 for both single and double precision floats. However, when I use different data types to store the floats, nothing changes between single and double, and I expected the number of digits to change.
#include <stdio.h>
#include <math.h>
#define PI 3.1415926
int main()
{
float angle = 0.0;
float pi_single = 3.1415926;
printf("single precision:\n");
while(angle < pi_single/2){
float(tanangle) = 0.0;
tanangle = tan(angle);
printf("tan(%f) = %f\n", angle, tanangle);
angle = angle + pi_single/20;
}
double angle2 = 0.0;
double pi_double = 3.141592653589793;
printf("double precision:\n");
while(angle2 < pi_double/2 ){
double(tanangle2) = 0.0;
tanangle2 = tan(angle2);
printf("tan(%lf) = %lf\n", angle2, tanangle2);
angle2 = angle2 + pi_double/20;
}
return 0;
}
I'm trying to replicate the result of this Python program:
import numpy as np
theta_32 = np.arange(0, np.pi/2+np.pi/20, np.pi/20, dtype = 'float32')
print('single precision')
for theta in theta_32:
print(theta)
print(np.tan(theta))
print()
[enter image description here][1]
theta_64 = np.arange(0, np.pi/2+np.pi/20, np.pi/20, dtype = 'float64')
print('double precision')
for theta_new in theta_64:
print(theta_new)
print(np.tan(theta_new))