Is there a way of expressing the MATHEMATICAL module of an int in C?









up vote
-2
down vote

favorite












I mean this | | not this %.
Like let's say that I've got two integers x and y and and integer z.
Now



z = x - y


Is there a way to express |z| so that if z is positive it stays positive and if z is negative it's turnt into its opposite? I don't mean to express it with an if, just a mathematical equation, symbol or something like that.










share|improve this question



















  • 1




    You mean the absolute value of z? Because that exists in math.h. Edit: Since you answered positively, I'll post an answer.
    – gsamaras
    Nov 11 at 16:19







  • 1




    If you want to learn a different language, then please start at the beginning. Either taking on or more classes, or by reading books.
    – Some programmer dude
    Nov 11 at 16:23










  • As for your problem, mathematically it's also called the absolute value and searching for that term (together with C) should have given you an answer quite quickly.
    – Some programmer dude
    Nov 11 at 16:23







  • 1




    Ok damn, sorry for the dumb question then. I could have just looked it up but in my language we call the absolute value module so when I searched for "module of an int in C" I could find nothing. Thanks anyways! @Someprogrammerdude
    – FoxyIT
    Nov 11 at 16:24











  • @PaulOgilvie: Re “CPUs don't have native instructions for that”: The ARMv7 architecture has absolute difference instructions for integers, which provide exactly the abs(x-y) the OP seeks and of course provide abs(x) by making y zero. The Intel 64 and IA-32 architectures have absolute value and absolute difference instructions for integers.
    – Eric Postpischil
    Nov 11 at 18:55















up vote
-2
down vote

favorite












I mean this | | not this %.
Like let's say that I've got two integers x and y and and integer z.
Now



z = x - y


Is there a way to express |z| so that if z is positive it stays positive and if z is negative it's turnt into its opposite? I don't mean to express it with an if, just a mathematical equation, symbol or something like that.










share|improve this question



















  • 1




    You mean the absolute value of z? Because that exists in math.h. Edit: Since you answered positively, I'll post an answer.
    – gsamaras
    Nov 11 at 16:19







  • 1




    If you want to learn a different language, then please start at the beginning. Either taking on or more classes, or by reading books.
    – Some programmer dude
    Nov 11 at 16:23










  • As for your problem, mathematically it's also called the absolute value and searching for that term (together with C) should have given you an answer quite quickly.
    – Some programmer dude
    Nov 11 at 16:23







  • 1




    Ok damn, sorry for the dumb question then. I could have just looked it up but in my language we call the absolute value module so when I searched for "module of an int in C" I could find nothing. Thanks anyways! @Someprogrammerdude
    – FoxyIT
    Nov 11 at 16:24











  • @PaulOgilvie: Re “CPUs don't have native instructions for that”: The ARMv7 architecture has absolute difference instructions for integers, which provide exactly the abs(x-y) the OP seeks and of course provide abs(x) by making y zero. The Intel 64 and IA-32 architectures have absolute value and absolute difference instructions for integers.
    – Eric Postpischil
    Nov 11 at 18:55













up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











I mean this | | not this %.
Like let's say that I've got two integers x and y and and integer z.
Now



z = x - y


Is there a way to express |z| so that if z is positive it stays positive and if z is negative it's turnt into its opposite? I don't mean to express it with an if, just a mathematical equation, symbol or something like that.










share|improve this question















I mean this | | not this %.
Like let's say that I've got two integers x and y and and integer z.
Now



z = x - y


Is there a way to express |z| so that if z is positive it stays positive and if z is negative it's turnt into its opposite? I don't mean to express it with an if, just a mathematical equation, symbol or something like that.







c math






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 16:21









gsamaras

49.3k2398179




49.3k2398179










asked Nov 11 at 16:18









FoxyIT

617




