Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 127 128 [129] 130 131 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 907226 times)

Nadaka

  • Bay Watcher
    • View Profile
    • http://www.nadaka.us
Re: if self.isCoder(): post() #Programming Thread
« Reply #1920 on: March 08, 2012, 06:16:35 pm »

You know that stuff that makes people hate java, but they never tell you what it is?

Here is one of them:

//This is invalid
Double doubleObj = 1;
int intPrimative = doubleObj;

//This is not
Integer intObj = 1;
double doublePrimative = intObj;

//This is also invalid
Double doubleObj = 1;
int intPrimative = (int)doubleObj;

//and so is this!
Double doubleObj = 1;
int intPrimative = (Integer)doubleObj;

//This is required
Double doubleObj = 1;
int intPrimative = (int)(double)doubleObj;


This needless semantic cruft is the best way to get a Double into an int. The other way is to convert the double to a string, possibly with formatting, then parse the string to an int.

At first you might think: Ok, I get it. It won't automatically cast from a double to an int. And you are right, except that isn't really your problem.

This is because java can only up or down cast the object wrapper of primitive type to and from its own primitive type, and does so automatically under almost every circumstance, but not this one. And for no particular reason.
« Last Edit: March 08, 2012, 06:19:02 pm by Nadaka »
Logged
Take me out to the black, tell them I ain't comin' back...
I don't care cause I'm still free, you can't take the sky from me...

I turned myself into a monster, to fight against the monsters of the world.

Levi

  • Bay Watcher
  • Is a fish.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1921 on: March 08, 2012, 06:19:15 pm »

I hate stuff like that.  Drives me nuts when in one case something seems to work fine and in another case the behavior is changed.
Logged
Avid Gamer | Goldfish Enthusiast | Canadian | Professional Layabout

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1922 on: March 08, 2012, 06:50:45 pm »

Fun fact: I never once hated any part of Perl syntax/behaviour.
Typecasting and pointers don't even exist in Perl.
It's just like Perl was designed to be awesome.

"There Is More Than One Way To Do It"
Try asking on a forum how to implement a loop in Common Lisp. You're guaranteed to get 5 completely different answers.
Same in Perl, but another Perl motto is "Making easy things easy and hard things possible". I don't quite see how the first part applies to Lisp, when you have to ask on a forum to know how to write a loop.
Logged

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1923 on: March 08, 2012, 07:03:29 pm »

You typically don't, as most introductory text cover that and the loop macro is very straight-forward, so you can usually fall back on that. However, ever so often someone will ask a question involving a loop, which yields a lot of different responses. Usually the problems stem from someone not being familiar with s-expressions.
« Last Edit: March 08, 2012, 07:08:35 pm by Virex »
Logged

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1924 on: March 08, 2012, 07:13:59 pm »

You know that stuff that makes people hate java, but they never tell you what it is?

Here is one of them:

//This is invalid
Double doubleObj = 1;
int intPrimative = doubleObj;

//This is not
Integer intObj = 1;
double doublePrimative = intObj;

//This is also invalid
Double doubleObj = 1;
int intPrimative = (int)doubleObj;

//and so is this!
Double doubleObj = 1;
int intPrimative = (Integer)doubleObj;

//This is required
Double doubleObj = 1;
int intPrimative = (int)(double)doubleObj;


This needless semantic cruft is the best way to get a Double into an int. The other way is to convert the double to a string, possibly with formatting, then parse the string to an int.

At first you might think: Ok, I get it. It won't automatically cast from a double to an int. And you are right, except that isn't really your problem.

This is because java can only up or down cast the object wrapper of primitive type to and from its own primitive type, and does so automatically under almost every circumstance, but not this one. And for no particular reason.
You do understand that you are using a class, right? A class with methods, as such
Code: [Select]
//This is required
Double doubleObj = 1;
int intPrimative = (int)(double)doubleObj;
Is not in fact required. Instead you could use
Code: [Select]
Double doubleObj = 1;
int intPrimative = doubleObj.intValue();

Levi

  • Bay Watcher
  • Is a fish.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1925 on: March 08, 2012, 11:08:15 pm »

I made myself a stock tracker for my stocks.

Its not finished yet, but it does update itself and display the correct data(although only at 10min intervals to keep me from spamming finance.yahoo.com).  Its using the python Django web app framework thingy, which so far has been a pain in the arse.

My stocks.

Also WHY WON'T MY STOCKS GO UP!    :'(

 :P

I added my previous stock history now to the website.  The kind of neat thing is this is all calculated from the transactions themselves.  I enter a list of buys/sells/splits/bankruptcy transactions and it calculates the totals on the fly. 

Also now that I can look at my previous stock sells, I don't look like quite as hopeless of an investor.  If you don't count my current stocks it all looks quite rosy.  :)
Logged
Avid Gamer | Goldfish Enthusiast | Canadian | Professional Layabout

SolarShado

  • Bay Watcher
  • Psi-Blade => Your Back
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1926 on: March 08, 2012, 11:23:18 pm »

>snip<
You do understand that you are using a class, right? A class with methods, as such
Code: [Select]
//This is required
Double doubleObj = 1;
int intPrimative = (int)(double)doubleObj;
Is not in fact required. Instead you could use
Code: [Select]
Double doubleObj = 1;
int intPrimative = doubleObj.intValue();

I would argue that it's better to do this:
Code: [Select]
Double doubleObj = 1;
int intPrimative = (int)Math.{floor|ceil|round}(doubleObj); // 98% certain this works

