Can I shorten a function name I use repeatedly?












8















I have a long formula, like the following:



float a = sin(b)*cos(c)+sin(c+d)*sin(d)....


Is there a way to use s instead of sin in C, to shorten the formula, without affecting the running time?










share|improve this question




















  • 2





    double (*s)(double) = sin;

    – iBug
    Jan 23 at 14:01






  • 25





    The only effect using #define to shorten sin to s is to make your code unreadable

    – Chris Turner
    Jan 23 at 14:01






  • 1





    You could write an emac macro that writes for you sin, whenever you type e.g. CTRL + s. In Vim you could write once sin and then use the . to repeat typing that as many times as you want.

    – Joey Mallone
    Jan 23 at 14:04








  • 5





    I understand there might be reasons to disagree with OP's coding style but this seems to be a perfectly valid and to-the-point question. I don't see why it should be downvoted.

    – hugomg
    Jan 23 at 14:06






  • 6





    Not downvoting but I encourage you to reconsider youe idea. Renaming standard library functions can create confusion for future code readers and maintainers. Make your expressions more readable by using whitespace and if necessary splitting them over more than one line.

    – rici
    Jan 23 at 14:15
















8















I have a long formula, like the following:



float a = sin(b)*cos(c)+sin(c+d)*sin(d)....


Is there a way to use s instead of sin in C, to shorten the formula, without affecting the running time?










share|improve this question




















  • 2





    double (*s)(double) = sin;

    – iBug
    Jan 23 at 14:01






  • 25





    The only effect using #define to shorten sin to s is to make your code unreadable

    – Chris Turner
    Jan 23 at 14:01






  • 1





    You could write an emac macro that writes for you sin, whenever you type e.g. CTRL + s. In Vim you could write once sin and then use the . to repeat typing that as many times as you want.

    – Joey Mallone
    Jan 23 at 14:04








  • 5





    I understand there might be reasons to disagree with OP's coding style but this seems to be a perfectly valid and to-the-point question. I don't see why it should be downvoted.

    – hugomg
    Jan 23 at 14:06






  • 6





    Not downvoting but I encourage you to reconsider youe idea. Renaming standard library functions can create confusion for future code readers and maintainers. Make your expressions more readable by using whitespace and if necessary splitting them over more than one line.

    – rici
    Jan 23 at 14:15














8












8








8








I have a long formula, like the following:



float a = sin(b)*cos(c)+sin(c+d)*sin(d)....


Is there a way to use s instead of sin in C, to shorten the formula, without affecting the running time?










share|improve this question
















I have a long formula, like the following:



float a = sin(b)*cos(c)+sin(c+d)*sin(d)....


Is there a way to use s instead of sin in C, to shorten the formula, without affecting the running time?







c formula






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 27 at 18:18









double-beep

2,1382824




2,1382824










asked Jan 23 at 14:00









BenBen

676




676








  • 2





    double (*s)(double) = sin;

    – iBug
    Jan 23 at 14:01






  • 25





    The only effect using #define to shorten sin to s is to make your code unreadable

    – Chris Turner
    Jan 23 at 14:01






  • 1





    You could write an emac macro that writes for you sin, whenever you type e.g. CTRL + s. In Vim you could write once sin and then use the . to repeat typing that as many times as you want.

    – Joey Mallone
    Jan 23 at 14:04








  • 5





    I understand there might be reasons to disagree with OP's coding style but this seems to be a perfectly valid and to-the-point question. I don't see why it should be downvoted.

    – hugomg
    Jan 23 at 14:06






  • 6





    Not downvoting but I encourage you to reconsider youe idea. Renaming standard library functions can create confusion for future code readers and maintainers. Make your expressions more readable by using whitespace and if necessary splitting them over more than one line.

    – rici
    Jan 23 at 14:15














  • 2





    double (*s)(double) = sin;

    – iBug
    Jan 23 at 14:01






  • 25





    The only effect using #define to shorten sin to s is to make your code unreadable

    – Chris Turner
    Jan 23 at 14:01






  • 1





    You could write an emac macro that writes for you sin, whenever you type e.g. CTRL + s. In Vim you could write once sin and then use the . to repeat typing that as many times as you want.

    – Joey Mallone
    Jan 23 at 14:04








  • 5





    I understand there might be reasons to disagree with OP's coding style but this seems to be a perfectly valid and to-the-point question. I don't see why it should be downvoted.

    – hugomg
    Jan 23 at 14:06






  • 6





    Not downvoting but I encourage you to reconsider youe idea. Renaming standard library functions can create confusion for future code readers and maintainers. Make your expressions more readable by using whitespace and if necessary splitting them over more than one line.

    – rici
    Jan 23 at 14:15








