Re: Subtract smaller or larger number from x?
- From: Willem <willem@xxxxxxxx>
- Date: Wed, 29 Mar 2006 12:01:24 +0000 (UTC)
eksamor@xxxxxxxxx wrote:
) I have a number x. I would now like to find the difference between this
) number x and another number y.
)
) But y can be either larger or smaller than x. Currently I check if the
) number y is greater or smaller than x:
)
) int diff; // the difference between x and y
)
) if (y > x){
) diff = y-x;
) }else{
) diff = x-y;
) }
)
) is there not a more elegant way to do this when I would like the
) difference to be a positive value??
That rather depends on what you mean by elegant.
For example, a mathematician would want to keep as close to
the/a mathematical definition of 'difference' as possible.
A low-level (assembly) hacker might want to eliminate conditionals.
I'm sure there are more meanings of 'elegant'.
SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
.
- References:
- Subtract smaller or larger number from x?
- From: eksamor
- Subtract smaller or larger number from x?
- Prev by Date: Yacc = Bison???
- Next by Date: Re: Subtract smaller or larger number from x?
- Previous by thread: Subtract smaller or larger number from x?
- Next by thread: Re: Subtract smaller or larger number from x?
- Index(es):
Relevant Pages
|