POV-Ray : Newsgroups : povray.off-topic : Unexpected Server Time
12 Oct 2026 01:42:25 EDT (-0400)
  Unexpected (Message 1 to 30 of 30)  
From: Orchid Win7 v1
Subject: Unexpected
Date: 17 Aug 2013 05:20:50
Message: <520f4072$1@news.povray.org>
OK, well I never expected to end up on the *other* side of an interview 
table. o_O

Basically one of the guys had to leave the office suddenly, meaning that 
I was the most senior developer left. (Hey, there's only 4 of us to 
start with!) So, on about twenty minutes' notice, I had to go into an 
interview. Looking like crap, because it's Friday afternoon. :-.

I forget how long the interview was, but my boss apparently made his 
mind up within about five minutes. (Not that he said that at the time, 
of course.)

As part of the interview process, my boss likes to get people to 
implement the absolute function in C#. That is, he got out some paper, 
scribbled

   int abs(int input)
   {

and handed it to the guy. (He usually ends up having to explain what an 
"absolute function" is, but that's fair enough.) The objective is not so 
much to have perfect syntax, but to write something that is logically sound.

On this day, the applicant came up with this:

   int abs(int input)
   {
     int value = 0;
     for (int n = input; n != 0; n++)
     {
       value++;
     }
     return value;
   }

The guy explained how it works, and my boss just stared at him in 
disbelief, and then explained "that's the most bizarre answer I've ever 
seen!"

Perhaps slightly frighteningly, this function *actually works* - for 
negative inputs, at least. If you feed it -7, then the loop counter 
counts upwards from -7 all the way to zero, and "value" at the same time 
counts from 0 up to +7, which is the value returned.

There's a final twist. Because the loop condition is "!= 0" rather than 
"< 0" or similar, if the input is positive to start with, the function 
goes into an infinite loop...

...except that it doesn't. It locks up for a few minutes, AND THEN 
RETURNS -7! O_O

We stared at each other in disbelief, and then realised that the loop 
goes around roughly 2^31 times and then the counter overflows, becoming 
negative. The other counter similarly overflows, eventually resulting in 
the -7 we saw output.

In short, this isn't abs(), this is negate()! And it actually works! (If 
by "works" you accept solutions which are absurdly slow.) Note that if 
you did, say, "n <= 0" then you would have an off-by-one bug resulting 
in the wrong answer. Yet this guy managed (by skill or luck) to avoid 
that pitfall. Without a PC to check it with!

The guy spent so long desperately trying to wrap his brain around how to 
negate a number, that he completely forgot the possibility of the number 
being positive to start with. When we pointed this out, he then spent 
another several minutes trying to figure out how you could perhaps tell 
if a number was positive. He spent ages trying to think up what the name 
of the standard function for that is...

That's pretty special. But my boss told me afterwards it's the most 
bizarre code he's ever seen. *Clearly* he hasn't seen the more 
obfuscated possibilities of logic programming or functional 
programming... I may have to dig up some examples.

(My boss has a standard set of ways to implement abs(). They include:

* if (x < 0) return 0-x; else return x;

* return sqrt(x*x); // Evil.

* return str.Remove('-'); // String munging. Very evil!

* if (x < 0) return x ^ 0x8000; else return x; // DOESN'T ACTUALLY WORK!

I'm sure there must be more evil ways of doing this...)


Post a reply to this message

From: Warp
Subject: Re: Unexpected
Date: 17 Aug 2013 12:56:58
Message: <520fab5a@news.povray.org>
Orchid Win7 v1 <voi### [at] devnull> wrote:
>    int abs(int input)
>    {
>      int value = 0;
>      for (int n = input; n != 0; n++)
>      {
>        value++;
>      }
>      return value;
>    }

Having been a teaching assistant at the computing science lab, I got to
see some pretty weird programs made by students.

For example, you wouldn't believe how complicated calculating the length
of a 2D vector can become in the hands of an inexperienced programmer.
(This should be a literal one-liner, yet even 100 lines of code were
regularly broken just to do this simple thing.)

> * return sqrt(x*x); // Evil.

Many C and C++ compilers will actually optimize that to abs(x). I wouldn't
be surprised if a C# compiler wouldn't do the same...

-- 
                                                          - Warp


Post a reply to this message

From: Orchid Win7 v1
Subject: Re: Unexpected
Date: 17 Aug 2013 13:56:37
Message: <520fb955$1@news.povray.org>
On 17/08/2013 05:56 PM, Warp wrote:
> Having been a teaching assistant at the computing science lab, I got to
> see some pretty weird programs made by students.
>
> For example, you wouldn't believe how complicated calculating the length
> of a 2D vector can become in the hands of an inexperienced programmer.
> (This should be a literal one-liner, yet even 100 lines of code were
> regularly broken just to do this simple thing.)

Hmm, yes. I'm just trying to visualise that now... What would I do if I 
had no idea what I'm doing? (I guess the question is, how many of these 
programs ever *worked*?)

>> * return sqrt(x*x); // Evil.
>
> Many C and C++ compilers will actually optimize that to abs(x). I wouldn't
> be surprised if a C# compiler wouldn't do the same...

Perhaps. That doesn't mean this is a good way to achieve that goal though.


Post a reply to this message

From: clipka
Subject: Re: Unexpected
Date: 17 Aug 2013 13:58:36
Message: <520fb9cc$1@news.povray.org>
Am 17.08.2013 11:20, schrieb Orchid Win7 v1:

>    int abs(int input)
>    {

...

> (My boss has a standard set of ways to implement abs(). They include:
>
> * if (x < 0) return 0-x; else return x;
>
> * return sqrt(x*x); // Evil.
>
> * return str.Remove('-'); // String munging. Very evil!
>
> * if (x < 0) return x ^ 0x8000; else return x; // DOESN'T ACTUALLY WORK!
>
> I'm sure there must be more evil ways of doing this...)

He should also be prepared for

     return Math.Abs(input);

possibly with an added comment to the effect that the abs(int) function 
should be refactored away at the earliest possible opportunity ;-)


Post a reply to this message

From: Samuel Benge
Subject: Re: Unexpected
Date: 17 Aug 2013 15:20:00
Message: <web.520fcbbc825e3e9f8cb76ff30@news.povray.org>
Orchid Win7 v1 <voi### [at] devnull> wrote:
> I'm sure there must be more evil ways of doing this...)

Here's one (probably not the best) way to do it:

return x*(1-2*(x<0));

Not sure how C# handles types, but in C that would return an int.

Thanks for the laughs, btw :) When Haskell isn't the main subject, you're
actually pretty funny ;)

