From: Ikke on
Hi everybody,

There's a function that basically returns an integer, either 0 or 1, but
I'd like it to return -1 or 1. Obviously I can't alter the function itself,
otherwise there wouldn't be a problem.

The simplest solution would be to just check for the value with an if
statement, like so:
if function = 0 then
value = -1
else
value = 1

But I'd like to do it in one statement (don't ask why). Is there a way to
convert both values (0 or 1) to -1 and 1 respectively, perhaps using some
maths? I can't seem to find a way...

Thanks in advance,

Ikke!

--- news://freenews.netfront.net/ - complaints: news(a)netfront.net ---
From: Jussi Piitulainen on
Ikke writes:

> There's a function that basically returns an integer, either 0 or 1,
> but I'd like it to return -1 or 1. Obviously I can't alter the
> function itself, otherwise there wouldn't be a problem.
>
> The simplest solution would be to just check for the value with an
> if statement, like so:
> if function = 0 then
> value = -1
> else
> value = 1
>
> But I'd like to do it in one statement (don't ask why). Is there a
> way to convert both values (0 or 1) to -1 and 1 respectively,
> perhaps using some maths? I can't seem to find a way...

Since 2*0 - 1 == -1 and 2*1 - 1 == 1, you can do simple arithmetic:

value = 2*function - 1

Or if False is treated as 0 and True as 1, which I think they are but
am too lazy to check, you can do the Iverson:

value = (function == 1) - (function == 0)
From: Patricia Shanahan on
Ikke wrote:
> Hi everybody,
>
> There's a function that basically returns an integer, either 0 or 1, but
> I'd like it to return -1 or 1. Obviously I can't alter the function itself,
> otherwise there wouldn't be a problem.
>
> The simplest solution would be to just check for the value with an if
> statement, like so:
> if function = 0 then
> value = -1
> else
> value = 1
>
> But I'd like to do it in one statement (don't ask why). Is there a way to
> convert both values (0 or 1) to -1 and 1 respectively, perhaps using some
> maths? I can't seem to find a way...

value = 2 * function - 1

I got this by solving a couple of linear equations. In effect, you are
asking for a function f such that f(0) is -1 and f(1) is 1. Given only
two points, one of the possibilities for f is a straight line a*x+b
through those points.

Patricia

From: Jussi Piitulainen on
Jussi Piitulainen writes:

> Or if False is treated as 0 and True as 1, which I think they are
> but am too lazy to check, you can do the Iverson:
>
> value = (function == 1) - (function == 0)

Sorry, I thought I was in another newsgroup: it depends on the
programming language whether that language treats truth values
as 0 and 1.
From: Ikke on
Jussi Piitulainen <jpiitula(a)ling.helsinki.fi> wrote in
news:qotvdb2d9cm.fsf(a)ruuvi.it.helsinki.fi:

> Jussi Piitulainen writes:
>
>> Or if False is treated as 0 and True as 1, which I think they are
>> but am too lazy to check, you can do the Iverson:
>>
>> value = (function == 1) - (function == 0)
>
> Sorry, I thought I was in another newsgroup: it depends on the
> programming language whether that language treats truth values
> as 0 and 1.

Never mind - your earlier suggested solution was perfect!

Thanks, and thanks Patricia!

Ikke

--- news://freenews.netfront.net/ - complaints: news(a)netfront.net ---