Regarding abs() and labs()
Hello all
Just took a look at abs() and can not understand why it seem so overcomplicated. Why not just:
#define abs(x) ((x) < 0) ? -(x) : (x))
and skip labs() (not used anyway)
+ it gives a warning with -W if x is of a unsigned type ("condition is always false" or similar).
But remember to use %u when printing an unsigned, took me a while before I understood how an unsigned could contain a "negative" value :)
Happy weekend
Richard Knutsson
_______________________________________________
Kernel-janitors mailing list
[email protected]
https://lists.osdl.org/mailman/listinfo/kernel-janitors
Quoted 6 lines · On Sat, 03 Feb 2007 02:13:44 +0100 Richard Knutsson wrote:
Hello all
Just took a look at
abs()and can not understand why it seem so overcomplicated. Why not just:#define abs(x) ((x) < 0) ? -(x) : (x))
It's almost that simple. It just adds a local __x so that 'x' won't be evaluated multiple times as it would in the simpler version. Multiple evaluations (of an expression) (that could have side effects) would take more CPU horsepower and could have bad side effects as well.
Quoted 5 lines
and skip
labs()(not used anyway)
+ it gives a warning with -W if x is of a unsigned type ("condition is always false" or similar).
But remember to use %u when printing an unsigned, took me a while before I understood how an unsigned could contain a "negative" value :)
---
~Randy
_______________________________________________
Kernel-janitors mailing list
[email protected]
https://lists.osdl.org/mailman/listinfo/kernel-janitors
Quoted 8 lines · Randy Dunlap
Quoted 5 lines · On Sat, 03 Feb 2007 02:13:44 +0100 Richard Knutsson wrote:
Hello all
Just took a look at
abs()and can not understand why it seem so overcomplicated. Why not just:
Quoted 9 lines
Quoted 2 lines
#define abs(x) ((x) < 0) ? -(x) : (x))
It's almost that simple. It just adds a local
__xso that 'x' won't be evaluated multiple times as it would in the simpler version. Multiple evaluations (of an expression) (that could have side effects) would take more CPU horsepower and could have bad side effects as well.
You mean if x is ex a + b? In such case; why not use typeof instead of int? What kind of side-effects are you talking about?
Richard Knutsson
_______________________________________________
Kernel-janitors mailing list
[email protected]
https://lists.osdl.org/mailman/listinfo/kernel-janitors
Quoted 12 lines · On Sat, 03 Feb 2007 03:22:29 +0100 Richard Knutsson wrote:
Quoted 11 lines · Randy Dunlap
Quoted 7 lines · On Sat, 03 Feb 2007 02:13:44 +0100 Richard Knutsson wrote:
Hello all
Just took a look at
abs()and can not understand why it seem so overcomplicated. Why not just:#define abs(x) ((x) < 0) ? -(x) : (x))
Quoted 7 lines
Quoted 6 lines
It's almost that simple. It just adds a local
__xso that 'x' won't be evaluated multiple times as it would in the simpler version. Multiple evaluations (of an expression) (that could have side effects) would take more CPU horsepower and could have bad side effects as well.You mean if x is ex a + b? In such case; why not use typeof instead of int?
I suppose that would make sense if we have a need for abs(various types), but until we do, just using: int __x; is good enough.
Quoted 1 line
What kind of side-effects are you talking about?
The usual kind that affect computer software. E.g.:
a = abs(var1++); a = abs(fseek(File, -offset, whence));
but I don't want to limit the possibilities of side effects.
---
~Randy
_______________________________________________
Kernel-janitors mailing list
[email protected]
https://lists.osdl.org/mailman/listinfo/kernel-janitors
Quoted 24 lines · Randy Dunlap
Quoted 21 lines · On Sat, 03 Feb 2007 03:22:29 +0100 Richard Knutsson wrote:
Quoted 19 lines · Randy Dunlap
Quoted 8 lines · On Sat, 03 Feb 2007 02:13:44 +0100 Richard Knutsson wrote:
Hello all
Just took a look at
abs()and can not understand why it seem so overcomplicated. Why not just:#define abs(x) ((x) < 0) ? -(x) : (x))
It's almost that simple. It just adds a local
__xso that 'x' won't be evaluated multiple times as it would in the simpler version. Multiple evaluations (of an expression) (that could have side effects) would take more CPU horsepower and could have bad side effects as well.
Quoted 6 lines
Quoted 2 lines
You mean if x is ex a + b? In such case; why not use typeof instead of int?
I suppose that would make sense if we have a need for abs(various types), but until we do, just using: int
__x; is good enough.
Mm, true. Don't mess with something that is not broken (if not for the fun of it).
But that really leaves labs() hanging. I guess it was added because it is in gcc's stdlib.h.
Quoted 7 lines
Quoted 2 lines
What kind of side-effects are you talking about?
The usual kind that affect computer software. E.g.:
a = abs(var1++);
Oh thanks, good "eye-opener" :)
Thanks for clearing things up
Richard Knutsson
_______________________________________________
Kernel-janitors mailing list
[email protected]
https://lists.osdl.org/mailman/listinfo/kernel-janitors