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
TOO SCSI FOR MY CAT
Oct 12, 2008

this is what happens when you take UI design away from engineers and give it to a bunch of hipster art student "designers"
code:
buildInfo.setName(resultSet.nextString())); //column 75

Adbot
ADBOT LOVES YOU

Malloc Voidstar
May 7, 2007

Fuck the cowboys. Unf. Fuck em hard.
I don't think this is really a horror but it's horribly ugly to me so whatever

I'm converting the subtitles of TED Talks, which is in this JSON structure:
http://www.ted.com/talks/subtitles/id/1032/lang/eng

Using Google Gson to deserialize that:
code:
Gson gs = new Gson();
Type type = new TypeToken<Map<String, List<Map<String, String>>>>(){}.getType();
Map<String, List<Map<String, String>>> raw = gs.fromJson(jsonSubString, type);
List<Map<String, String>> subs = raw.get("captions"); // actual List of the subtitle objects
too many angle brackets :(

baquerd
Jul 2, 2007

by FactsAreUseless

Aleksei Vasiliev posted:

Type type = new TypeToken<Map<String, List<Map<String, String>>>>(){}.getType();
Map<String, List<Map<String, String>>> raw = gs.fromJson(jsonSubString, type);
List<Map<String, String>> subs = raw.get("captions"); // actual List of the subtitle objects[/code]
too many angle brackets :(

code:
private static class WHATISLOVE extends Map<String, List<Map<String, String>>> { }
Type type = new TypeToken<WHATISLOVE>(){}.getType();
WHATISLOVE raw = gs.fromJson(jsonSubString, type);

PrBacterio
Jul 19, 2000
Just got tripped up by this fun little gem in a module that's supposed to send commands (which the part of the program I'm developing/debugging right now is supposed to generate and then dispatch by use of this module) to a direct drive motor controller over ProfiBus:
code:
...
			int n = sizeof(bus_stat);
			n = bus_device->pbusRead(device_num, n, recvbuf);
			if(n < 0)
...
And this is the declaration of pbusRead from the relevant header file to go along with it:
code:
	virtual int pbusRead(int device, int &count, const unsigned char *&) = 0;
:gonk:

Now looking at the implementation of pbusRead it turns out that the output parameter count is never actually written to so just changing the parameter from being passed by reference to being passed by value should probably suffice to clear this up, but still ...

shrughes
Oct 11, 2008

(call/cc call/cc)
loving code! Why does C++ automatically convert into to bool? I had one of these happen:

Original signature:
code:
typedef int some_id;


Foo(Bar *b, bool should_load);
Foo(Bar *b, some_id id);

Then, later, the second signature gets changed:

code:
Foo(Bar *b, bool should_load);
Foo(Bar *b, some_id id, Timestamp recency);
and now some code we had that did

code:
Foo *f = new Foo(bar, blah_change_id);
compiles and has now called the wrong constructor! loving C++.

Bjarne more like Bjarse.

MutantBlue
Jun 8, 2001

Add 'explicit' to your constructor, problem solved. Or not, I haven't tested it.

Nevermind, 'explicit' only applies to single-argument constructors.

MutantBlue fucked around with this message at 06:27 on Mar 23, 2011

pokeyman
Nov 26, 2006

That elephant ate my entire platoon.
Wait why does that compile?

Karanth
Dec 25, 2003
I need to finish Xenogears sometime, damn it.

pokeyman posted:

Wait why does that compile?

Because implicit conversion always sounds like such a good idea at the time.

Opinion Haver
Apr 9, 2007

pokeyman posted:

Wait why does that compile?

some_id implicitly gets converted into bool.

pokeyman
Nov 26, 2006

That elephant ate my entire platoon.

yaoi prophet posted:

some_id implicitly gets converted into bool.

Oh, now I get it. Thanks!

HFX
Nov 29, 2004

yaoi prophet posted:

some_id implicitly gets converted into bool.

Implicit type conversion, or how you get to laugh at someone when they tell you C++ is strongly typed.

beuges
Jul 4, 2005
fluffy bunny butterfly broomstick
code:
		long amt = Convert.ToInt64(amount);
		string strAmount = CXGlobals.LanguageManager.CurrentLanguageSettings.CurrencySymbol + (amt/100).ToString() + (amt%100).ToString();

nielsm
Jun 1, 2009



HFX posted:

Implicit type conversion, or how you get to laugh at someone when they tell you C++ is strongly typed.

code:
class safe_bool {
  // it holds a bool!
  bool v;
  // don't construct from non-boolean types
  safe_bool(int);
  safe_bool(void*);
  safe_bool(double);
public:
  // safe constructors
  safe_bool() : v(false) { }
  safe_bool(bool val) : v(val) { }
  safe_bool(const safe_bool &other) : v(other.v) { }
  // safe assignment operators
  safe_bool & operator = (bool val) { v = val; return *this; }
  safe_bool & operator = (const safe_bool &val) { v = val.v; return *this; }
  // converts to bool
  bool operator bool() const { return v; }
  // acts like a bool
  safe_bool operator ! () const { return !v; }
  safe_bool operator == (const safe_bool &other) const { return v == other.v; }
  safe_bool operator != (const safe_bool &other) const { return v != other.v; }
  // ... add more comparisons here
};
Did I forget anything? Will this even work? :downs:

pseudorandom name
May 6, 2007

Did you deliberately choose the name safe_bool and not implement the safe bool idiom to point out the other problems with bool in C++, or was that a happy coincidence?

nielsm
Jun 1, 2009



It's just intended to be a more_typesafe_bool. I'd like to hear about those other problems with bool in C++.

raminasi
Jan 25, 2005

a last drink with no ice

HFX posted:

Implicit type conversion, or how you get to laugh at someone when they tell you C++ is strongly typed.

Do people actually claim this?

pseudorandom name
May 6, 2007

nielsm posted:

It's just intended to be a more_typesafe_bool. I'd like to hear about those other problems with bool in C++.

Consider what happens when e.g. a smart pointer template class implements operator bool and you compare two different instantiations of that template for equality.

pseudorandom name fucked around with this message at 02:27 on Mar 24, 2011

nielsm
Jun 1, 2009



pseudorandom name posted:

Consider what happens when e.g. a smart pointer template class implements operator bool and you compare two different instantiations of that template for equality.

I guess that can be a problem, yes. You mean like this?
code:
shared_ptr<Base> foo;
shared_ptr<Derived> bar;
if (foo == bar) ...
Shouldn't this still solve the problem? But yes it isn't ideal.
code:
template <typename Left, typename Right>
bool operator == (shared_ptr<Left> left, shared_ptr<Right> right)
{ ... }
The solution to problems caused by operator overloads? More operator overloads!

more like dICK
Feb 15, 2010

This is inevitable.
I stumbled upon an interesting file today written by one of our old-timers today. It was last modified in 2001 and it both terrifies and amazes me.

message_writer.c posted:

__asm {
// 12k lines of inline assembly
// no c at all
}

Scaramouche
Mar 26, 2001

SPACE FACE! SPACE FACE!

Hah I remember doing this back in the day. It was when I was just learning and didn't know anything about compiler operation. I was teaching myself C and I had some module that had to be 'super fast' so I thought 'well what's faster than asm?'.

Ended up being problematic to debug and modify when I came back to it a few years later for some reason...

MrMoo
Sep 14, 2000

Thankfully inline assembler is no longer supported with Win64.

Lysandus
Jun 21, 2010
code:
long thisInterval = ((Long)data).longValue();
long nextInterval = ((Long)data).longValue();
						
if( thisInterval < nextInterval )
{
    _data = data;
}

Edison was a dick
Apr 3, 2010

direct current :roboluv: only
Well it might do something if data changes, which could happen if it's volatile.
What are data and _data?

Lysandus
Jun 21, 2010

Edison was a dick posted:

Well it might do something if data changes, which could happen if it's volatile.
What are data and _data?

It's not. data is passed into the method and _data is the passed in data but global. Later on the method _data = data; anyway based some some other criteria.

POKEMAN SAM
Jul 8, 2004

MrMoo posted:

Thankfully inline assembler is no longer supported with Win64.

Wait what?! :(

nielsm
Jun 1, 2009



MSVC for 64 bit doesn't allow inline assembly. You can still have assembly source files and call functions written entirely in assembly.

Lysandus
Jun 21, 2010
Bad naming usually pisses me off, but this was a good laugh.

code:
private void addBasicGorillasToTheBananaHeaders ( Hashtable<String, String> properlyRequestedBananaRequisitionFormsWithCheifGorillaApproval  )
{
    //stuff
}

pokeyman
Nov 26, 2006

That elephant ate my entire platoon.
Shame about the typo.

Modern Pragmatist
Aug 20, 2008
I spend all my time in an academic research environment, and this type of stuff is rampant.

Question: How do I speed this up?

code:
counter=1;
I_Matrix=[[1+j100; 100] [100; 100] [100; 100] [100; 100]...
[100; 100] [100; 100] [100; 100] [100; 100]...
[1+j50; 50] [50; 50] [50; 50] [50; 50]...
[50; 50] [50; 50] [50; 50] [50; 50]];
                                    
for a=1:1:2 %I1
for b=1:1:2 %I2
for c=1:1:2 %I3
for d=1:1:2 %I4
for e=1:1:2 %I5
for f=1:1:2 %I6
for g=1:1:2 %I7 
for h=1:1:2 %I8
for k=1:1:2 %I9
for l=1:1:2 %I10
for m=1:1:2 %I11
for n=1:1:2 %I12
for o=1:1:2 %I13
for p=1:1:2 %I14
for q=1:1:2 %I15
for r=1:1:2 %I16 
run Combinations_16SSPCs; % == calculation that delivers M and W
F16(counter,1)=((M-W)/W)*100;%1%((M-W)/W))*100; 
counter=counter+1;
end 
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end

Bozart
Oct 28, 2006

Give me the finger.

Modern Pragmatist posted:

I spend all my time in an academic research environment, and this type of stuff is rampant.

Question: How do I speed this up?
code:
% so I can read it!  Also there are starving matrices in Africa waiting for
% square brackets
I_Matrix=transpose([
    1+100j 100 
    100    100
    100    100
    100    100
    100    100
    100    100
    100    100
    100    100
    1+50j  50
    50     50
    50     50
    50     50
    50     50
    50     50
    50     50
    50     50]);

F16 = repmat(NaN,2^size(I_Matrix,2),1);

% a silly way to create all combinations of what you loop over.  We could
% also just use mod but this takes fewer lines and it isn't going to take
% long (2 sec)
Combos = dec2bin(1:numel(F16)-1);
Combos = num2cell(Combos);
Combos = cellfun(@strcmp,Combos,repmat({'1'},size(Combos))) + 1;
Combos = num2cell(Combos,2);

% now each element of combos corresponds to an element in F16.

for counter = 1:numel(F16)
    % if I knew Combinations_16SSPCs was a function instead oa a script,
    % and if it was vectorized we could just feed it some form of Combos
    % (probably the nacho cheese type) and I_Matrix.  But I can't tell if
    % it uses the variable "counter" or previous outputs, which I suspect
    % it does, so I can't vectorize it.  Also, I suspect that the script
    % should make use of the filter command if it does depend on previous
    % outputs instead of whatever it is trying to do.
    [a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r] = deal(Combos{counter,:});
    
    % scripts are evil.
    run Combinations_16SSPCs; % == calculation that delivers M and W
    F16(counter,1)=((M-W)/W)*100;%1%((M-W)/W))*100; well that god for that!