617







  • 1




    You mean the absolute value of z? Because that exists in math.h. Edit: Since you answered positively, I'll post an answer.
    – gsamaras
    Nov 11 at 16:19







  • 1




    If you want to learn a different language, then please start at the beginning. Either taking on or more classes, or by reading books.
    – Some programmer dude
    Nov 11 at 16:23










  • As for your problem, mathematically it's also called the absolute value and searching for that term (together with C) should have given you an answer quite quickly.
    – Some programmer dude
    Nov 11 at 16:23







  • 1




    Ok damn, sorry for the dumb question then. I could have just looked it up but in my language we call the absolute value module so when I searched for "module of an int in C" I could find nothing. Thanks anyways! @Someprogrammerdude
    – FoxyIT
    Nov 11 at 16:24











  • @PaulOgilvie: Re “CPUs don't have native instructions for that”: The ARMv7 architecture has absolute difference instructions for integers, which provide exactly the abs(x-y) the OP seeks and of course provide abs(x) by making y zero. The Intel 64 and IA-32 architectures have absolute value and absolute difference instructions for integers.
    – Eric Postpischil
    Nov 11 at 18:55













  • 1




    You mean the absolute value of z? Because that exists in math.h. Edit: Since you answered positively, I'll post an answer.
    – gsamaras
    Nov 11 at 16:19







  • 1




    If you want to learn a different language, then please start at the beginning. Either taking on or more classes, or by reading books.
    – Some programmer dude
    Nov 11 at 16:23










  • As for your problem, mathematically it's also called the absolute value and searching for that term (together with C) should have given you an answer quite quickly.
    – Some programmer dude
    Nov 11 at 16:23







  • 1




    Ok damn, sorry for the dumb question then. I could have just looked it up but in my language we call the absolute value module so when I searched for "module of an int in C" I could find nothing. Thanks anyways! @Someprogrammerdude
    – FoxyIT
    Nov 11 at 16:24











  • @PaulOgilvie: Re “CPUs don't have native instructions for that”: The ARMv7 architecture has absolute difference instructions for integers, which provide exactly the abs(x-y) the OP seeks and of course provide abs(x) by making y zero. The Intel 64 and IA-32 architectures have absolute value and absolute difference instructions for integers.
    – Eric Postpischil
    Nov 11 at 18:55








1




1




You mean the absolute value of z? Because that exists in math.h. Edit: Since you answered positively, I'll post an answer.
– gsamaras
Nov 11 at 16:19





You mean the absolute value of z? Because that exists in math.h. Edit: Since you answered positively, I'll post an answer.
– gsamaras
Nov 11 at 16:19





1




1




If you want to learn a different language, then please start at the beginning. Either taking on or more classes, or by reading books.
– Some programmer dude
Nov 11 at 16:23




If you want to learn a different language, then please start at the beginning. Either taking on or more classes, or by reading books.
– Some programmer dude
Nov 11 at 16:23












As for your problem, mathematically it's also called the absolute value and searching for that term (together with C) should have given you an answer quite quickly.
– Some programmer dude
Nov 11 at 16:23





As for your problem, mathematically it's also called the absolute value and searching for that term (together with C) should have given you an answer quite quickly.
– Some programmer dude
Nov 11 at 16:23





1




1




Ok damn, sorry for the dumb question then. I could have just looked it up but in my language we call the absolute value module so when I searched for "module of an int in C" I could find nothing. Thanks anyways! @Someprogrammerdude
– FoxyIT
Nov 11 at 16:24





Ok damn, sorry for the dumb question then. I could have just looked it up but in my language we call the absolute value module so when I searched for "module of an int in C" I could find nothing. Thanks anyways! @Someprogrammerdude
– FoxyIT
Nov 11 at 16:24













@PaulOgilvie: Re “CPUs don't have native instructions for that”: The ARMv7 architecture has absolute difference instructions for integers, which provide exactly the abs(x-y) the OP seeks and of course provide abs(x) by making y zero. The Intel 64 and IA-32 architectures have absolute value and absolute difference instructions for integers.
– Eric Postpischil
Nov 11 at 18:55





