Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
gonadic io
Feb 16, 2011

>>=

qntm posted:

Python code:
def isEven(n):
    return True if n == 0 else isOdd(n - 1)

def isOdd(n):
    return False if n == 0 else isEven(n - 1)

the Peano approach

Adbot
ADBOT LOVES YOU

omeg
Sep 3, 2012

code:
 * @parm notify_offset The byte offset in the page to use for unmap notification; -1 for none.

uint32_t notify_offset,
I too love magic parameter values.

Hughlander
May 11, 2005

omeg posted:

code:
 * @parm notify_offset The byte offset in the page to use for unmap notification; -1 for none.

uint32_t notify_offset,

I too love magic parameter values.

I love an unsigned int having a signed magic number.

ChickenWing
Jul 22, 2010

:v:

Biowarfare posted:

they were right though



no no no no no no no no no no no no no no no no no no no no no :psyboom:

canis minor
May 4, 2011

ChickenWing posted:

no no no no no no no no no no no no no no no no no no no no no :psyboom:

My next favourite thing, after JS/Java, is - "Have you programmed in MVC? How many applications have you written in MVC?"

(it might be pedantic, but MVC is not a programming language, goddamit)

canis minor fucked around with this message at 16:20 on May 14, 2015

Polio Vax Scene
Apr 5, 2009



I'm an expert in Microsoft.

Excel?

No, Microsoft.

New Yorp New Yorp
Jul 18, 2003

Only in Kenya.
Pillbug

Manslaughter posted:

I'm an expert in Microsoft.

Excel?

No, Microsoft.

My Adobe doesn't work.

Polio Vax Scene
Apr 5, 2009



Maybe you should pay it a living wage.

FlapYoJacks
Feb 12, 2009

Ithaqua posted:

My Adobe doesn't work.

It only works on java se 6.dailyReleaseBuild32.4

1337JiveTurkey
Feb 17, 2005

Manslaughter posted:

I'm an expert in Microsoft.

Excel?

No, Microsoft.

I'm such an expert in Microsoft that my email's BillyGIsMyHomeboy@microsoft.com

Pavlov
Oct 21, 2012

I've long been fascinated with how the alt-right develops elaborate and obscure dog whistles to try to communicate their meaning without having to say it out loud
Stepan Andreyevich Bandera being the most prominent example of that

QuarkJets posted:

I really liked Julia a lot until I started using Python's Numba module

Is numba any good? I feel like it's trying to do something to python that python just wasn't built to do.

zergstain
Dec 15, 2005

Dr. Stab posted:

code:
for(int i = INT_MIN; i<INT_MAX; i+=2){
    if (n == i) return true;		
}

I love how that will overflow if n is odd.

Space Kablooey
May 6, 2009


zergstain posted:

I love how that will overflow if n is odd.

code:
//isOdd
try{
 for(int i = INT_MIN; i<INT_MAX; i+=2) {}
} catch(OverflowException e) {
 return true;
}
return false;

Dessert Rose
May 17, 2004

awoken in control of a lucid deep dream...

zergstain posted:

I love how that will overflow if n is odd.

My favorite part is just what the gently caress is it even doing?

HappyHippo
Nov 19, 2003
Do you have an Air Miles Card?
code:
return (n & 0x0001) == 0
or maybe

code:
return (n >> 1 << 1) == n

HappyHippo fucked around with this message at 19:03 on May 14, 2015

Ruzihm
Aug 11, 2010

Group up and push mid, proletariat!


HappyHippo posted:

code:
return (n >> 1 << 1) == n
I like this one!

No Safe Word
Feb 26, 2005

ratbert90 posted:

It only works on java se 6.dailyReleaseBuild32.4

There is hardware I work with that has management software that only runs with Java 6 on IE 8 in Windows XP.

Not kidding.

Blotto Skorzany
Nov 7, 2008

He's a PSoC, loose and runnin'
came the whisper from each lip
And he's here to do some business with
the bad ADC on his chip
bad ADC on his chiiiiip

HardDisk posted:

code:
//isOdd
try{
 for(int i = INT_MIN; i<INT_MAX; i+=2) {}
} catch(OverflowException e) {
 return true;
}
return false;

