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
Dnatural
Feb 19, 2004
Robocop's #1 Fan
if( Math.Abs( x - y ) < 0 )
{
...
}


In bizaro world where math.abs can return a non-absolute value.

Adbot
ADBOT LOVES YOU

Internet Janitor
May 17, 2008

"That isn't the appropriate trash receptacle."
Perhaps Math.abs() is modified at runtime to control the behavior of that code. :v:

Deep Dish Fuckfest
Sep 6, 2006

Advanced
Computer Touching


Toilet Rascal
You never know what future versions will do. Better safe than sorry.

pokeyman
Nov 26, 2006

That elephant ate my entire platoon.

YeOldeButchere posted:

You never know what future versions will do. Better safe than sorry.

I realize you're joking but surely an assertion would make more sense if that was the goal.

I've definitely seen assertions for e.g. C++ STL containers, like this:

code:
someVector.resize(5);
assert(someVector.size(), 5);
which is silly in my book but doesn't harm anyone.

Kelson
Jan 23, 2005

NotShadowStar posted:

Wow, people keep touting that Apple doesn't have problems with OSX because nobody has targeted it. I don't think that's the case, because Apple just uses standard unix permissions instead of weirdo crap like this. Finder doesn't run in a special privilege, it asks permission just like every singe other application.
Apple's soft core, despite being built on top of BSD/Darwin, dropped most of the security mechanisms. Permissions aren't really used internally, although they effect low level non-Darwin stuff. There are a number of root processes that run user commands without checking any permissions, and it gets worse when using the API.

Windows doesn't have many users who care about security, their priority tends towards performance and/or usability. Only company security officers really cares. CEOs tend to only after the company got owned. They don't blame the OS afterward though, since operating systems are hard to change (I guess?); they just place security devices around the OS. Microsoft is happy. Customers are happy. Attackers are happy. Yay!

At the end of the day, they're both oriented to serve their marginally technical customer base. That base doesn't want UAC prompts; they don't understand the threat posed anyhow. Unfortunately, that provides a pretty soft target for criminals, although OS hardening has significantly strengthened Windows (now it is only somewhat easy to pwn, vs trivial).

edit: clarified which SO/CEOs

Kelson fucked around with this message at 19:23 on Jan 29, 2011

RitualConfuser
Apr 22, 2008

Dnatural posted:

In bizaro world where math.abs can return a non-absolute value.
http://download.oracle.com/javase/6/docs/api/java/lang/Math.html#abs%28int%29

quote:

Note that if the argument is equal to the value of Integer.MIN_VALUE, the most negative representable int value, the result is that same value, which is negative.

So, not that crazy, in some languages.

The Reaganomicon
Oct 14, 2010

by Lowtax
No, I'm fairly certain that Java is a crazy language.

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe
Kind of disconcerting that the absolute value function could return a negative value. What do other languages do? (I mean languages in which the minimum representable integer is greater in absolute value than the maximum representable integer, obviously.)

TasteMyHouse
Dec 21, 2006

Hammerite posted:

(I mean languages in which the minimum representable integer is greater in absolute value than the maximum representable integer, obviously.)

AFAIK this should ALWAYS be the case (and is a function of hardware, not language)

Vanadium
Jan 8, 2005

Hammerite posted:

Kind of disconcerting that the absolute value function could return a negative value. What do other languages do? (I mean languages in which the minimum representable integer is greater in absolute value than the maximum representable integer, obviously.)

ghci> abs (minBound :: Int)
-9223372036854775808

< Vanadium> cout << std::abs(std::numeric_limits<int>::min())
< cout> -2147483648

The Reaganomicon
Oct 14, 2010

by Lowtax

TasteMyHouse posted:

AFAIK this should ALWAYS be the case (and is a function of hardware, not language)

Isn't the entire purpose of not coding in assembly defeated this way? I can definitely see fun things happening because someone assumed that abs would always be positive, given that the actual abs function is always positive. You know, in this reality. By definition.

TasteMyHouse
Dec 21, 2006

The Reaganomicon posted:

