# Thread: Question on program to display a table of sine and cosine value between (0,1)

1. ## Question on program to display a table of sine and cosine value between (0,1)

Hello, I am a new learner on c program. I am writing a program to
display a table of sine and cosine value between (0,1)

the program is like this:

Code:
```#include <stdio.h>
#include <math.h>
int main()
{

double interval, i = 0;
printf("Enter the desired interval (eg. 0.1):\n");
scanf("%lf", &interval);
for (; i <= 1; i += interval) {
printf("sin(%f)=%f\n", i, sin(i));
printf("cos(%f)=%f\n", i, cos(i));

}
return 0;
}```
When I key in 0.1 interval, it display the values from 0 to 1. however, when I key in 0.01, it only display the values from 0 to 0.99.

I don't get it. Can any one explain to me ? thank you so much. 2. > When I key in 0.1 interval, it display the values from 0 to 1. however, when I key in 0.01, it only display the values from 0 to 0.99.
All floating point operations are approximations.
0.01 added together 100 times isn't necessarily the same as 0.1 added together 10 times.

Code:
```#include <stdio.h>
#include <math.h>
int main()
{

double interval, i = 0;
printf("Enter the desired interval (eg. 0.1):\n");
scanf("%lf", &interval);
for (; i <= 1; i += interval) {
printf("sin(%.15f)=%f\n", i, sin(i));
printf("cos(%.15f)=%f\n", i, cos(i));
}
return 0;
}``` 3. Code:
```#include <stdio.h>
#include <math.h>

int main() {

double interval, loops, i, j;

printf("Enter the desired interval (eg. 0.1):\n");
scanf("%lf", &interval);

for (loops=(1/interval);j<=(loops);i=(interval*++j)) {

printf("sin(%f)=%d\n", i, sin(i));
printf("cos(%f)=%d\n", i, cos(i));

}

return 0;
}```
Can any one explain to me ?
One over three multiplied by three is infinitely less than one.
x = 1/3; x * 3=0.9999999... 4. Originally Posted by Structure Code:
```#include <stdio.h>
#include <math.h>

int main() {

double interval, loops, i, j;

printf("Enter the desired interval (eg. 0.1):\n");
scanf("%lf", &interval);

for (loops=(1/interval);j<=(loops);i=(interval*++j)) {

printf("sin(%f)=%d\n", i, sin(i));
printf("cos(%f)=%d\n", i, cos(i));

}

return 0;
}```
One over three multiplied by three is infinitely less than one.

That's mathematically incorrect though (i.e., 0.999... repeating is in fact mathematically equal to 1), so Salem's explanation is better. Maybe you wanted to use truncation of 1/3 in decimal to demonstrate how such inaccuracy could occur in a decimal representation? 5. Binary scaling - Wikipedia

i.e., 0.999... repeating is in fact mathematically equal to 1
truncation of 1/3 in decimal
Code:
```:> try 0.3
sin(0.000000)=3944448
cos(0.000000)=0
sin(0.300000)=-1782924749
cos(0.300000)=-735114822
sin(0.600000)=396706137
cos(0.600000)=1011374869
sin(0.900000)=-1738692544
cos(0.900000)=-1768758755

:> try 0.333333333333333
sin(0.000000)=2428928
cos(0.000000)=0
sin(0.333333)=109740230
cos(0.333333)=1502713901
sin(0.666667)=2015401829
cos(0.666667)=174860566
sin(1.000000)=-1895232274
cos(1.000000)=263521932```
try.c :
Code:
```#include <math.h>
#include <stdio.h>
#include <stdlib.h>

int main(int args, char *argv[]) {

double interval = 0.0, loops, i, j;

interval = atof(argv);

for (loops=(1/interval); j<=loops; i=(interval*++j)) {
printf("sin(%f)=%d\n", i, sin(i));
printf("cos(%f)=%d\n", i, cos(i));
}

return 0;
}```
Repeating decimal - Wikipedia
Small-angle approximation - Wikipedia 6. Structure: what are you trying to illustrate? 7. "... where speed and extra accuracy are required, binary scaling works on simpler hardware and is more accurate."
Binary scaling - Wikipedia