Sam


Post a reply to this message

From: Samuel Benge
Subject: Re: Unexpected
Date: 17 Aug 2013 15:30:01
Message: <web.520fce4c825e3e9fde4684940@news.povray.org>
"Samuel Benge" <stb### [at] hotmailcom> wrote:
>
> return x*(1-2*(x<0));

Oops, that should be

return x*(1-2*int(x<0));

I just woke up :\

It blows me away that many of the job applicants must be told what an absolute
function is o_O


Post a reply to this message

From: Warp
Subject: Re: Unexpected
Date: 18 Aug 2013 03:50:14
Message: <52107cb6@news.povray.org>
Orchid Win7 v1 <voi### [at] devnull> wrote:
> On 17/08/2013 05:56 PM, Warp wrote:
> > Having been a teaching assistant at the computing science lab, I got to
> > see some pretty weird programs made by students.
> >
> > For example, you wouldn't believe how complicated calculating the length
> > of a 2D vector can become in the hands of an inexperienced programmer.
> > (This should be a literal one-liner, yet even 100 lines of code were
> > regularly broken just to do this simple thing.)

> Hmm, yes. I'm just trying to visualise that now... What would I do if I 
> had no idea what I'm doing? (I guess the question is, how many of these 
> programs ever *worked*?)

Mind you, these people generally knew the pythagorean formula to do the
job (after all, they were university students.) They still managed to
break the 100-line mark.

-- 
                                                          - Warp


Post a reply to this message

From: Orchid Win7 v1
Subject: Re: Unexpected
Date: 18 Aug 2013 04:45:33
Message: <521089ad@news.povray.org>
On 17/08/2013 06:58 PM, clipka wrote:
> He should also be prepared for
>
> return Math.Abs(input);
>
> possibly with an added comment to the effect that the abs(int) function
> should be refactored away at the earliest possible opportunity ;-)

