From: Patricia Shanahan on 6 May 2010 08:52 Lie Ryan wrote: > 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. I perhaps should have pointed out that I think something like this does need to be commented. In addition to explaining what is happening, the comments should explain why it is necessary, unless that is obvious in context. Otherwise, some future programmer may be tempted to improve readability by going back to the original if-then-else structure. Patricia
From: Calum on 7 May 2010 12:48 On May 5, 9:30 am, Ikke <i...(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 > > 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: n...(a)netfront.net --- Tempting though it would be to write something like (f<<1)-1, you should resist. You should avoid hard-coded constants in code. Ideally you should also favour enumerations instead of integers, so that the compiler can catch errors when you use one instead of the other (depending on the language of course), and discourages use of numerical hacks which could so easily break if the vendor of your library decides to add more enumerations or change the values in between versions. A well written library would have defined constants for the return codes. That should apply to your own code as well as the vendors. You might be tempted with a look-up table, but this is also error- prone if the vendor returns values beyond the bounds of the array. Boring though it is, your best bet is code like this: switch( function() ) { case S_OK: return MY_SUCCESS; case E_FAILURE: return MY_FAILURE; default: raise error in some way, or also return MY_FAILURE depending on the circumstances } Actually, in one set of coding guidelines I've had to write to, all cases need a default clause to catch unexpected values.
From: Pascal J. Bourguignon on 7 May 2010 20:43 Keith Thompson <kst-u(a)mib.org> writes: > 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? He doesn't want it in one _statement_, he wants it in an expression. The obvious solution is that instead of writing (if (zerop function) (setf value -1) (setf value 1)) you just factorize the setf value: (setf value (if (zerop function) -1 1)) Of course if you program in assembler, it's harder, and you have more editing to do to go from: if(0==function){ value=-1; }else{ value=1; } to value=(0==function)?-1:1; Now, the real why is why would anybody try to program anything in BrainFuck^W a language that doesn't have "ternary operators"? >> 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? For example, one reason why you want to do that is to avoid breaking the functionnal flow. In mathematics, you combine your functions as you want: f: N --> N x |-> 0 if x is odd 1 otherwise. n: N --> N x |-> -1 if x==0 1 otherwise. g: N --> N x |-> x*3 y=g(n(f(x))) You don't stop in the middle of an equation like this: z=f(x) if z=0 then w=-1 else w=1 y=g(w) This is silly. Well in programming it is as silly as in mathematics. Therefore any programming languages that makes a distinction between expressions and statements should be killed. -- __Pascal Bourguignon__ http://www.informatimago.com
From: Lie Ryan on 7 May 2010 20:51 On 05/06/10 22:52, Patricia Shanahan wrote: > Lie Ryan wrote: >> 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. > > I perhaps should have pointed out that I think something like this does > need to be commented. > > In addition to explaining what is happening, the comments should explain > why it is necessary, unless that is obvious in context. Otherwise, some > future programmer may be tempted to improve readability by going back to > the original if-then-else structure. or worse, someone have just read the newest and fastest algorithm to solve linear equations and thought to replace the "linear equation solver" to the new and hot algorithms. More realistically, someone calling your function because he thought it's a linear equation solver. Never realizing that the original problem is actually "ret == 0 ? -1 : 1"[1], and a few weeks later, another programmer decided to revert the function to using if-else only to break the previous programmer's code. [1] btw, have anyone suggested trying: " ret == 0 and -1 or 1 " beware that this idiom has pitfalls when the "true-part" (in this case "-1") is considered false.
From: robin on 7 May 2010 21:21
"Jussi Piitulainen" <jpiitula(a)ling.helsinki.fi> wrote in message news:qotzl0ed9ht.fsf(a)ruuvi.it.helsinki.fi... | 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) The first is obviously better. It's simple. The second requires more code (two tests). |