Isn't the entire purpose of not coding in assembly defeated this way? I can definitely see fun things happening because someone assumed that abs would always be positive, given that the actual abs function is always positive. You know, in this reality. By definition.

Its just kind of a difficult problem to solve. In two's complement, the most negative number HAS no positive representation. How would you propose to handle this? In C, the behavior is undefined... I personally would prefer having it be defined, so that if it actually is a problem in my code I can anticipate the exact case and handle it.

The Reaganomicon
Oct 14, 2010

by Lowtax

TasteMyHouse posted:

Its just kind of a difficult problem to solve. In two's complement, the most negative number HAS no positive representation. How would you propose to handle this? In C, the behavior is undefined... I personally would prefer having it be defined, so that if it actually is a problem in my code I can anticipate the exact case and handle it.

I understand the reasoning, but having a single edge case act in a way contrary to what you would normally expect seems to be a Bad Solution. They probably considered this when writing abs, but christ that's ugly.

Scaevolus
Apr 16, 2007

If you're calling abs(INT_MIN), something has probably gone wrong already.

TasteMyHouse
Dec 21, 2006

The Reaganomicon posted:

I understand the reasoning, but having a single edge case act in a way contrary to what you would normally expect seems to be a Bad Solution. They probably considered this when writing abs, but christ that's ugly.

I just don't know what you want to happen. 2's complement is built into the hardware, and it is a natural consequence of 2's complement that there is a value that sticks out in this way.

It is not possible for abs to return a value consistent with its normal behavior on this input.

and no one is going to throw out the advantages of 2'complement so that abs makes more sense

Edison was a dick
Apr 3, 2010

direct current :roboluv: only
The only logical outcome for abs(INT_MIN) would be for some form of exception.
I like INT_MIN, it's an edge case that is often forgotten. I've got calculator programs to segfault with it.

Internet Janitor
May 17, 2008

"That isn't the appropriate trash receptacle."
One solution might've been to shift INT_MIN up by one, making the range for negative numbers equal to the range for positive numbers. That has problems too- discarding that pesky value means you can't use a signed int to store an arbitrary 32-bit hex value for masking.

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean

Fun Shoe
I would have preferred the function throw an exception rather than return a negative number.

pixelpusherbot
Jun 3, 2009
code:
<html 
xmlns="http://www.w3.org/1999/xhtml" 
xmlns:dc="http://purl.org/dc/elements/1.1/" 
xmlns:dct="http://purl.org/dc/terms/" 
xmlns:fb="http://www.facebook.com/2008/fbml" 
xmlns:foaf="http://xmlns.com/foaf/0.1/" 
xmlns:media="http://search.yahoo.com/mrss/" 
xmlns:og="http://opengraphprotocol.org/schema/" 
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:vs="http://www.w3.org/2003/06/sw-vocab-status/ns#"
xml:lang="en-gb" 
version="XHTML+RDFa 1.0">
Who got the biggest?

NotShadowStar
Sep 20, 2000
Man I'm nowhere near it right at this moment but Drupal puts a metric fuckton of poo poo in HTML headers by default. I had never even heard of GRDDL until I dealt with Drupal. It's another bloated as gently caress specification from the crazy loving people at W3C who, instead of doing useful things like un-loving the DOM or working on HTML5 have been doing batshit stuff like this. It's at least a dozen tags and a few namespaces that Drupal puts in for GRDDL stuff, poo poo that they don't even put in the drupal.org home page.

Bonfire Lit
Jul 9, 2008

If you're one of the sinners who caused this please unfriend me now.

Vanadium posted:

< Vanadium> cout << std::abs(std::numeric_limits<int>::min())
< cout> -2147483648
Since the overloads have the same semantics as the C stdlib, and C99 explicitly states that the behaviour here is undefined, it might also have totally different results.

MEAT TREAT posted:

I would have preferred the function throw an exception rather than return a negative number.

The original code looks like C# to me, in which case: it does (System.OverflowException).

Bonfire Lit fucked around with this message at 14:34 on Jan 30, 2011