2




2





double (*s)(double) = sin;

– iBug
Jan 23 at 14:01





double (*s)(double) = sin;

– iBug
Jan 23 at 14:01




25




25





The only effect using #define to shorten sin to s is to make your code unreadable

– Chris Turner
Jan 23 at 14:01





The only effect using #define to shorten sin to s is to make your code unreadable

– Chris Turner
Jan 23 at 14:01




1




1





You could write an emac macro that writes for you sin, whenever you type e.g. CTRL + s. In Vim you could write once sin and then use the . to repeat typing that as many times as you want.

– Joey Mallone
Jan 23 at 14:04







You could write an emac macro that writes for you sin, whenever you type e.g. CTRL + s. In Vim you could write once sin and then use the . to repeat typing that as many times as you want.

– Joey Mallone
Jan 23 at 14:04






5




5





I understand there might be reasons to disagree with OP's coding style but this seems to be a perfectly valid and to-the-point question. I don't see why it should be downvoted.

– hugomg
Jan 23 at 14:06





I understand there might be reasons to disagree with OP's coding style but this seems to be a perfectly valid and to-the-point question. I don't see why it should be downvoted.

– hugomg
Jan 23 at 14:06




6




6





Not downvoting but I encourage you to reconsider youe idea. Renaming standard library functions can create confusion for future code readers and maintainers. Make your expressions more readable by using whitespace and if necessary splitting them over more than one line.

– rici
Jan 23 at 14:15





Not downvoting but I encourage you to reconsider youe idea. Renaming standard library functions can create confusion for future code readers and maintainers. Make your expressions more readable by using whitespace and if necessary splitting them over more than one line.

– rici
Jan 23 at 14:15












3 Answers
3






active

oldest

votes


















13














There are at least three options for using s for sin:



Use a preprocessor macro:



#define s(x) (sin(x))
#define c(x) (cos(x))
float a = s(b)*c(c)+s(c+d)*c(d)....
#undef c
#undef s


Note that the macros definitions are immediately removed with #undef to prevent them from affecting subsequent code. Also, you should be aware of the basics of preprocessor macro substitution, noting the fact that the first c in c(c) will be expanded but the second c will not since the function-like macro c(x) is expanded only where c is followed by (.



This solution will have no effect on run time.



Use an inline function:



static inline double s(double x) { return sin(x); }
static inline double c(double x) { return cos(x); }


With a good compiler, this will have no effect on run time, since the compiler should replace a call to s or c with a direct call to sin or cos, having the same result as the original code. Unfortunately, in this case, the c function will conflict with the c object you show in your sample code. You will need to change one of the names.



Use function pointers:



static double (* const s)(double) = sin;
static double (* const c)(double) = cos;


With a good compiler, this also will have no effect on run time, although I suspect a few more compilers might fail to optimize code using this solution than than previous solution. Again, you will have the name conflict with c. Note that using function pointers creates a direct call to the sin and cos functions, bypassing any macros that the C implementation might have defined for them. (C implementations are allowed to implement library function using macros as well as functions, and they might do so to support optimizations or certain features. With a good quality compiler, this is usually a minor concern; optimization of a direct call still should be good.)






