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
haveblue
Aug 15, 2005



Toilet Rascal
seems parenthesized right to me :confused: there's an extra set around the X for no reason but the conversion looks right

Adbot
ADBOT LOVES YOU

Fifty-Nine
Oct 15, 2003

sex offendin Link posted:

seems parenthesized right to me :confused: there's an extra set around the X for no reason but the conversion looks right

The extra set is to maintain order of operations.

E.g. SIN( x + 30 ) would expand to sin( x + 30 * M_PI / 180 ) without the extra parens, versus sin( (x + 30) * M_PI / 180 ) with.

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
Yeah, on a second look I read the parens wrong

Scaevolus
Apr 16, 2007

Avenging Dentist posted:

The sine function generally does not range from -0.0174532778 to 0.0174532778.

The only thing I can find wrong with it is that it uses degrees instead of gradians. :smug:

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

Scaevolus posted:

The only thing I can find wrong with it is that it uses degrees instead of gradians. :smug:

Why would a system that uses 400 units for the angle of a circle be better?

Scaevolus
Apr 16, 2007

Avenging Dentist posted:

Why would a system that uses 400 units for the angle of a circle be better?

Why would a 404 help your point?

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

Scaevolus posted:

Why would a 404 help your point?

If you can't be bothered to figure out what the URL should be, perhaps you should go and hit the books?

Scaevolus
Apr 16, 2007

Avenging Dentist posted:

If you can't be bothered to figure out what the URL should be, perhaps you should go and hit the books mayhaps?

3 char extensions should be good enough for anyone!

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

Scaevolus posted:

3 char extensions should be good enough for anyone!

You know, if you got a 3-char extension maybe you wouldn't be a 2-bit poster anymore!

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
This is why macros are bad, they kick sand in everyone's vaginas and :regd07: happens

Scaevolus
Apr 16, 2007

Avenging Dentist posted:

You know, if you got a 3-char extension maybe you wouldn't be a 2-bit poster anymore!

0_0

Mustach
Mar 2, 2003

In this long line, there's been some real strange genes. You've got 'em all, with some extras thrown in.
Gradian: The GNU radian

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

Otto Skorzeny posted:

Yeah, on a second look I read the parens wrong
Christ, so did I. :suicide: Shouldn't try to post smarmy comments before my third cup of coffee of the day.

necrobobsledder
Mar 21, 2005
Lay down your soul to the gods rock 'n roll
Nap Ghost

Mustach posted:

Gradian: The GNU radian
...with file extension .gr8

gold brick
Jun 19, 2001

no he isn't
code:
switch (something)
{
   case "foo":
        
       break;
   case "bar":
       doSomethingElse();
       break;
   default:

       break;
}

geetee
Feb 2, 2004