"... the result is always a valid angle in the range of −180 degrees (−π radians) to +180 degrees ( radians)."
Euclidean space - Wikipedia | Rotation matrix - Wikipedia 8. Uh, I hate to say this, but it looks like you're posting irrelevant Wikipedia articles to bluff your way through having made an incorrect statement in post #3 that one of the articles you shared contradicts (the Wikipedia article on Repeating decimal). If you're trying to reply to cball's original post from a different perspective, then you really should provide a prose explanation of what you're trying to illustrate. 9. ... what are you trying to illustrate? Originally Posted by cball When I key in 0.1 interval, it display the values from 0 to 1. however, when I key in 0.01, it only display the values from 0 to 0.99. I don't get it. Can any one explain to me ? thank you so much.
... 0.01 added together 100 times isn't necessarily the same as 0.1 added together 10 times.
... this explanation ...  Originally Posted by cball 0.01, it only display the values from 0 to 0.99
try.c
Code:
```#include <math.h>
#include <stdio.h>
#include <stdlib.h>

int main(int args, char *argv[]) {

double interval = 0.0, loops, i, j;
interval = atof(argv);

for (loops=(1/interval); j<=loops; i=(interval*++j)) {
printf("sin(%f)=%d\n", i, sin(i));
printf("cos(%f)=%d\n", i, cos(i));
}

return 0;
}```
gcc try.c -o try.exe
Code:
```:> try 0.01
sin(0.000000)=3760128
cos(0.000000)=0
sin(0.010000)=-384440313
cos(0.010000)=615356330
sin(0.020000)=-844801945
cos(0.020000)=-1788506730
sin(0.030000)=-241801274
cos(0.030000)=1513446646
etc...
sin(1.000000)=-1895232274
cos(1.000000)=263521932``` 10. No wonder you're seeing garbage....
Code:
```\$ gcc -Wall -Wextra -O2 foo.c
foo.c: In function ‘main’:
foo.c:11:12: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘double’ [-Wformat=]
printf("sin(%f)=%d\n", i, sin(i));
^
foo.c:12:12: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘double’ [-Wformat=]
printf("cos(%f)=%d\n", i, cos(i));
^
foo.c:5:14: warning: unused parameter ‘args’ [-Wunused-parameter]
int main(int args, char *argv[]) {
^
foo.c:10:39: warning: ‘j’ may be used uninitialized in this function [-Wmaybe-uninitialized]
for (loops=(1/interval); j<=loops; i=(interval*++j)) {
^
foo.c:7:33: warning: ‘i’ may be used uninitialized in this function [-Wmaybe-uninitialized]
double interval = 0.0, loops, i, j;
^```
Bad format strings and uninitialised variables all over the place. 11. As said here before, floating point values are (often) approximations. Furthermore, they are binary representations of rational values (ℚ domain), not real values (ℝ domain). A decimal value, represented in double precision follows the equation: Where 's', 'f' and 'e' are unsigned integers with 1, 52 and 11 bits, respectively.

Take 0.1 (decimal) as an example. In binary it is writen as 0.000110011001100... ad infinitum. It doesn't have an exact representation. The nearest value is given by Or 0.100000000000000005551115123125782702118158340454 1015625

So... 10*0.1 isn't the same as 1.0!

Most values cannot be represented exactly using floating point (examples of approximations with 1 decimal digit: 0.1, 0.2, 0.3, 0.4, 0.6, 0.7, 0.8 and 0.9 aren't exact!). 0.5 (which is 2⁻¹) is! 12. ## Code:
```#include <math.h>
#include <stdio.h>
#include <stdlib.h>

int main(int args, char *argv[]) {

double interval = 0.0, loops = 0.0;
float i = 0.0, j = 0.0, c = 0.0;

if (args > 1) { interval = atof(argv); }
else { interval = atof("0.3333333"); }

for (loops=(1/interval); j<=loops; i=(interval*++j)) { c = i ;
printf("sin(%0.4f)\t|%f\ncos(%0.4f)\t|%f\n",i,sin(i),i,cos(i));
}

if ( (int)((1-c)*10) < 1 ) { printf("tan(%0.4f)\t|%f",c,tan(c) ); }
else { printf("tan(%0.4f)\t|%f",(c+interval),tan((c+interval)) ); }

return 0;
}``` Popular pages Recent additions #include, #sin cos program, cosine, interval, program 