Malloc Voidstar
May 7, 2007

Fuck the cowboys. Unf. Fuck em hard.
Wanted to add a stemmer to a small program I'm writing. (A stemmer reduces words to their stem; "writing" becomes "write")

Used Apache's modification of the generated Java version of the Snowball Porter2 stemmer. (Snowball code here)

It worked fine, but then I decided to look at the Java code and :gonk:

The Snowball->Java compiler apparently writes everything as labeled while(true){} statements or do {} while (false); statements.
code:
                    v_9 = limit - cursor;
                    lab11: do {
                        // call Step_2, line 220
                        if (!r_Step_2())
                        {
                            break lab11;
                        }
                    } while (false);
                    cursor = limit - v_9;
                    // do, line 221
                    v_10 = limit - cursor;
                    lab12: do {
                        // call Step_3, line 221
                        if (!r_Step_3())
                        {
                            break lab12;
                        }
                    } while (false);
                    cursor = limit - v_10;
And sometimes doesn't understand the concept of arrays.
code:
        public boolean stem() {
            int v_1;
            int v_2;
            int v_3;
            int v_4;
            int v_5;
            int v_6;
            int v_7;
            int v_8;
            int v_9;
            int v_10;
            int v_11;
            int v_12;
            int v_13;
Pastebinned code

Qwertycoatl
Dec 31, 2008

Treating generated code as anything other than an object file is a horror itself. You're not supposed to read it, so it's fine for a generator to make life easy on itself with stuff like do { ....} while (false) and whatever.

Malloc Voidstar
May 7, 2007

Fuck the cowboys. Unf. Fuck em hard.

Qwertycoatl posted:

Treating generated code as anything other than an object file is a horror itself. You're not supposed to read it, so it's fine for a generator to make life easy on itself with stuff like do { ....} while (false) and whatever.
Apache modified it to eliminate reflection, and use an abstract class instead, so the real horror here is... Apache

Paul MaudDib
May 3, 2006

TEAM NVIDIA:
FORUM POLICE
code:
for ( eof = 0, skip = 0; ; )
{
    // read exactly one block or multiple
    for ( numchar = 0; numchar < blksize; numchar += n )
    {
        n = read( fdin, &buf[ numchar ], blksize - numchar );
        if ( n < 0)
            err_sys("read error");
        if ( n == 0 )
        {
            eof = 1;	// end of file
            break;	// if n = 0 terminates the execution the nearest loop
        }
    }

//check if we can skip this part 
if ( numchar == blksize )
{
    //linear search for a byte other than null 
    for ( n = 0; n < blksize; n++ )
        if ( buf[ n ] != null )
            break;
    if ( n == blksize )
    {
        skip += blksize;		//skip = skip + blksize
        continue;				// if n is equal to blksize  passes control
                            // to next iteration
    }
}

// lseek over the null bytes 
if ( skip != 0 )
{
    // keep one block if we got eof( to write the last block)
    if ( numchar == 0 )
    {
        skip -= blksize;		// skip = skip - blksize
        numchar += blksize;		// numchar = numchar + blksize
    }

    i = lseek( fdout, skip, SEEK_CUR );	// lseek the null bytes
    if ( i < 0 ) 
        err_sys("lseek error");
    skip = 0;
}

/* write exactly the number of characters */
for ( n = 0; n < numchar; n += i )		// jump the hole to cont 
{
    i = write(fdout, &buf[ n ], numchar - n );
    if ( i < 0)				// if i < 0 can't write
        err_sys("write error");
}

if ( eof ) 	//end of file
    break;
}
It's an assignment for an intro C class, use lseek to copy a file with holes in it (without expanding the file). Highlights: abuse of the for statement, use of the continue statement, I don't even know what the intention of the read loop is, and unnecessary use of flags. This person will have passed at least 2 semesters of C# and one semester of SPARC assembly.

Paul MaudDib fucked around with this message at 06:26 on Feb 1, 2011

Malloc Voidstar
May 7, 2007

Fuck the cowboys. Unf. Fuck em hard.
the comments

skip += blksize; //skip = skip + blksize

:negative:

mr_jim
Oct 30, 2006

OUT OF THE DARK

Student code is usually bad. It's when someone's getting paid to write it that makes it really a horror.



















But that code is very bad.

Scaramouche
Mar 26, 2001

SPACE FACE! SPACE FACE!

Well... at least the brackets and white space look nice?

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
The indentation is wrong, so no.

pseudorandom name
May 6, 2007

Plorkyeran posted:

The indentation is wrong, so no.

What, four spaces instead of a (8 space, as is right and proper) tab?

Malloc Voidstar
May 7, 2007

Fuck the cowboys. Unf. Fuck em hard.

pseudorandom name posted:

What, four spaces instead of a (8 space, as is right and proper) tab?
code:
for ( eof = 0, skip = 0; ; )
{
    for ( numchar = 0; numchar < blksize; numchar += n )
    {
        n = read( fdin, &buf[ numchar ], blksize - numchar );
        if ( n < 0)
            err_sys("read error");
        if ( n == 0 )
        {
            eof = 1;	// end of file
            break;	// if n = 0 terminates the execution the nearest loop
        }
    }
if ( numchar == blksize )
{ 
does this help demonstrate

pseudorandom name
May 6, 2007

Hah.

This rabbit hole doesn't end, does it?

Milotic
Mar 4, 2009

9CL apologist
Slippery Tilde

Paul MaudDib posted:

...use of the continue statement...

Unless I'm missing something here, the continue statement isn't a horror in principle. It's a very useful construct.

Beef
Jul 26, 2004
Looks like a student that has been staring at assembly code for too long.

Also explains the ... 'comments'

baquerd
Jul 2, 2007

by FactsAreUseless

Beef posted:

Looks like a student that has been staring at assembly code for too long.

Hah, yeah I just noticed one of their first programming courses was assembler and the other two were C, talk about your horrors to learn with.

Xenogenesis
Nov 8, 2005
http://www.exploringbinary.com/java-hangs-when-converting-2-2250738585072012e-308/

:kiddo:

NotShadowStar
Sep 20, 2000
I'm taking a numeric analysis course this semester, and the second chapter is literally all about floating point errors and bounds checking and how much this is going to gently caress you over and over until you get it right.

Then we have languages like PHP and Java, who were apparently too good to learn that crap.

zootm
Aug 8, 2006

We used to be better friends.

NotShadowStar posted:

I'm taking a numeric analysis course this semester, and the second chapter is literally all about floating point errors and bounds checking and how much this is going to gently caress you over and over until you get it right.
As much as it should, I would be impressed if the chapter mentions the x87 weirdness that causes this particular issue. Whoever thought it would be a good idea that your value can change when it moves from a register to memory ought to be shot.

evensevenone
May 12, 2001
Glass is a solid.
ah, the simple joys of embedded coding.
code:
    
   TERMIO_Init();

   TIOS |= TIOS_IOS4_MASK; // enable output capture
   TSCR1 |= TSCR1_TEN_MASK; // enable timer module
   TCTL1 &= ~(TCTL1_OM4_MASK|TCTL1_OL4_MASK); // disconnects output pins from TIM
   TSCR2 |= (TSCR2_PR0_MASK|TSCR2_PR1_MASK|TSCR2_PR2_MASK);
   TSCR2 &= ~(TSCR2_TCRE_MASK|TSCR2_TOI_MASK);
      
   TC4 = TCNT+PERIOD;
   TFLG1=TFLG1_C4F_MASK;
   TIE |= TIE_C4I_MASK;   

Adbot
ADBOT LOVES YOU

POKEMAN SAM
Jul 8, 2004

zootm posted:

As much as it should, I would be impressed if the chapter mentions the x87 weirdness that causes this particular issue. Whoever thought it would be a good idea that your value can change when it moves from a register to memory ought to be shot.

I don't think that the Java problem is the same x87 weirdness as PHP. Their algorithm is different (approximating toward the target from the other direction), and also it seems to work (not work) on 32-bit and 64-bit hardware.

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