Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 567 568 [569] 570 571 ... 796

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

Dutrius

  • Bay Watcher
  • No longer extremely unavailable!
    • View Profile
    • Arcanus Technica
Re: if self.isCoder(): post() #Programming Thread
« Reply #8520 on: November 30, 2015, 01:45:03 pm »

Today I learned something about division in C#.

Code: [Select]
int aNumber = 30;
double result;

result = aNumber / 100;       //This returns 0, which is wrong.
result = aNumber / 100.0;     //This returns 0.3, which is correct.

Apparently, if you divide by an integer, you get an integer.
If you divide by a real number, you get a real number.
Logged
No longer extremely unavailable!
Sig text
ArcTech: Incursus. On hold indefinitely.

Telgin

  • Bay Watcher
  • Professional Programmer
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8521 on: November 30, 2015, 02:05:31 pm »

That kind of behavior is pretty typical in C-like languages, and has definitely caught me off guard a few times.  It's also annoying to have to sprinkle a lot of (double) casts all over the place in math heavy code as a result, but it is what it is.
Logged
Through pain, I find wisdom.

itisnotlogical

  • Bay Watcher
  • might be dat boi
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8522 on: November 30, 2015, 02:48:40 pm »

Speaking of numbers and weird behavior, here's a helpful video on floating point errors and why it happens.

Linky dinky
Logged
This game is Curtain Fire Shooting Game.
Girls do their best now and are preparing. Please watch warmly until it is ready.

TheBiggerFish

  • Bay Watcher
  • Somewhere around here.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8523 on: November 30, 2015, 03:59:29 pm »

Java's BigDecimal class FTW.
Logged
Sigtext

It has been determined that Trump is an average unladen swallow travelling northbound at his maximum sustainable speed of -3 Obama-cubits per second in the middle of a class 3 hurricane.

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #8524 on: November 30, 2015, 08:48:55 pm »

Java's BigDecimal class is terrible, not because of any fault in itself, but because Java is stupid and doesn't support arithmetic overloading >:l

I mean, if you want to do math with BigDecimal, your code will look really ugly.

// with primitives a, b, c
return (-b + Math.sqrt(b*b - 4 * a * c) ) / a / 2
// with BigDecimal a, b, c
return b.negate().add((b.pow(2).subtract(BIGSQRT(a.multiply(c).multiply(BigDecimal(4)))).divide(a)


and so on. BigInt doesn't have a sqrt method for obvious reasons (impossible to ensure infinite precision), but I feel like it would look so much simpler if Java just had operator overloading.... All the maths libraries of the world cry :v It's probably worse for vector and matrix math.
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

TheBiggerFish

  • Bay Watcher
  • Somewhere around here.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8525 on: November 30, 2015, 09:21:08 pm »

*shrug*
That's what documentation is for.
Logged
Sigtext

It has been determined that Trump is an average unladen swallow travelling northbound at his maximum sustainable speed of -3 Obama-cubits per second in the middle of a class 3 hurricane.

Putnam

  • Bay Watcher
  • DAT WIZARD
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8526 on: November 30, 2015, 10:44:08 pm »

i say we should all just switch to prefix notation

Orange Wizard

  • Bay Watcher
  • mou ii yo
    • View Profile
    • S M U G
Re: if self.isCoder(): post() #Programming Thread
« Reply #8527 on: November 30, 2015, 10:50:23 pm »

#reversepolishmasterrace
Logged
Please don't shitpost, it lowers the quality of discourse
Hard science is like a sword, and soft science is like fear. You can use both to equally powerful results, but even if your opponent disbelieve your stabs, they will still die.

Gatleos

  • Bay Watcher
  • Mournhold... City of Light... City of MAGIC!
    • View Profile
    • Someone Sig This
Re: if self.isCoder(): post() #Programming Thread
« Reply #8528 on: November 30, 2015, 11:32:38 pm »

I remember one of my programming professors going off on a digression about reverse polish notation just so that he could drop that reverse polish sausage xkcd at the end of class and everyone would understand it.
Logged
Think of it like Sim City, except with rival mayors that seek to destroy your citizens by arming legions of homeless people and sending them to attack you.
Quote from: Moonshadow101
it would be funny to see babies spontaneously combust
Gat HQ (Sigtext)
++U+U++ // ,.,.@UUUUUUUU

Bumber

  • Bay Watcher
  • REMOVE KOBOLD
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8529 on: November 30, 2015, 11:50:26 pm »

I think you might have to convert them into bytearrays, but there's something about needing to set encoding (i.e., utf8, etc.) and I don't know how to specify that.
Code: [Select]
b"this string is converted to a bytearray, usually UTF-8 encoding, I think"
bytes ("the bytes function will return an encoded bytearray", "utf-8")
I know zilch about networking though so I can't really help further.
Thanks. Looks like it's working:

Spoiler: Client Code (click to show/hide)
Spoiler: Server Code (click to show/hide)
Logged
Reading his name would trigger it. Thinking of him would trigger it. No other circumstances would trigger it- it was strictly related to the concept of Bill Clinton entering the conscious mind.

THE xTROLL FUR SOCKx RUSE WAS A........... DISTACTION        the carp HAVE the wagon

A wizard has turned you into a wagon. This was inevitable (Y/y)?

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8530 on: December 01, 2015, 12:21:50 am »

That kind of behavior is pretty typical in C-like languages, and has definitely caught me off guard a few times.  It's also annoying to have to sprinkle a lot of (double) casts all over the place in math heavy code as a result, but it is what it is.

Everything needs to be cast before the CPU can do maths operations, because the machine code instructions are different for int, float, double.

C-like languages are designed for speed. Automatically casting everything to doubles on the off chance that is what you want would actually make programs that run slower because they're always casting things to double even when you don't want it. Doubles use more bytes too, meaning you can't have as many working variables in the cpu registers at once if all your variables were cast as doubles. So the CPU would be doing a pile of useless additional effort if the compiler assumed you wanted doubles for literally every maths operation.,The compiler needs to make a decision every time it sees two non-matching variable being added/multiplied etc, it needs to throw a cast in there to make them compatible, and they err on the side of efficiency/speed.

*shrug*
That's what documentation is for.

documentation doesn't help if you can't work out the nesting of the parenthesis. Sure, maybe you can concoct that nightmare of code then comment the line. But lets hope you never have to debug a bug in that code.
« Last Edit: December 01, 2015, 12:34:21 am by Reelya »
Logged

Bumber

  • Bay Watcher
  • REMOVE KOBOLD
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8531 on: December 01, 2015, 02:26:19 am »

Doubles use more bytes too, meaning you can't have as many working variables in the cpu registers at once if all your variables were cast as doubles. So the CPU would be doing a pile of useless additional effort if the compiler assumed you wanted doubles for literally every maths operation.
I don't think this is true for x86-64 architecture. The 32-bit mov instructions clear the upper half of 64-bit registers, and you have to shift bits left and right to get 32-bit numbers in and out of the upper halves. You're no better off with a float than a double unless you're on an x86 (a.k.a., i386.) You can pretty much forget about doing this on the SSE (xmm 128-bit, ymm 256-bit, and soon zmm 512-bit) vector registers. It's difficult enough to get doubles in and out of the upper areas of those (efficiently, anyway.) More to the point, I'm pretty sure C compilers simply don't put two floats into one register.

Edit: And if you were comparing doubles to ints: 'int' is 64-bit (a.k.a, long int) by default now on most C compilers. The vector registers are absolutely useless for ints, so you actually get more registers from doubles.

There's no reason not to use doubles over floats. There is good reason not to convert between ints and doubles (it's slow, so preferably use a double to begin with unless you really need an int.)
« Last Edit: December 01, 2015, 03:04:49 am by Bumber »
Logged
Reading his name would trigger it. Thinking of him would trigger it. No other circumstances would trigger it- it was strictly related to the concept of Bill Clinton entering the conscious mind.

THE xTROLL FUR SOCKx RUSE WAS A........... DISTACTION        the carp HAVE the wagon

A wizard has turned you into a wagon. This was inevitable (Y/y)?

Gentlefish

  • Bay Watcher
  • [PREFSTRING: balloon-like qualities]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8532 on: December 01, 2015, 04:26:12 am »

The only reason I can ever remember casting (double) from int was when I needed to multiply x by 0.y or 1.y; It always came out as an int, but I needed to cast the double to ensure I got the division correct.

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8533 on: December 01, 2015, 04:51:29 am »

Today I learned something about division in C#.

Code: [Select]
int aNumber = 30;
double result;

result = aNumber / 100;       //This returns 0, which is wrong.
result = aNumber / 100.0;     //This returns 0.3, which is correct.

Apparently, if you divide by an integer, you get an integer.
If you divide by a real number, you get a real number.

Actually, that's the correct result it should produce, come to think of it. C is modular and tries to produce consistent code for the same input.

"aNumber/100" is integer-integer division. This statement will always produce an int/int opcode.

the "=" operator is just a binary operator that takes the output from the RHS and tries to store it in the LHS, it will implicitly cast things if needed/possible, but it only does that on the entire result for the RHS, not any component parts thereof.

So, here, producing consistent code for the same input trumps trying to second-guess where the programmer made a mistake and arbitrarily cast variables by itself based on non-local code. Such automatically mutating code behaviour might be helpful sometimes to avoid manual casts, but many other times, producing basically arbitrary code for the same input would be sure to produce untrackable bugs. So the compiler goes for consistency of individual operations.
« Last Edit: December 01, 2015, 05:10:26 am by Reelya »
Logged

Telgin

  • Bay Watcher
  • Professional Programmer
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8534 on: December 01, 2015, 08:41:50 am »

That kind of behavior is pretty typical in C-like languages, and has definitely caught me off guard a few times.  It's also annoying to have to sprinkle a lot of (double) casts all over the place in math heavy code as a result, but it is what it is.

Everything needs to be cast before the CPU can do maths operations, because the machine code instructions are different for int, float, double.

C-like languages are designed for speed. Automatically casting everything to doubles on the off chance that is what you want would actually make programs that run slower because they're always casting things to double even when you don't want it. Doubles use more bytes too, meaning you can't have as many working variables in the cpu registers at once if all your variables were cast as doubles. So the CPU would be doing a pile of useless additional effort if the compiler assumed you wanted doubles for literally every maths operation.,The compiler needs to make a decision every time it sees two non-matching variable being added/multiplied etc, it needs to throw a cast in there to make them compatible, and they err on the side of efficiency/speed.

Oh, sure, I'm not denying that.  I'm mostly just saying that if someone is accustomed to scripting languages, it would probably catch them by surprise, and the fix is verbose.  "What?  Why doesn't 1 / 2 give me 0.5?  What do you mean I have to tell the compiler that 2 is a floating point value here?  It's a whole number!"  Of course, this kind of boils down to the difference between strongly and weakly typed languages in general.

And of course, let's not forget that doubles don't solve all math problems like you want anyway.  Good luck representing values like 1/3 as a double or float.  Or in the case of a game, which might use integers over some fixed base to represent fractions.  Diablo II, for example, represents HP in increments of 1/256, so 1 HP is 256.  That way it can very rapidly perform calculations that keep track of tiny amounts of damage or healing.  Instead of using floats or doubles, you just add integers.  Dwarf Fortress does this a lot too.
Logged
Through pain, I find wisdom.
Pages: 1 ... 567 568 [569] 570 571 ... 796