Heh, yeah. I think when an interviewer asks how to implement a standard 
library function, and you call the standard library function, you're 
probably not going to get mad props for that. ;-)


Post a reply to this message

From: Orchid Win7 v1
Subject: Re: Unexpected
Date: 18 Aug 2013 04:47:13
Message: <52108a11$1@news.povray.org>
On 17/08/2013 08:15 PM, Samuel Benge wrote:
> Orchid Win7 v1<voi### [at] devnull>  wrote:
>> I'm sure there must be more evil ways of doing this...)
>
> Here's one (probably not the best) way to do it:
>
> return x*(1-2*(x<0));
>
> Not sure how C# handles types, but in C that would return an int.

Well, I mean, the "obvious" way is

   if (x<0) return -x; else return x;

But you can phrase that as

   return x ? x : -x;

if you prefer.

> Thanks for the laughs, btw :) When Haskell isn't the main subject, you're
> actually pretty funny ;)

If you wanted to find crazy ways to do this in Haskell, you would have 
some pretty scary options available... ;-)


Post a reply to this message

From: Orchid Win7 v1
Subject: Re: Unexpected
Date: 18 Aug 2013 04:50:49
Message: <52108ae9$1@news.povray.org>
>> Hmm, yes. I'm just trying to visualise that now... What would I do if I
>> had no idea what I'm doing? (I guess the question is, how many of these
>> programs ever *worked*?)
>
> Mind you, these people generally knew the pythagorean formula to do the
> job (after all, they were university students.) They still managed to
> break the 100-line mark.

OK, ouch. Pythagoras and you're *still* getting this wrong??

Mind you, I recall an incident at university where our lecturer wrote 
the Shannon channel theorem on the board, and everyone was like "woah, 
what the heck does 'log' mean?" And the guy was like "oh, I'm sorry, I 
just expected that 3rd year computer science students would know what a 
logarithm is."

It turns out that only two people in the room knew what that actually 
is. One of them was obviously me. Weirdly, the other one was the crazy 
Mancunian rugby lout. It's the first (and last) time I ever saw him say 
something sensible. And it wasn't like he had a vague idea... he seemed 
to actually know what he was talking about. I guess there really *is* 
more to that guy than I first imagined.


Post a reply to this message

From: clipka
Subject: Re: Unexpected
Date: 18 Aug 2013 06:20:59
Message: <5210a00b@news.povray.org>
Am 18.08.2013 09:50, schrieb Warp:
> Orchid Win7 v1 <voi### [at] devnull> wrote:
>> On 17/08/2013 05:56 PM, Warp wrote:
>>> Having been a teaching assistant at the computing science lab, I got to
>>> see some pretty weird programs made by students.
>>>
>>> For example, you wouldn't believe how complicated calculating the length
>>> of a 2D vector can become in the hands of an inexperienced programmer.
>>> (This should be a literal one-liner, yet even 100 lines of code were
>>> regularly broken just to do this simple thing.)
>
>> Hmm, yes. I'm just trying to visualise that now... What would I do if I
>> had no idea what I'm doing? (I guess the question is, how many of these
>> programs ever *worked*?)
>
> Mind you, these people generally knew the pythagorean formula to do the
> job (after all, they were university students.) They still managed to
> break the 100-line mark.

That /is/ scary - then again, maybe they didn't know the sqrt() function?


Post a reply to this message

From: Bald Eagle
Subject: Re: Unexpected
Date: 18 Aug 2013 07:00:00
Message: <web.5210a922825e3e9f2c424ebe0@news.povray.org>
> > Mind you, these people generally knew the pythagorean formula to do the
> > job (after all, they were university students.) They still managed to
> > break the 100-line mark.

That's what happens when you're used to programming in Goldberg on a
Governmentium processor.


Post a reply to this message

From: Warp
Subject: Re: Unexpected
Date: 18 Aug 2013 08:27:58
Message: <5210bdce@news.povray.org>
Orchid Win7 v1 <voi### [at] devnull> wrote:
> But you can phrase that as

>    return x ? x : -x;

No, you can't.

Don't blame others for doing mistakes if you are doing them yourself. ;)

-- 
                                                          - Warp


Post a reply to this message