@PaulOgilvie: Re “CPUs don't have native instructions for that”: The ARMv7 architecture has absolute difference instructions for integers, which provide exactly the abs(x-y) the OP seeks and of course provide abs(x) by making y zero. The Intel 64 and IA-32 architectures have absolute value and absolute difference instructions for integers.
– Eric Postpischil
Nov 11 at 18:55













2 Answers
2






active

oldest

votes

















up vote
2
down vote



accepted










You want the abs() function, provided in the math.h header.



Example:



z = abs(x - y);





share|improve this answer



























    up vote
    1
    down vote













    If no abs() function, second way (without branching):



     int num;

    printf("Enter num: ");
    scanf("%d", &num);
    printf("Abs=%dn", num*(1+(num<0)*-2));


    EDIT after comment from StoryTeller:



    The trick works because a conditional test, like "<", returns 0 (for false) or 1 (for true). We can deploy this returned number in a more complex expression. In this case, we want an expression which returns 1 or -1. If the number num is NOT less than 0, i.e. is not negative, "num<0" returns 0 and the expression gives 1+(-2*0) = 1. The number num is returned unmodified. If num IS less than 0, i.e. it is negative, the expression returns -1: 1 + (-2*TRUE), where TRUE's value is 1.



    Having now 1 or -1 depending on num being negative, a simple multiplication inverts the sign of num, when needed.






    share|improve this answer






















    • A word about the mapping you use ([0, 1] -> [-1, 1]) will make the answer better, but +1 from me.
      – StoryTeller
      Nov 11 at 19:46











    • @StoryTeller correct, ty. I edited the post.
      – linuxfan
      Nov 12 at 16:01










    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',
    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%2f53250680%2fis-there-a-way-of-expressing-the-mathematical-module-of-an-int-in-c%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote



    accepted










    You want the abs() function, provided in the math.h header.



    Example:



    z = abs(x - y);





    share|improve this answer
























      up vote
      2
      down vote



      accepted










      You want the abs() function, provided in the math.h header.



      Example:



      z = abs(x - y);





      share|improve this answer






















        up vote
        2
        down vote



        accepted







        up vote
        2
        down vote



        accepted






        You want the abs() function, provided in the math.h header.



        Example:



        z = abs(x - y);





        share|improve this answer












        You want the abs() function, provided in the math.h header.



        Example:



        z = abs(x - y);






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 11 at 16:20









        gsamaras

        49.3k2398179




        49.3k2398179






















            up vote
            1
            down vote













            If no abs() function, second way (without branching):



             int num;

            printf("Enter num: ");
            scanf("%d", &num);
            printf("Abs=%dn", num*(1+(num<0)*-2));


            EDIT after comment from StoryTeller:



            The trick works because a conditional test, like "<", returns 0 (for false) or 1 (for true). We can deploy this returned number in a more complex expression. In this case, we want an expression which returns 1 or -1. If the number num is NOT less than 0, i.e. is not negative, "num<0" returns 0 and the expression gives 1+(-2*0) = 1. The number num is returned unmodified. If num IS less than 0, i.e. it is negative, the expression returns -1: 1 + (-2*TRUE), where TRUE's value is 1.



            Having now 1 or -1 depending on num being negative, a simple multiplication inverts the sign of num, when needed.






            share|improve this answer






















            • A word about the mapping you use ([0, 1] -> [-1, 1]) will make the answer better, but +1 from me.
              – StoryTeller
              Nov 11 at 19:46











            • @StoryTeller correct, ty. I edited the post.
              – linuxfan
              Nov 12 at 16:01














            up vote
            1
            down vote













            If no abs() function, second way (without branching):



             int num;

            printf("Enter num: ");
            scanf("%d", &num);
            printf("Abs=%dn", num*(1+(num<0)*-2));


            EDIT after comment from StoryTeller:



            The trick works because a conditional test, like "<", returns 0 (for false) or 1 (for true). We can deploy this returned number in a more complex expression. In this case, we want an expression which returns 1 or -1. If the number num is NOT less than 0, i.e. is not negative, "num<0" returns 0 and the expression gives 1+(-2*0) = 1. The number num is returned unmodified. If num IS less than 0, i.e. it is negative, the expression returns -1: 1 + (-2*TRUE), where TRUE's value is 1.



            Having now 1 or -1 depending on num being negative, a simple multiplication inverts the sign of num, when needed.






            share|improve this answer






















            • A word about the mapping you use ([0, 1] -> [-1, 1]) will make the answer better, but +1 from me.
              – StoryTeller
              Nov 11 at 19:46











            • @StoryTeller correct, ty. I edited the post.
              – linuxfan
              Nov 12 at 16:01












            up vote
            1
            down vote










            up vote
            1
            down vote









            If no abs() function, second way (without branching):



             int num;

            printf("Enter num: ");
            scanf("%d", &num);
            printf("Abs=%dn", num*(1+(num<0)*-2));


            EDIT after comment from StoryTeller:



            The trick works because a conditional test, like "<", returns 0 (for false) or 1 (for true). We can deploy this returned number in a more complex expression. In this case, we want an expression which returns 1 or -1. If the number num is NOT less than 0, i.e. is not negative, "num<0" returns 0 and the expression gives 1+(-2*0) = 1. The number num is returned unmodified. If num IS less than 0, i.e. it is negative, the expression returns -1: 1 + (-2*TRUE), where TRUE's value is 1.



            Having now 1 or -1 depending on num being negative, a simple multiplication inverts the sign of num, when needed.






            share|improve this answer














            If no abs() function, second way (without branching):



             int num;

            printf("Enter num: ");
            scanf("%d", &num);
            printf("Abs=%dn", num*(1+(num<0)*-2));


            EDIT after comment from StoryTeller:



            The trick works because a conditional test, like "<", returns 0 (for false) or 1 (for true). We can deploy this returned number in a more complex expression. In this case, we want an expression which returns 1 or -1. If the number num is NOT less than 0, i.e. is not negative, "num<0" returns 0 and the expression gives 1+(-2*0) = 1. The number num is returned unmodified. If num IS less than 0, i.e. it is negative, the expression returns -1: 1 + (-2*TRUE), where TRUE's value is 1.



            Having now 1 or -1 depending on num being negative, a simple multiplication inverts the sign of num, when needed.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 12 at 14:54

























            answered Nov 11 at 17:00









            linuxfan

            1,7742617




            1,7742617











            • A word about the mapping you use ([0, 1] -> [-1, 1]) will make the answer better, but +1 from me.
              – StoryTeller
              Nov 11 at 19:46











            • @StoryTeller correct, ty. I edited the post.
              – linuxfan
              Nov 12 at 16:01
















            • A word about the mapping you use ([0, 1] -> [-1, 1]) will make the answer better, but +1 from me.
              – StoryTeller
              Nov 11 at 19:46











            • @StoryTeller correct, ty. I edited the post.
              – linuxfan
              Nov 12 at 16:01















            A word about the mapping you use ([0, 1] -> [-1, 1]) will make the answer better, but +1 from me.
            – StoryTeller
            Nov 11 at 19:46





            A word about the mapping you use ([0, 1] -> [-1, 1]) will make the answer better, but +1 from me.
            – StoryTeller
            Nov 11 at 19:46













            @StoryTeller correct, ty. I edited the post.
            – linuxfan
            Nov 12 at 16:01




            @StoryTeller correct, ty. I edited the post.
            – linuxfan
            Nov 12 at 16:01

















            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2f53250680%2fis-there-a-way-of-expressing-the-mathematical-module-of-an-int-in-c%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







            這個網誌中的熱門文章

            What does pagestruct do in Eviews?

            Dutch intervention in Lombok and Karangasem

            Channel Islands