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

  1. #1
    Registered User
    Join Date
    Jul 2020
    Posts
    4

    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.
    Last edited by Salem; 07-12-2020 at 01:15 AM. Reason: Removed font train wreck

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 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;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338
    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...

    Last edited by Structure; 07-12-2020 at 12:23 PM.
    "without goto we would be wtf'd"

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Structure View Post
    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?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338
    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[1]);
        
      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
    Last edited by Structure; 07-12-2020 at 01:02 PM.
    "without goto we would be wtf'd"

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Structure: what are you trying to illustrate?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338
    "... 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
    Last edited by Structure; 07-12-2020 at 01:38 PM.
    "without goto we would be wtf'd"

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338
    ... what are you trying to illustrate?
    Quote Originally Posted by cball View Post
    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 ...

    Question on program to display a table of sine and cosine value between (0,1)-numbers-png

    Quote Originally Posted by cball View Post
    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[1]);
    
      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
    Last edited by Structure; 07-12-2020 at 02:12 PM.
    "without goto we would be wtf'd"

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    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:

    Question on program to display a table of sine and cosine value between (0,1)-png-latex-png

    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

    Question on program to display a table of sine and cosine value between (0,1)-png-latex2-png

    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. #12
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338

    Post

    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[1]); } 
        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;
    }
    Last edited by Structure; 07-12-2020 at 05:00 PM.
    "without goto we would be wtf'd"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 11-15-2017, 09:02 AM
  2. Reverse of sine table (back to code)
    By xArt in forum Tech Board
    Replies: 2
    Last Post: 01-06-2017, 05:58 AM
  3. Replies: 3
    Last Post: 09-23-2014, 07:33 PM
  4. Replies: 26
    Last Post: 10-30-2013, 11:34 PM
  5. Graphing a sine/cosine/tan curves in C???
    By Brokn in forum C Programming
    Replies: 3
    Last Post: 12-13-2005, 03:36 AM

Tags for this Thread