From: Warp
Subject: Re: Unexpected
Date: 18 Aug 2013 08:31:59
Message: <5210bebf@news.povray.org>
clipka <ano### [at] anonymousorg> wrote:
> That /is/ scary - then again, maybe they didn't know the sqrt() function?

The most common problem was that, for some reason, they thought that
you had to calculate the formula differently for positive numbers than
for negative numbers. And since there are four possible combinations of
signs for the two parameters... (Even then you would think that it would
take like 8 lines: Four conditionals and four instances of the same
pythagorean formula. But no, it couldn't be that simple either. You see,
you have to first remove the sign in order to do the pythagorean theorem
with positive numbers only... which kind of reminds me of the abs() problem
referred to by Andrew. I don't even remember anymore how they managed to
get so many lines of code.)

I never quite understood what made them think that you can only calculate
the pythagorean formula with positive values. One would think that they
know that yes, you can raise a negative value to the power of two and that
the result will be positive, but seemingly it's not that simple.

The hilarious (or sad, depending on your persective) thing about it was
that they pretty much ended up writing the same code four times in their
implementation, inside four conditionals, without realizing it.

-- 
                                                          - Warp


Post a reply to this message

From: clipka
Subject: Re: Unexpected
Date: 18 Aug 2013 08:43:30
Message: <5210c172$1@news.povray.org>
Am 18.08.2013 14:27, schrieb Warp:
> Orchid Win7 v1 <voi### [at] devnull> wrote:
>> But you can phrase that as
>
>>     return x ? x : -x;
>
> No, you can't.
>
> Don't blame others for doing mistakes if you are doing them yourself. ;)

It's a creative way of writing "return x" though ;-)


Post a reply to this message

From: Warp
Subject: Re: Unexpected
Date: 18 Aug 2013 09:23:49
Message: <5210cae4@news.povray.org>
clipka <ano### [at] anonymousorg> wrote:
> Am 18.08.2013 14:27, schrieb Warp:
> > Orchid Win7 v1 <voi### [at] devnull> wrote:
> >> But you can phrase that as
> >
> >>     return x ? x : -x;
> >
> > No, you can't.
> >
> > Don't blame others for doing mistakes if you are doing them yourself. ;)

> It's a creative way of writing "return x" though ;-)

Out of curiosity: Does C#, like C/C++, consider a non-boolean value as
a valid boolean (where a boolean is expected)?

I know that there are some languages that don't, but on the other hand.
C# is kind of in the C family of languages...

-- 
                                                          - Warp


Post a reply to this message

From: clipka
Subject: Re: Unexpected
Date: 18 Aug 2013 09:50:12
Message: <5210d114$1@news.povray.org>
Am 18.08.2013 15:23, schrieb Warp:
> clipka <ano### [at] anonymousorg> wrote:
>> Am 18.08.2013 14:27, schrieb Warp:
>>> Orchid Win7 v1 <voi### [at] devnull> wrote:
>>>> But you can phrase that as
>>>
>>>>      return x ? x : -x;
>>>
>>> No, you can't.
>>>
>>> Don't blame others for doing mistakes if you are doing them yourself. ;)
>
>> It's a creative way of writing "return x" though ;-)
>
> Out of curiosity: Does C#, like C/C++, consider a non-boolean value as
> a valid boolean (where a boolean is expected)?

Me I dunno. Though, thinking about it, Microsoft did various things 
right with C#, so I suspect it won't accept non-booleans there.

> I know that there are some languages that don't, but on the other hand.
> C# is kind of in the C family of languages...

So is Java, but even that insists on proper booleans for tests.


Post a reply to this message

From: Orchid Win7 v1
Subject: Re: Unexpected
Date: 18 Aug 2013 10:40:34
Message: <5210dce2@news.povray.org>
On 18/08/2013 01:27 PM, Warp wrote:
> Orchid Win7 v1<voi### [at] devnull>  wrote:
>> But you can phrase that as
>
>>     return x ? x : -x;
>
> No, you can't.
>
> Don't blame others for doing mistakes if you are doing them yourself. ;)

The guy took about 6 minutes to come up with the answer illustrated. My 
problem was in only spending 6 seconds. :-/


Post a reply to this message

From: Warp
Subject: Re: Unexpected
Date: 18 Aug 2013 10:42:49
Message: <5210dd68@news.povray.org>
Orchid Win7 v1 <voi### [at] devnull> wrote:
> On 18/08/2013 01:27 PM, Warp wrote:
> > Orchid Win7 v1<voi### [at] devnull>  wrote:
> >> But you can phrase that as
> >
> >>     return x ? x : -x;
> >
> > No, you can't.
> >
> > Don't blame others for doing mistakes if you are doing them yourself. ;)

> The guy took about 6 minutes to come up with the answer illustrated. My 
> problem was in only spending 6 seconds. :-/

The problem is that the answer above doesn't work.

-- 
                                                          - Warp


Post a reply to this message

From: Orchid Win7 v1
Subject: Re: Unexpected
Date: 18 Aug 2013 10:43:02
Message: <5210dd76$1@news.povray.org>
On 18/08/2013 02:23 PM, Warp wrote:
> Out of curiosity: Does C#, like C/C++, consider a non-boolean value as
> a valid boolean (where a boolean is expected)?

No.

(I just had to look that up.)

Apparently C# does not support implicit (or even explicit) conversion 
between bool and any other type.

Which is weird, because this is the exact same programming language 
where if you create an enum, it's actually just an int, as far as the 
type system is concerned...

(Oh, but bool isn't an enum. Naturally.)


Post a reply to this message

From: Orchid Win7 v1
Subject: Re: Unexpected
Date: 18 Aug 2013 10:44:35
Message: <5210ddd3$1@news.povray.org>
>>>> But you can phrase that as
>>>
>>>>      return x ? x : -x;
>>>
>>> No, you can't.
>>>
>>> Don't blame others for doing mistakes if you are doing them yourself. ;)
>
>> The guy took about 6 minutes to come up with the answer illustrated. My
>> problem was in only spending 6 seconds. :-/
>
> The problem is that the answer above doesn't work.

Yes, because I typed it too fast. What I meant to say was

   return (x >= 0) ? x : -x;

which works as expected.


Post a reply to this message

From: Samuel Benge
Subject: Re: Unexpected
Date: 18 Aug 2013 15:25:00
Message: <web.52111f00825e3e9f51343230@news.povray.org>
Orchid Win7 v1 <voi### [at] devnull> wrote:
> On 17/08/2013 08:15 PM, Samuel Benge wrote:
> > return x*(1-2*(x<0));
>
> Well, I mean, the "obvious" way is
>
>    if (x<0) return -x; else return x;

Obviously.

The method I posted isn't the "scariest" or "craziest," I'll admit. It would be
hard to beat the job applicants' answers, what with their hapless ingenuity and
all.

> > Thanks for the laughs, btw :) When Haskell isn't the main subject, you're
> > actually pretty funny ;)
>
> If you wanted to find crazy ways to do this in Haskell, you would have
> some pretty scary options available... ;-)

If you're trying to convince me Haskell is somehow worth my time, you're not
succeeding :-P


Post a reply to this message

From: Orchid Win7 v1
Subject: Re: Unexpected
Date: 18 Aug 2013 17:26:24
Message: <52113c00$1@news.povray.org>
>> If you wanted to find crazy ways to do this in Haskell, you would have
>> some pretty scary options available... ;-)
>
> If you're trying to convince me Haskell is somehow worth my time, you're not
> succeeding :-P

Nah, I've pretty much given up on anybody ever believing that one.


Post a reply to this message

From: scott
Subject: Re: Unexpected
Date: 19 Aug 2013 05:10:23
Message: <5211e0ff$1@news.povray.org>
> On this day, the applicant came up with this:
>
>    int abs(int input)
>    {
>      int value = 0;
>      for (int n = input; n != 0; n++)
>      {
>        value++;
>      }
>      return value;
>    }

That's a pretty epic fail. The rest of the interview must have been fun :-)


Post a reply to this message

From: Orchid Win7 v1
Subject: Re: Unexpected
Date: 19 Aug 2013 13:50:21
Message: <52125add$1@news.povray.org>
On 19/08/2013 10:10 AM, scott wrote:
>> On this day, the applicant came up with this:
>>
>> int abs(int input)
>> {
>> int value = 0;
>> for (int n = input; n != 0; n++)
>> {
>> value++;
>> }
>> return value;
>> }
>
> That's a pretty epic fail.

As I say, the impressive thing is that it's a correct (yet inefficient) 
implementation of negate().

> The rest of the interview must have been fun :-)

My boss was particularly amused by the time the guy said he read 
something about design patterns, and when we asked which book, he 
couldn't remember, but he said it was "the one with the Singleton 
pattern in it".

When asked for an example of a design pattern, he fumblingly described 
something which sounded exactly like a switch block.


Post a reply to this message

From: Warp
Subject: Re: Unexpected
Date: 19 Aug 2013 15:01:16
Message: <52126b7c@news.povray.org>
Orchid Win7 v1 <voi### [at] devnull> wrote:
> > That's a pretty epic fail.

> As I say, the impressive thing is that it's a correct (yet inefficient) 
> implementation of negate().

Out of curiosity, I wanted to see what gcc would produce from that code
(when written in C++). In other words this:

//-----------------------------------------
int a(int input)
{
    int value = 0;
    for(int n = input; n != 0; ++n)
        ++value;
    return value;
}
//-----------------------------------------

It produces this, in asm:

//-----------------------------------------
.LFB0:
        .cfi_startproc
        movl    %edi, %edx
        xorl    %eax, %eax
        negl    %edx
        testl   %edi, %edi
        cmovne  %edx, %eax
        ret
        .cfi_endproc
//-----------------------------------------

If we "disassemble" that back to C++, it's basically equivalent to:

//-----------------------------------------
int a(int input)
{
    return input == 0 ? 0 : -input;
}
//-----------------------------------------

For some reason gcc produces a needless test against zero, but
otherwise it's rather impressive that it got rid of the loop completely.

-- 
                                                          - Warp


Post a reply to this message

From: Warp
Subject: Re: Unexpected
Date: 19 Aug 2013 15:33:57
Message: <52127325@news.povray.org>
Warp <war### [at] tagpovrayorg> wrote:
> If we "disassemble" that back to C++, it's basically equivalent to:

> //-----------------------------------------
> int a(int input)
> {
>     return input == 0 ? 0 : -input;
> }
> //-----------------------------------------

Btw, clang is able to optimize it to an outright "return -input;"

-- 
                                                          - Warp


Post a reply to this message

From: Orchid Win7 v1
Subject: Re: Unexpected
Date: 19 Aug 2013 17:05:03
Message: <5212887f$1@news.povray.org>
On 19/08/2013 08:01 PM, Warp wrote:
> If we "disassemble" that back to C++, it's basically equivalent to:
>
> //-----------------------------------------
> int a(int input)
> {
>      return input == 0 ? 0 : -input;
> }
> //-----------------------------------------
>
> For some reason gcc produces a needless test against zero, but
> otherwise it's rather impressive that it got rid of the loop completely.

That *is* pretty impressive, actually... I wonder how on Earth it 
figures that out. I doubt anybody would have put a special case into the 
compiler just for this, so I wonder how it figures it out...


Post a reply to this message

From: scott
Subject: Re: Unexpected
Date: 20 Aug 2013 10:33:16
Message: <52137e2c$1@news.povray.org>
>> int abs(int input)
>>> {
>>> int value = 0;
>>> for (int n = input; n != 0; n++)
>>> {
>>> value++;
>>> }
>>> return value;
>>> }
>>
>> That's a pretty epic fail.
>
> As I say, the impressive thing is that it's a correct (yet inefficient)
> implementation of negate().

Yes, there must be a prize for that somewhere, but presumably not one 
that involves working at your company :-) I wonder what would have 
happened if you'd asked him to implement an "add" function...?


Post a reply to this message

From: clipka
Subject: Re: Unexpected
Date: 20 Aug 2013 11:30:46
Message: <52138ba6@news.povray.org>
Am 20.08.2013 16:33, schrieb scott:
>>> int abs(int input)
>>>> {
>>>> int value = 0;
>>>> for (int n = input; n != 0; n++)
>>>> {
>>>> value++;
>>>> }
>>>> return value;
>>>> }
>>>
>>> That's a pretty epic fail.
>>
>> As I say, the impressive thing is that it's a correct (yet inefficient)
>> implementation of negate().
>
> Yes, there must be a prize for that somewhere,

http://www.ioccc.org/

(Okay, this is for C code, not C#, but it should be easy enough to port 
the function)


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.