end 


Hey Matlab buddy ( I think? ) the biggest problem is the Combinations_16SSPCs script. Without vectorizing it, we'll probably be slow. Try running the profiler.

Modern Pragmatist
Aug 20, 2008

Bozart posted:

Hey Matlab buddy ( I think? ) the biggest problem is the Combinations_16SSPCs script. Without vectorizing it, we'll probably be slow. Try running the profiler.

Yes. Unfortunately I'm forced to work with matlab. This was the fine work of a co-worker (self-proclaimed programmer). I basically threw it in the trash.

Bozart
Oct 28, 2006

Give me the finger.

Modern Pragmatist posted:

Yes. Unfortunately I'm forced to work with matlab. This was the fine work of a co-worker (self-proclaimed programmer). I basically threw it in the trash.

It can be surprisingly elegant if used correctly, but it takes practice, and you have to think vectors instead of iterations.

The fact that it can't handle dates or strings well is something that belongs in this thread. I suggest using java classes for those. And just stay away from the database toolbox, scripts, and the eval command.

Modern Pragmatist
Aug 20, 2008

Bozart posted:

It can be surprisingly elegant if used correctly, but it takes practice, and you have to think vectors instead of iterations.

The fact that it can't handle dates or strings well is something that belongs in this thread. I suggest using java classes for those. And just stay away from the database toolbox, scripts, and the eval command.

I love linear algebra, and can optimize me some Matlab code. However, 99% of scientists who use it cannot, and are ok with running a script over night because 2 hours is "fast enough".

NotShadowStar
Sep 20, 2000
After working in research for a few years I'm amazed that anything ever gets done at all. Everything is done in hackey poo poo like that, and it's so bad that results are not reproducible unless someone has the exact environment that all the hackey poo poo was run in.

If someone published a paper and they said 'well you can't reproduce our results except only in our lab' would get people taken out back and beaten up. But they're all strangely okay with 'you can't reproduce our analysis unless you have our exact environment'.

Deep Dish Fuckfest
Sep 6, 2006

Advanced
Computer Touching


Toilet Rascal

Modern Pragmatist posted:

I love linear algebra, and can optimize me some Matlab code. However, 99% of scientists who use it cannot, and are ok with running a script over night because 2 hours is "fast enough".

This is something I understood after the first assignment of my Computer Vision class when I was an undergrad. You're doing linear algebra, not C programming. You're dealing with vectors and matrices, not arrays and, well, 2D arrays I guess. The point is, for loops are your enemy.

Worse, however, are the people who do not understand the difference between numerical and symbolic computations and always use the latter with Mathematica or Maple.

BigRedDot
Mar 6, 2008

YeOldeButchere posted:

This is something I understood after the first assignment of my Computer Vision class when I was an undergrad. You're doing linear algebra, not C programming. You're dealing with vectors and matrices, not arrays and, well, 2D arrays I guess. The point is, for loops are your enemy.
Yes, well heaven help the poor bastard like me that has to take lovely matlab prototypes (even great matlab is lovely) and convert them into production applications in C.

quote:

Worse, however, are the people who do not understand the difference between numerical and symbolic computations and always use the latter with Mathematica or Maple.
What? If you need symbolic computations (like I did when I was in graduate school for physics) then Mathematica and Maple are exactly the right choice. If you are doing symbolic math in matlab, you are doing it wrong.

Opinion Haver
Apr 9, 2007

code:
// we should protect against SQL injection attacks here...?
You don't say. No super-valuable data or anything, just stuff that would take a couple weekends on some high-end machines to replace. Though I guess since mysql_query() only allows single queries, the built query is a select and the database isn't secret or anything and would take a weekend of number-crunching to regenerate, there's not really much that could be done. Then again, there might be some hilarious way to embed UPDATE/DELETEs into a SELECT because lol MySQL.

I replaced it with a prepared statement, then ran into escaping-related issues because one of the things I was using was a regexp that involved both literal parens and grouping parens and I couldn't for the life of me figure out how to escape properly. So I just wound up running

code:
$regexp = preg_replace('(', '[(]', $regexp);
beforehand. Simpler than loving around with backslashes. I don't know whether that statement, PHP, SQL, or all three is the horror here.

Opinion Haver fucked around with this message at 08:47 on Mar 26, 2011

qntm
Jun 17, 2009

yaoi prophet posted:

code:
$regexp = preg_replace('(', '[(]', $regexp);
I don't know whether that statement, PHP, SQL, or all three is the horror here.

That statement there is a pretty big horror.

code:
"\(stuff\)" => "\[(]stuff\)"
Congratulations, you have inserted a literal left-bracket and right-bracket?

Deep Dish Fuckfest
Sep 6, 2006

Advanced
Computer Touching


Toilet Rascal

BigRedDot posted:

What? If you need symbolic computations (like I did when I was in graduate school for physics) then Mathematica and Maple are exactly the right choice. If you are doing symbolic math in matlab, you are doing it wrong.

That's not what I meant, but looking back at what I wrote I can see that's what it sounded like. If you need symbolic math then Maple/Mathematica are definitely the way to go, but some people apparently have no idea that there's a faster way to go than "crank through symbolic math -> evaluate result" when all they care about is the numerical values. So they always use Maple/Mathematica. It leads to things that would take seconds with even the worst Matlab script taking hours.

Adbot
ADBOT LOVES YOU

Opinion Haver
Apr 9, 2007

qntm posted:

That statement there is a pretty big horror.

code:
"\(stuff\)" => "\[(]stuff\)"
Congratulations, you have inserted a literal left-bracket and right-bracket?

Fortunately nobody is going to be searching for stuff including backslashes in our data. :v:

edit: from the Python questions thread:


king_kilr posted:

Fun times:

code:
>>> a = ([], 2)

>>> a[0] += ["boy oh boy"]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

/home/alex/<ipython console> in <module>()

TypeError: 'tuple' object does not support item assignment

>>> a
[3] (['boy oh boy'], 2)

Opinion Haver fucked around with this message at 10:01 on Mar 27, 2011

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