share|improve this answer































    15















    if I use define, does it affect runtime?




    define works by doing text-based substitution at compile time. If you #define s(x) sin(x) then the C pre-processor will rewrite all the s(x) into sin(x) before the compiler gets a chance to look at it.



    BTW, this kind of low-level text-munging is exactly why define can be dangerous to use for more complex expressions. For example, one classic pitfall is that if you do something like #define times(x, y) x*y then times(1+1,2) rewrites to 1+1*2, which evaluates to 3 instead of the expected 4. For more complex expressions like it is often a good idea to use inlineable functions instead.






    share|improve this answer





















    • 2





      Macro substitution substitutes preprocessor tokens, not text.

      – Eric Postpischil
      Jan 23 at 14:25



















    7














    Don't do this.



    Mathematicians have been abbreviating the trigonometric functions to sin, cos, tan, sinh, cosh, and tanh for many many years now. Even though mathematicians (like me) like to use their favourite and often idiosyncratic notation so puffing up any paper by a number of pages, these have emerged as pretty standard. Even LaTeX has commands like sin, cos, and tan.



    The Japanese immortalised the abbreviations when releasing scientific calculators in the 1970s (the shorthand can fit easily on a button), and the C standard library adopted them.



    If you deviate from this then your code immediately becomes difficult to read. This can be particularly pernicious with mathematical code where you can't immediately see the effects of a bad implementation.



    But if you must, then a simple



    static double(*const s)(double) = sin;


    will suffice.






    share|improve this answer

























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54328965%2fcan-i-shorten-a-function-name-i-use-repeatedly%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      13














      There are at least three options for using s for sin:



      Use a preprocessor macro:



      #define s(x) (sin(x))
      #define c(x) (cos(x))
      float a = s(b)*c(c)+s(c+d)*c(d)....
      #undef c
      #undef s


      Note that the macros definitions are immediately removed with #undef to prevent them from affecting subsequent code. Also, you should be aware of the basics of preprocessor macro substitution, noting the fact that the first c in c(c) will be expanded but the second c will not since the function-like macro c(x) is expanded only where c is followed by (.



      This solution will have no effect on run time.



      Use an inline function:



      static inline double s(double x) { return sin(x); }
      static inline double c(double x) { return cos(x); }


      With a good compiler, this will have no effect on run time, since the compiler should replace a call to s or c with a direct call to sin or cos, having the same result as the original code. Unfortunately, in this case, the c function will conflict with the c object you show in your sample code. You will need to change one of the names.



      Use function pointers:



      static double (* const s)(double) = sin;
      static double (* const c)(double) = cos;


      With a good compiler, this also will have no effect on run time, although I suspect a few more compilers might fail to optimize code using this solution than than previous solution. Again, you will have the name conflict with c. Note that using function pointers creates a direct call to the sin and cos functions, bypassing any macros that the C implementation might have defined for them. (C implementations are allowed to implement library function using macros as well as functions, and they might do so to support optimizations or certain features. With a good quality compiler, this is usually a minor concern; optimization of a direct call still should be good.)






      share|improve this answer




























        13














        There are at least three options for using s for sin:



        Use a preprocessor macro:



        #define s(x) (sin(x))
        #define c(x) (cos(x))
        float a = s(b)*c(c)+s(c+d)*c(d)....
        #undef c
        #undef s


        Note that the macros definitions are immediately removed with #undef to prevent them from affecting subsequent code. Also, you should be aware of the basics of preprocessor macro substitution, noting the fact that the first c in c(c) will be expanded but the second c will not since the function-like macro c(x) is expanded only where c is followed by (.



        This solution will have no effect on run time.



        Use an inline function:



        static inline double s(double x) { return sin(x); }
        static inline double c(double x) { return cos(x); }


        With a good compiler, this will have no effect on run time, since the compiler should replace a call to s or c with a direct call to sin or cos, having the same result as the original code. Unfortunately, in this case, the c function will conflict with the c object you show in your sample code. You will need to change one of the names.



        Use function pointers:



        static double (* const s)(double) = sin;
        static double (* const c)(double) = cos;


        With a good compiler, this also will have no effect on run time, although I suspect a few more compilers might fail to optimize code using this solution than than previous solution. Again, you will have the name conflict with c. Note that using function pointers creates a direct call to the sin and cos functions, bypassing any macros that the C implementation might have defined for them. (C implementations are allowed to implement library function using macros as well as functions, and they might do so to support optimizations or certain features. With a good quality compiler, this is usually a minor concern; optimization of a direct call still should be good.)






        share|improve this answer


























          13












          13








          13







          There are at least three options for using s for sin:



          Use a preprocessor macro:



          #define s(x) (sin(x))
          #define c(x) (cos(x))
          float a = s(b)*c(c)+s(c+d)*c(d)....
          #undef c
          #undef s


          Note that the macros definitions are immediately removed with #undef to prevent them from affecting subsequent code. Also, you should be aware of the basics of preprocessor macro substitution, noting the fact that the first c in c(c) will be expanded but the second c will not since the function-like macro c(x) is expanded only where c is followed by (.



          This solution will have no effect on run time.



          Use an inline function:



          static inline double s(double x) { return sin(x); }
          static inline double c(double x) { return cos(x); }


          With a good compiler, this will have no effect on run time, since the compiler should replace a call to s or c with a direct call to sin or cos, having the same result as the original code. Unfortunately, in this case, the c function will conflict with the c object you show in your sample code. You will need to change one of the names.



          Use function pointers:



          static double (* const s)(double) = sin;
          static double (* const c)(double) = cos;


          With a good compiler, this also will have no effect on run time, although I suspect a few more compilers might fail to optimize code using this solution than than previous solution. Again, you will have the name conflict with c. Note that using function pointers creates a direct call to the sin and cos functions, bypassing any macros that the C implementation might have defined for them. (C implementations are allowed to implement library function using macros as well as functions, and they might do so to support optimizations or certain features. With a good quality compiler, this is usually a minor concern; optimization of a direct call still should be good.)






          share|improve this answer













          There are at least three options for using s for sin:



          Use a preprocessor macro:



          #define s(x) (sin(x))
          #define c(x) (cos(x))
          float a = s(b)*c(c)+s(c+d)*c(d)....
          #undef c
          #undef s


          Note that the macros definitions are immediately removed with #undef to prevent them from affecting subsequent code. Also, you should be aware of the basics of preprocessor macro substitution, noting the fact that the first c in c(c) will be expanded but the second c will not since the function-like macro c(x) is expanded only where c is followed by (.



          This solution will have no effect on run time.



          Use an inline function:



          static inline double s(double x) { return sin(x); }
          static inline double c(double x) { return cos(x); }


          With a good compiler, this will have no effect on run time, since the compiler should replace a call to s or c with a direct call to sin or cos, having the same result as the original code. Unfortunately, in this case, the c function will conflict with the c object you show in your sample code. You will need to change one of the names.



          Use function pointers:



          static double (* const s)(double) = sin;
          static double (* const c)(double) = cos;


          With a good compiler, this also will have no effect on run time, although I suspect a few more compilers might fail to optimize code using this solution than than previous solution. Again, you will have the name conflict with c. Note that using function pointers creates a direct call to the sin and cos functions, bypassing any macros that the C implementation might have defined for them. (C implementations are allowed to implement library function using macros as well as functions, and they might do so to support optimizations or certain features. With a good quality compiler, this is usually a minor concern; optimization of a direct call still should be good.)







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 23 at 14:39









          Eric PostpischilEric Postpischil

          73.9k879160




          73.9k879160

























              15















              if I use define, does it affect runtime?




              define works by doing text-based substitution at compile time. If you #define s(x) sin(x) then the C pre-processor will rewrite all the s(x) into sin(x) before the compiler gets a chance to look at it.



              BTW, this kind of low-level text-munging is exactly why define can be dangerous to use for more complex expressions. For example, one classic pitfall is that if you do something like #define times(x, y) x*y then times(1+1,2) rewrites to 1+1*2, which evaluates to 3 instead of the expected 4. For more complex expressions like it is often a good idea to use inlineable functions instead.






              share|improve this answer





















              • 2





                Macro substitution substitutes preprocessor tokens, not text.

                – Eric Postpischil
                Jan 23 at 14:25
















              15















              if I use define, does it affect runtime?




              define works by doing text-based substitution at compile time. If you #define s(x) sin(x) then the C pre-processor will rewrite all the s(x) into sin(x) before the compiler gets a chance to look at it.



              BTW, this kind of low-level text-munging is exactly why define can be dangerous to use for more complex expressions. For example, one classic pitfall is that if you do something like #define times(x, y) x*y then times(1+1,2) rewrites to 1+1*2, which evaluates to 3 instead of the expected 4. For more complex expressions like it is often a good idea to use inlineable functions instead.






              share|improve this answer





















              • 2





                Macro substitution substitutes preprocessor tokens, not text.

                – Eric Postpischil
                Jan 23 at 14:25














              15












              15








              15








              if I use define, does it affect runtime?




              define works by doing text-based substitution at compile time. If you #define s(x) sin(x) then the C pre-processor will rewrite all the s(x) into sin(x) before the compiler gets a chance to look at it.



              BTW, this kind of low-level text-munging is exactly why define can be dangerous to use for more complex expressions. For example, one classic pitfall is that if you do something like #define times(x, y) x*y then times(1+1,2) rewrites to 1+1*2, which evaluates to 3 instead of the expected 4. For more complex expressions like it is often a good idea to use inlineable functions instead.






              share|improve this answer
















              if I use define, does it affect runtime?




              define works by doing text-based substitution at compile time. If you #define s(x) sin(x) then the C pre-processor will rewrite all the s(x) into sin(x) before the compiler gets a chance to look at it.



              BTW, this kind of low-level text-munging is exactly why define can be dangerous to use for more complex expressions. For example, one classic pitfall is that if you do something like #define times(x, y) x*y then times(1+1,2) rewrites to 1+1*2, which evaluates to 3 instead of the expected 4. For more complex expressions like it is often a good idea to use inlineable functions instead.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Jan 23 at 14:09

























              answered Jan 23 at 14:02









              hugomghugomg

              49.9k17120201




              49.9k17120201








              • 2





                Macro substitution substitutes preprocessor tokens, not text.

                – Eric Postpischil
                Jan 23 at 14:25














              • 2





                Macro substitution substitutes preprocessor tokens, not text.

                – Eric Postpischil
                Jan 23 at 14:25








              2




              2





              Macro substitution substitutes preprocessor tokens, not text.

              – Eric Postpischil
              Jan 23 at 14:25





              Macro substitution substitutes preprocessor tokens, not text.

              – Eric Postpischil
              Jan 23 at 14:25











              7














              Don't do this.



              Mathematicians have been abbreviating the trigonometric functions to sin, cos, tan, sinh, cosh, and tanh for many many years now. Even though mathematicians (like me) like to use their favourite and often idiosyncratic notation so puffing up any paper by a number of pages, these have emerged as pretty standard. Even LaTeX has commands like sin, cos, and tan.



              The Japanese immortalised the abbreviations when releasing scientific calculators in the 1970s (the shorthand can fit easily on a button), and the C standard library adopted them.



              If you deviate from this then your code immediately becomes difficult to read. This can be particularly pernicious with mathematical code where you can't immediately see the effects of a bad implementation.



              But if you must, then a simple



              static double(*const s)(double) = sin;


              will suffice.






              share|improve this answer






























                7














                Don't do this.



                Mathematicians have been abbreviating the trigonometric functions to sin, cos, tan, sinh, cosh, and tanh for many many years now. Even though mathematicians (like me) like to use their favourite and often idiosyncratic notation so puffing up any paper by a number of pages, these have emerged as pretty standard. Even LaTeX has commands like sin, cos, and tan.



                The Japanese immortalised the abbreviations when releasing scientific calculators in the 1970s (the shorthand can fit easily on a button), and the C standard library adopted them.



                If you deviate from this then your code immediately becomes difficult to read. This can be particularly pernicious with mathematical code where you can't immediately see the effects of a bad implementation.



                But if you must, then a simple



                static double(*const s)(double) = sin;


                will suffice.






                share|improve this answer




























                  7












                  7








                  7







                  Don't do this.



                  Mathematicians have been abbreviating the trigonometric functions to sin, cos, tan, sinh, cosh, and tanh for many many years now. Even though mathematicians (like me) like to use their favourite and often idiosyncratic notation so puffing up any paper by a number of pages, these have emerged as pretty standard. Even LaTeX has commands like sin, cos, and tan.



                  The Japanese immortalised the abbreviations when releasing scientific calculators in the 1970s (the shorthand can fit easily on a button), and the C standard library adopted them.



                  If you deviate from this then your code immediately becomes difficult to read. This can be particularly pernicious with mathematical code where you can't immediately see the effects of a bad implementation.



                  But if you must, then a simple



                  static double(*const s)(double) = sin;


                  will suffice.






                  share|improve this answer















                  Don't do this.



                  Mathematicians have been abbreviating the trigonometric functions to sin, cos, tan, sinh, cosh, and tanh for many many years now. Even though mathematicians (like me) like to use their favourite and often idiosyncratic notation so puffing up any paper by a number of pages, these have emerged as pretty standard. Even LaTeX has commands like sin, cos, and tan.



                  The Japanese immortalised the abbreviations when releasing scientific calculators in the 1970s (the shorthand can fit easily on a button), and the C standard library adopted them.



                  If you deviate from this then your code immediately becomes difficult to read. This can be particularly pernicious with mathematical code where you can't immediately see the effects of a bad implementation.



                  But if you must, then a simple



                  static double(*const s)(double) = sin;


                  will suffice.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jan 23 at 19:56

























                  answered Jan 23 at 15:04









                  BathshebaBathsheba

                  178k27253378




                  178k27253378






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54328965%2fcan-i-shorten-a-function-name-i-use-repeatedly%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      How to change which sound is reproduced for terminal bell?

                      Can I use Tabulator js library in my java Spring + Thymeleaf project?

                      Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents