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.
c math
|
show 1 more comment
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.
c math
1
You mean the absolute value ofz? 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 theabs(x-y)the OP seeks and of course provideabs(x)by makingyzero. The Intel 64 and IA-32 architectures have absolute value and absolute difference instructions for integers.
– Eric Postpischil
Nov 11 at 18:55
|
show 1 more comment
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.
c math
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
c math
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 ofz? 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 theabs(x-y)the OP seeks and of course provideabs(x)by makingyzero. The Intel 64 and IA-32 architectures have absolute value and absolute difference instructions for integers.
– Eric Postpischil
Nov 11 at 18:55
|
show 1 more comment
1
You mean the absolute value ofz? 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 theabs(x-y)the OP seeks and of course provideabs(x)by makingyzero. 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
|
show 1 more comment
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);
add a comment |
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.
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
add a comment |
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);
add a comment |
up vote
2
down vote
accepted
You want the abs() function, provided in the math.h header.
Example:
z = abs(x - y);
add a comment |
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);
You want the abs() function, provided in the math.h header.
Example:
z = abs(x - y);
answered Nov 11 at 16:20
gsamaras
49.3k2398179
49.3k2398179
add a comment |
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 provideabs(x)by makingyzero. The Intel 64 and IA-32 architectures have absolute value and absolute difference instructions for integers.– Eric Postpischil
Nov 11 at 18:55