>;[

Mick posted:

code:
switch (something)
{
   case "foo":
        
       break;
   case "bar":
       doSomethingElse();
       break;
   default:

       break;
}

It's almost as if there used to be a line of code under "foo".

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

geetee posted:

It's almost as if there used to be a line of code under "foo".

People have an unhealthy obsession with switch statements though (especially in languages like C# or Java). I mean ok, sure, in C/C++ with a sub-optimal compiler, a switch statement might be faster than if/elses by implementing it as a jump table (a smart compiler should be able to figure this out too for if/elses), but that sort of thing isn't going to work with string comparisons.

Flobbster
Feb 17, 2005

"Cadet Kirk, after the way you cheated on the Kobayashi Maru test I oughta punch you in tha face!"

Avenging Dentist posted:

People have an unhealthy obsession with switch statements though (especially in languages like C# or Java). I mean ok, sure, in C/C++ with a sub-optimal compiler, a switch statement might be faster than if/elses by implementing it as a jump table (a smart compiler should be able to figure this out too for if/elses), but that sort of thing isn't going to work with string comparisons.

I went to search Google for some statistics on when C#'s compile-string-switches-to-a-hashtable approach becomes more/less efficient than a chain of if/elses, and I found something else that belongs in this thread: shoehorning in a functional lambda-based switch construct that accomplishes the same thing as a bunch of ifs, but uglier!

code:
// here’s one way:
new Switch<string>(switchValue)
{
  { "hello", 
     ()=> { Console.WriteLine("Value was hello"); } 
  },
  { x => x.StartsWith("h"), 
     ()=> { Console.WriteLine("value started with h, but wasn't hello"); } 
  },
  { // default
     () => { Console.WriteLine("No match - Fallthrough"); } 
  }
};

// how about a case insensitive comparison? switch (switchValue.ToLower()) ?
// or...

new Switch<string>(switchValue, StringComparer.InvariantCultureIgnoreCase)
{
  { "HOUSE", ()=> { Console.WriteLine("Value was house"); } },
  { x => x.StartsWith("H", StringComparison.InvariantCultureIgnoreCase), 
     ()=> { Console.WriteLine("value started with h, but wasn't house"); } 
  },
  { () => { Console.WriteLine("No match - Fallthrough"); } }
};
:psyduck:

The best part is that you would think that you could say "Oh that's almost kinda cool, I could create this switch object and pass it around", but you can't because it abuses collection initializers and executes each case immediately in the IEnumerable.Add method.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

Flobbster posted:

I went to search Google for some statistics on when C#'s compile-string-switches-to-a-hashtable approach becomes more/less efficient than a chain of if/elses

I would hope that the compiler would notice that you're using a chain of if/elses and transform them into a switch statement in the IL.

Goat Bastard
Oct 20, 2004

Avenging Dentist posted:

People have an unhealthy obsession with switch statements though (especially in languages like C# or Java). I mean ok, sure, in C/C++ with a sub-optimal compiler, a switch statement might be faster than if/elses by implementing it as a jump table (a smart compiler should be able to figure this out too for if/elses), but that sort of thing isn't going to work with string comparisons.

Here is a small peek into the mind of someone who loves switch statements. I found this today:
code:
if (registerHeader != null) {
    switch (registerHeader.getFBA1EventCode()) {
        case ACCEPTED_BY_PROCESS_HANDLER :
        case INPUT_COMPLETED :
        case PROCESS_COMPLETED :
            break;
        default :
            throw new PQHInputException(Messages.getString("error.input.305.205")
                , Priority.PRIORITY_FATAL
                , PQHFaultType.PQH_STANDARD_INVALID_REGISTER_STATE_EXCEPTION
                , PQHInputException.MESSAGE_ID_NOT_AVAILABLE
            );
        } // end Switch
}
Later on this guy has to store a bunch of pojos in a database, for future retrieval and use. So he creates an XML object representing each one, and inserts it into a BLOB field. I mean how else would you do that?

BattleMaster
Aug 14, 2000

Avenging Dentist posted:

I would hope that the compiler would notice that you're using a chain of if/elses and transform them into a switch statement in the IL.

Not all if/else chains translate into a switch/case block, and if they do you just wasted a lot more time typing it out like that.

Also there's no good way to represent two consecutive labels with no break statement in between within a switch/case block if you type it out as an if/else chain.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

BattleMaster posted:

Not all if/else chains translate into a switch/case block, and if they do you just wasted a lot more time typing it out like that.

Do you really have that hard a time writing else if(foo== compared to break?

BattleMaster posted:

Also there's no good way to represent two consecutive labels with no break statement in between within a switch/case block if you type it out as an if/else chain.

They don't have the boolean-or operator where you're from?

Switch statements are useful, but people use them a lot of times because they think it makes them clever. Unless you're writing Duff's Device (and you shouldn't), you're not being clever. See the above example for a good misuse of switches, since I guess using boolean arithmetic just wasn't enough fun.

Goat Bastard
Oct 20, 2004

BattleMaster posted:

Also there's no good way to represent two consecutive labels with no break statement in between within a switch/case block if you type it out as an if/else chain.

code:
if(condition == FIRST_STATE) {
	doFirstStateStuff();
} else if (condition == SECOND_STATE) {
	doFirstStateStuff();
	doSecondStateStuff();
}
This also saves the next guy to look it from having to work out whether the absence of a break; is intentional or not.

BattleMaster
Aug 14, 2000

Cool, extraneous function calls and code duplication. That works out really well when you have to fit your program into a few KB of flash memory.

Hint: Not everyone programs for desktop/server/laptop computers. A lot of embedded devices have compilers that aren't so hot at optimization. For example, MPLAB C18 for PIC microcontrollers never generates jump tables out of if/else chains. It instead turns it into a big chain of comparisons. Small Device C Compiler doesn't either, though its jump-table generation for switch/case is pretty bad as it is.

Zhentar
Sep 28, 2003

Brilliant Master Genius

Goat Bastard posted:

code

Alternatively, if duplicated function calls murdered your father and raped your mother:

code:
if(condition == FIRST_STATE || condition == SECOND_STATE) {
	doFirstStateStuff();
} 
if (condition == SECOND_STATE) {
	doSecondStateStuff();
}

BattleMaster
Aug 14, 2000

Zhentar posted:

Alternatively, if duplicated function calls murdered your father and raped your mother:

code:
if(condition == FIRST_STATE || condition == SECOND_STATE) {
	doFirstStateStuff();
} 
if (condition == SECOND_STATE) {
	doSecondStateStuff();
}

That works but then the compiler might not understand what you're trying to do and will not optimize it into a jump table. Not that any of the C compilers I work with will do that anyways.

Zhentar
Sep 28, 2003

Brilliant Master Genius

BattleMaster posted:

Hint: Not everyone programs for desktop/server/laptop computers. A lot of embedded devices have compilers that aren't so hot at optimization. For example, MPLAB C18 for PIC microcontrollers never generates jump tables out of if/else chains. It instead turns it into a big chain of comparisons. Small Device C Compiler doesn't either, though its jump-table generation for switch/case is pretty bad as it is.

A year later the inefficiency from forcing things into difficult to maintain code is going to dwarf the overhead of extra conditional branches.

BattleMaster
Aug 14, 2000

Zhentar posted:

A year later the inefficiency from forcing things into difficult to maintain code is going to dwarf the overhead of extra conditional branches.

Maybe, but a more immediate concern is crappy compilers and extremely limited RAM/ROM where you quite literally have to count bytes.

I don't find switch/case much harder to modify than if/else myself, though.

The Noble Nobbler
Jul 14, 2003
switch is awful and is the wrong thing to use 95% of the time

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
If you have one variable which may have one of several discrete values, switch is a perfectly fine way to make decisions based on that value, assuming you aren't some sort of OOD zombie that laments daily the sad passing of Smalltalk

CanSpice
Jan 12, 2002

GO CANUCKS GO

BattleMaster posted:

That works but then the compiler might not understand what you're trying to do and will not optimize it into a jump table. Not that any of the C compilers I work with will do that anyways.

What's that quote about premature optimization?

BattleMaster
Aug 14, 2000

CanSpice posted:

What's that quote about premature optimization?

What's that quote where I said that I work with devices that have bytes of RAM, kilobytes of flash memory and the compilers suck?

It's not premature optimization when you go to burn in a program and the debugger tells you the target doesn't have enough memory and somehow need to make it fit.

Edit: Seriously, I work with an architecture where you can define a 16 bit "near" pointer or a 24 bit "far" pointer, and the reason that they let you choose is because you might need that byte for something else.

BattleMaster fucked around with this message at 02:22 on Jul 25, 2009

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

CanSpice posted:

What's that quote about premature optimization?

"We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil." - Tony Hoare

The quote was popularized by Knuth. People (not Knuth, who spoke pretty clearly in the article where he first quoted Hoare that nobody bothers to read and everyone ignores) fail to realize that Hoare was referring to counting cycles. Accounting for known limits of your compiler by translating one simple structure into another that is also semantically valid for what you're doing is nothing like 'premature optimization'.

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

BattleMaster posted:

Edit: Seriously, I work with an architecture where you can define a 16 bit "near" pointer or a 24 bit "far" pointer, and the reason that they let you choose is because you might need that byte for something else.

What arch are you working on again? I thought segmented memory was a thing of the past even in embedded environments.

BattleMaster
Aug 14, 2000

Otto Skorzeny posted:

What arch are you working on again? I thought segmented memory was a thing of the past even in embedded environments.

Microchip PIC18 microcontrollers, which have one contiguous flash memory addressing space but segmented RAM. 16-bit Near ROM pointers refer to a position within the first 64KB of flash memory, and 24-bit far ROM pointers can access the entire 21-bit range. Most devices have much less than 64KB of flash memory and nothing comes anywhere close to the addressing limit so I've never actually had to use a far pointer before.

A far RAM pointer is 16-bit and refers to a spot in the entire 12-bit memory addressing space while an 8-bit near RAM pointer refers to a location in a special bank that is accessible at all times no matter what bank is selected.

awesmoe
Nov 30, 2005

Pillbug

BattleMaster posted:

What's that quote where I said that I work with devices that have bytes of RAM, kilobytes of flash memory and the compilers suck?

It's not premature optimization when you go to burn in a program and the debugger tells you the target doesn't have enough memory and somehow need to make it fit.

Edit: Seriously, I work with an architecture where you can define a 16 bit "near" pointer or a 24 bit "far" pointer, and the reason that they let you choose is because you might need that byte for something else.
Thats cool, the rest of us dont

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
A real man would use the GCC frontend with LLVM to compile to (optimized) C and then compile with the old-n-busted compiler you've got.

BattleMaster
Aug 14, 2000

I really should give that a try some day and see if it breaks anything. C18 and SDCC are really wacky and nonstandard though.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
If the compilers support functions, if statements, and gotos you'll probably be ok.

Adbot
ADBOT LOVES YOU

BattleMaster
Aug 14, 2000

Avenging Dentist posted:

If the compilers support functions, if statements, and gotos you'll probably be ok.

Durr hurr

I was thinking more along the lines of memory poo poo that other compilers wouldn't understand.

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