(That's Math.floor(), Math.ceil(), or Math.round(), as appropriate.)

Personally, I can never remember for sure exactly what casting a double to an int does (truncates, I think), so I greatly prefer the explicit function call.

Purely out of curiosity, why are you using Doubles instead of doubles? On the occasions I've needed to call a method from one of the primitive-wrapper classes there was a static version.
Logged
Avid (rabid?) Linux user. Preferred flavor: Arch

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1927 on: March 08, 2012, 11:29:01 pm »

Yea, that is also pretty good, not going to dispute the use of that for a second. You are correct in that removing the ambiguity is a preferable solution to what I had.
And yea, I'm like 99% sure it just truncates.

optimumtact

  • Bay Watcher
  • I even have sheep
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1928 on: March 09, 2012, 05:02:29 am »

And then there's networking. It's like threading, except that information about what the other thread did shows up in a more or less random order and may simply vanish entirely for the lulz.

Pointers are nice unless you forgot what you were doing with them.

Doesn't tcp guarantee that the packets will arrive in the order you sent them, as well as checking them for corruption? Of course, if you're using udp then all bets are off and stuff will arrive in any order. But for most common applications, unless you need high speed you should be using tcp for the properties it gives you.

edit: spelling.
« Last Edit: March 09, 2012, 05:06:46 am by optimumtact »
Logged
alternately, I could just take some LSD or something...

Mini

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1929 on: March 09, 2012, 05:06:46 am »

high speed
That is a terrible term to use in networking, since it can mean either low latency or high data flow (I assume it's latency in this case).
Logged

Nadaka

  • Bay Watcher
    • View Profile
    • http://www.nadaka.us
Re: if self.isCoder(): post() #Programming Thread
« Reply #1930 on: March 09, 2012, 09:32:22 am »

...
You do understand that you are using a class, right? A class with methods, as such
Code: [Select]
//This is required
Double doubleObj = 1;
int intPrimative = (int)(double)doubleObj;
Is not in fact required. Instead you could use
Code: [Select]
Double doubleObj = 1;
int intPrimative = doubleObj.intValue();
I do understand I am using an Object. Had forgotten about those methods. That does not change the issue that this is an exception to the nearly universal primitive wrapper rule for automatic conversion.

...

I would argue that it's better to do this:
Code: [Select]
Double doubleObj = 1;
int intPrimative = (int)Math.{floor|ceil|round}(doubleObj); // 98% certain this works

(That's Math.floor(), Math.ceil(), or Math.round(), as appropriate.)

Personally, I can never remember for sure exactly what casting a double to an int does (truncates, I think), so I greatly prefer the explicit function call.

Purely out of curiosity, why are you using Doubles instead of doubles? On the occasions I've needed to call a method from one of the primitive-wrapper classes there was a static version.

You can not use primitives as generics or in collections.
Logged
Take me out to the black, tell them I ain't comin' back...
I don't care cause I'm still free, you can't take the sky from me...

I turned myself into a monster, to fight against the monsters of the world.

kaijyuu

  • Bay Watcher
  • Hrm...
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1931 on: March 09, 2012, 12:37:13 pm »

....

See this is the problem with being self taught in things. I didn't know function pointers existed in c++ until I read this page, and there are innumerable things I could've made much simpler by using them. Looking at my usual documentation, function pointers are hidden away as a footnote, so I never noticed it. 

/rage
Logged
Quote from: Chesterton
For, in order that men should resist injustice, something more is necessary than that they should think injustice unpleasant. They must think injustice absurd; above all, they must think it startling. They must retain the violence of a virgin astonishment. When the pessimist looks at any infamy, it is to him, after all, only a repetition of the infamy of existence. But the optimist sees injustice as something discordant and unexpected, and it stings him into action.

olemars

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1932 on: March 09, 2012, 02:30:20 pm »

....

See this is the problem with being self taught in things. I didn't know function pointers existed in c++ until I read this page, and there are innumerable things I could've made much simpler by using them. Looking at my usual documentation, function pointers are hidden away as a footnote, so I never noticed it. 

/rage

You know how in various games you'll find notes here and there by scientists or wizards, mentioning some clever little thing they're going to try that'll make everything better, and then you find their charred skeletion in the next room? This is one of those notes.
Logged

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1933 on: March 09, 2012, 02:36:04 pm »

Hint: It's easier and safer to use command objects instead of function pointers. They even support closures.

Code: [Select]
class FooBarCommand {
  public:
    virtual Foo Execute (Bar) = 0;
};

class ThatCommand: public FooBarCommand {
  public:
    ThatCommand(Baz b): aBaz(b) {/*...*/};
    Foo Execute (Bar);
  private:
    Baz aBaz;
};

Foo ThatCommand::Execute (Bar aBar) {
  Foo aFoo;
  // do something with aFoo, aBar and aBaz
  return aFoo;
}

int main {
  Bar bar;
  Baz baz;
  FooBarCommand * c = new ThatCommand(baz);
  Foo foo = c.Execute(bar);

  // stuff

  delete c; c=0;
}

Edit: Fucking Tab-Return.
« Last Edit: March 09, 2012, 02:45:35 pm by MagmaMcFry »
Logged

Valid_Dark

  • Bay Watcher
  • If you wont let me Dream, I wont let you sleep.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1934 on: March 09, 2012, 02:50:46 pm »

....

See this is the problem with being self taught in things. I didn't know function pointers existed in c++ until I read this page, and there are innumerable things I could've made much simpler by using them. Looking at my usual documentation, function pointers are hidden away as a footnote, so I never noticed it. 

/rage

That's why I'm super happy with the book I'm using to teach myself, it goes really slow but covers everything very in depth.
Logged
There are 10 types of people in this world. Those that understand binary and those that don't


Quote
My milkshake brings all the criminals to justice.
Pages: 1 ... 127 128 [129] 130 131 ... 796