Since signed integer overflow doesn't cause an exception anywhere that has INT_MIN rather than something like Integer.MIN_VALUE, that's still going to misbehave when n is odd. It's left as an exercise to the reader to determine whether the loop doesn't terminate in that case or gives the wrong answer.

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
Obviously there's an implied #define int boost::multiprecision::number<boost::multiprecision::cpp_int_backend<32, 32, boost::multiprecision::signed_magnitude, boost::multiprecision::checked>> and #define OverflowException std::overflow_error first.

Van Kraken
Feb 13, 2012

code:
def isEven(x):
    return abs(x) in range(0, abs(x) + 1, 2)

In Python 3 this is O(1), because range now has a custom __contains__ method and doesn't have to test every value to check for inclusion.

Space Kablooey
May 6, 2009


Blotto Skorzany posted:

Since signed integer overflow doesn't cause an exception anywhere that has INT_MIN rather than something like Integer.MIN_VALUE, that's still going to misbehave when n is odd. It's left as an exercise to the reader to determine whether the loop doesn't terminate in that case or gives the wrong answer.

What plorky said.

I thought the code was C# for some reason and didn't notice that it wasn't Integer.MIN_VALUE until now.

Or whatever it is in C#

Space Kablooey fucked around with this message at 19:53 on May 14, 2015

The MUMPSorceress
Jan 6, 2012


^SHTPSTS

Gary’s Answer
code:
;isodd
n(n)
n nn
q n#(+nn+(1=1*2))

The MUMPSorceress fucked around with this message at 20:28 on May 14, 2015

Linear Zoetrope
Nov 28, 2011

A hero must cook
code:
func IsOdd(n int) bool {
    fmt.Printf("Is %d odd?\n", n)
    var b bool
    fmt.Scanln(&b)

    return b
}

Fergus Mac Roich
Nov 5, 2008

Soiled Meat

qntm posted:

Python code:
def isEven(n):
    return True if n == 0 else isOdd(n - 1)

def isOdd(n):
    return False if n == 0 else isEven(n - 1)

This one is a lot of fun, I like it.

HappyHippo
Nov 19, 2003
Do you have an Air Miles Card?

Jsor posted:

code:
func IsOdd(n int) bool {
    fmt.Printf("Is %d odd?\n", n)
    var b bool
    fmt.Scanln(&b)

    return b
}

Had a good laugh at this one

Munkeymon
Aug 14, 2003

Motherfucker's got an
armor-piercing crowbar! Rigoddamndicu𝜆ous.



HappyHippo posted:

code:
return (n >> 1 << 1) == n

Try that with 2147483648 (231) in JS.

Qwertycoatl
Dec 31, 2008

code:
unsigned int is_even(unsigned int x)
{
    for (unsigned int i = 0; i < x; i++) {
        x *= x;
    }
    return !x;
}

VikingofRock
Aug 24, 2008




Munkeymon posted:

Try that with 2147483648 (231) in JS.

Wait, why does the right shift become negative?

Linear Zoetrope
Nov 28, 2011

A hero must cook

VikingofRock posted:

Wait, why does the right shift become negative?

Maybe it secretly executes a "mul 2" instead of something like shift right??

Vanadium
Jan 8, 2005

a right shift should probably make it do "div 2" and not "mul"

It's probably because 2<<31 is one past the biggest value you can fit into a signed 32bit int and the bitshifts work in terms of 32bit ints so it gets cast to one and it overflows and is suddenly int_min?

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

Vanadium posted:

a right shift should probably make it do "div 2" and not "mul"

It's probably because 2<<31 is one past the biggest value you can fit into a signed 32bit int and the bitshifts work in terms of 32bit ints so it gets cast to one and it overflows and is suddenly int_min?

Yeah, it's two's-complement representation. MDN's bitwise operators page may be helpful in working it out.

Linear Zoetrope
Nov 28, 2011

A hero must cook

Vanadium posted:

a right shift should probably make it do "div 2" and not "mul"

Er, derp. Not sure how I made that mistake

IT BEGINS
Jan 15, 2009

I don't know how to make analogies
I was digging for a bug today that was affecting a part of our invoicing system. The thing that grouped invoices together wasn't properly updating the totals, and every group of invoices was getting $0 as a total.

I ask the dev responsible for writing this code a couple years ago if he's seen the issue:

quote:

Have you seen this before?
Oh yeah it's happened before. We tried to debug it and couldn't figure it out.
Ok, what did you guys find?
We couldn't figure it out.
So how'd you solve it?
Oh I wrote this function, invoices_update. It calculates the correct total amount
:catstare:

Can't find the bug? Just write a loving function to fix it afterwards! Genius!

VikingofRock
Aug 24, 2008




Subjunctive posted:

Yeah, it's two's-complement representation. MDN's bitwise operators page may be helpful in working it out.

I'm still confused. From that link:

quote:

>> (Sign-propagating right shift)

This operator shifts the first operand the specified number of bits to the right. Excess bits shifted off to the right are discarded. Copies of the leftmost bit are shifted in from the left. Since the new leftmost bit has the same value as the previous leftmost bit, the sign bit (the leftmost bit) does not change. Hence the name "sign-propagating".

and yet:

JavaScript code:
 > 2147483648 >> 1
=> -1073741824

EDIT: Wait nevermind. 2^31 = 1000 0000 0000 0000 0000 0000 0000 0000, not 0100 0000 0000 0000 0000 0000 0000 0000. I get it now.

VikingofRock fucked around with this message at 00:07 on May 15, 2015

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

VikingofRock posted:

I'm still confused. From that link:


and yet:

JavaScript code:
 > 2147483648 >> 1
=> -1073741824


ECMA-262 posted:

11.7.2 The Signed Right Shift Operator ( >> )
Performs a sign-filling bitwise right shift operation on the left operand by the amount specified by the right operand.
The production ShiftExpression : ShiftExpression >> AdditiveExpression is evaluated as follows:
1. Evaluate ShiftExpression.
2. Call GetValue(Result(1)).
3. Evaluate AdditiveExpression.
4. Call GetValue(Result(3)).
5. Call ToInt32(Result(2)).
6. Call ToUint32(Result(4)).
7. Mask out all but the least significant 5 bits of Result(6), that is, compute Result(6) & 0x1F.
8. Perform sign-extending right shift of Result(5) by Result(7) bits. The most significant bit is
propagated. The result is a signed 32 bit integer.
9. Return Result(8).

9.5 ToInt32: (Signed 32 Bit Integer)
The operator ToInt32 converts its argument to one of 232 integer values in the range 231 through 231-1, inclusive. This operator functions as follows:
1. Call ToNumber on the input argument.
2. If Result(1) is NaN, +0, -0, +∞, or -∞, return +0.
3. Compute sign(Result(1)) * floor(abs(Result(1))).
4. Compute Result(3) modulo 2 ; that is, a finite integer value k of Number type with positive sign and less than 232 in magnitude such the mathematical difference of Result(3) and k is mathematically an integer multiple of 232.
5. If Result(4) is greater than or equal to 231, return Result(4) - 232, otherwise return Result(4).

Bognar
Aug 4, 2011

I am the queen of France
Hot Rope Guy

Qwertycoatl posted:

code:
unsigned int is_even(unsigned int x)
{
    for (unsigned int i = 0; i < x; i++) {
        x *= x;
    }
    return !x;
}

I think this is my favorite.

sarehu
Apr 20, 2007

(call/cc call/cc)
Holy poo poo, at first I thought it was broken for x < 5.

xtal
Jan 9, 2011

by Fluffdaddy
nm

xtal fucked around with this message at 00:51 on May 18, 2015

Carthag Tuek
Oct 15, 2005

Tider skal komme,
tider skal henrulle,
slægt skal følge slægters gang



[code]
class EnterpriseOddEvenProviderBeanFactoryImpl {

Adbot
ADBOT LOVES YOU

Pavlov
Oct 21, 2012

I've long been fascinated with how the alt-right develops elaborate and obscure dog whistles to try to communicate their meaning without having to say it out loud
Stepan Andreyevich Bandera being the most prominent example of that

Snapchat A Titty posted:

[code]
class EnterpriseOddEvenProviderBeanFactoryImpl {

AbstractSingletonProxyFactoryBean?

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply