From: bart.c on 5 May 2010 05:56 "Ikke" <ikke(a)hier.be> wrote in message news:Xns9D6F6AE5C8C12ikkehierbe(a)202.177.16.121... > 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... Apart from 2*function-1 as mentioned, also: value=table[function] where table[0]=-1 and table[1]=1 -- Bartc
From: rossum on 5 May 2010 07:44 On Wed, 5 May 2010 08:30:31 +0000 (UTC), Ikke <ikke(a)hier.be> 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 > How about: return (function() == 0) ? -1 : 1; rossum >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: Ikke on 5 May 2010 09:03 rossum <rossum48(a)coldmail.com> wrote in news:qem2u55bgqa5kmm74hi5qdj48lrergkkga(a)4ax.com: > How about: return (function() == 0) ? -1 : 1; I've thought about that, but ternairy operators are not supported in Delphi :( Thanks anyway! Ikke --- news://freenews.netfront.net/ - complaints: news(a)netfront.net ---
From: Keith Thompson on 5 May 2010 18:44 Ikke <ikke(a)hier.be> 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). 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... Seriously, where does the requirement to do it in one statement come from? -- Keith Thompson (The_Other_Keith) kst-u(a)mib.org <http://www.ghoti.net/~kst> Nokia "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"
From: Lie Ryan on 6 May 2010 07:08
On 05/05/10 18:51, Ikke wrote: > 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! be aware that the trick with linear equations quite severely harms readability. noone would be able to guess that its sole purpose is to turn 0 to -1. |