Coding: Fleeting Thoughts

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

User avatar
You, sir, name?
Posts: 6983
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: Coding: Fleeting Thoughts

Postby You, sir, name? » Wed Feb 17, 2010 5:45 am UTC

That, and compiling with -O3 did make it bearably fast. Though it's still significantly slower than what I'd expect from C (but that's perhaps not that strange, as this is sort of computation is something C does exceedingly well.)
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

User avatar
b.i.o
Green is the loneliest number
Posts: 2519
Joined: Fri Jul 27, 2007 4:38 pm UTC
Location: Hong Kong

Re: Coding: Fleeting Thoughts

Postby b.i.o » Wed Feb 17, 2010 5:48 am UTC

TheChewanater wrote:Yes, it's almost certainly what you said, but it just seems silly to me to have it update in "realtime" when by realtime they mean the average amount it goes up per second over the course of a year. Unless you plan to hang around the same page for a year, it makes more sense IMO to just generate some estimate on the server and have it stay that way until the page reloads.

If you're looking at it from a completely technical standpoint then yes, that probably would make more sense. But then it wouldn't catch people's attention, which is the entire point.

lulzfish wrote:Of course. There isn't actually a constant stream of new hard drives being poured into the GMail server 24 hours a day. Of course it's fake.

There's not actually a 'Gmail server', either. On the scale of Google you're a lot more likely to be adding whole servers rather than hard drives.

afarnen
Posts: 157
Joined: Mon May 05, 2008 12:12 pm UTC

Re: Coding: Fleeting Thoughts

Postby afarnen » Wed Feb 17, 2010 9:21 am UTC

Today in french class, we were given some Mardi gras vocabulary, and to my humor, I saw that the french word "char" means "float". Of course, it means a carnival float and not a floating-point number, but I thought it was neat nonetheless. And I was the only person in the room who knew this, unsurprisingly.

User avatar
You, sir, name?
Posts: 6983
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: Coding: Fleeting Thoughts

Postby You, sir, name? » Wed Feb 17, 2010 9:36 am UTC

Speaking of which. How do you guys say "char"? Like "charcoal" or "character". I do the latter, and it bothers me when people do the former.
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

User avatar
Emu*
Posts: 689
Joined: Mon Apr 28, 2008 9:47 am UTC
Location: Cardiff, UK
Contact:

Re: Coding: Fleeting Thoughts

Postby Emu* » Wed Feb 17, 2010 10:05 am UTC

I tend to read char but say character, which can get a bit confusing when Java also has a Character class...

On the other hand I pronounce php as the word "phupp". :wink:
Cosmologicon wrote:Emu* implemented a naive east-first strategy and ran it for an hour, producing results that rivaled many sophisticated strategies, visiting 614 cells. For this, Emu* is awarded Best Deterministic Algorithm!

User avatar
Briareos
Posts: 1940
Joined: Thu Jul 12, 2007 12:40 pm UTC
Location: Town of the Big House

Re: Coding: Fleeting Thoughts

Postby Briareos » Wed Feb 17, 2010 2:39 pm UTC

"Character" since it's short for character.
Sandry wrote:Bless you, Briareos.

Blriaraisghaasghoasufdpt.
Oregonaut wrote:Briareos is my new bestest friend.

User avatar
Xanthir
My HERO!!!
Posts: 5412
Joined: Tue Feb 20, 2007 12:49 am UTC
Location: The Googleplex
Contact:

Re: Coding: Fleeting Thoughts

Postby Xanthir » Wed Feb 17, 2010 4:25 pm UTC

Char as in charcoal. I can't imagine forcing myself to pronounce it as "care".

That has resulted in me slipping up and saying "char(coal)acter" a few times, though, when I'm using that word after having said "char" a lot.
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))

User avatar
lulzfish
Posts: 1214
Joined: Tue Dec 16, 2008 8:17 am UTC

Re: Coding: Fleeting Thoughts

Postby lulzfish » Wed Feb 17, 2010 4:29 pm UTC

I think I use character too, but I rarely talk about code IRL so I honestly can't remember.

User avatar
TheChewanater
Posts: 1279
Joined: Sat Aug 08, 2009 5:24 am UTC
Location: lol why am I still wearing a Santa suit?

Re: Coding: Fleeting Thoughts

Postby TheChewanater » Wed Feb 17, 2010 4:32 pm UTC

You, sir, name? wrote:Speaking of which. How do you guys say "char"? Like "charcoal" or "character". I do the latter, and it bothers me when people do the former.

I pronounce it rhyming with "car". It makes more sense to pronounce it like that, like info. It isn't pronounced infor.
ImageImage
http://internetometer.com/give/4279
No one can agree how to count how many types of people there are. You could ask two people and get 10 different answers.

not baby Newt
Posts: 110
Joined: Wed Feb 03, 2010 11:30 pm UTC

Re: Coding: Fleeting Thoughts

Postby not baby Newt » Wed Feb 17, 2010 7:01 pm UTC

As far as I can tell, the starting (server-sider generated) number is pretty accurate, but the JavaScript stuff that increases it just estimates based on how much their space usually increases.

For the swedish version of gmail.com, the javascript is updated but the hardcoded starting value is still 2757.272164 MB, which I think was accurate a few years ago. It jumps to 7424.5 after half a second. Supposedly it'll be 10996 MB on Sun, 20 Dec 2037.

TheChewanater wrote:
phlip wrote:Well, it's probably just a simple interpolation... like, if averaging shows they're adding x GB per year or whatever, then they can make the counter go up by x/(365.2425*24*60*60) GB per second... and that counting would be just as accurate if it happened client-side or server-side...

Yes, it's almost certainly what you said, but it just seems silly to me to have it update in "realtime" when by realtime they mean the average amount it goes up per second over the course of a year. Unless you plan to hang around the same page for a year, it makes more sense IMO to just generate some estimate on the server and have it stay that way until the page reloads.

You are most likely right, but they *could* use the same method in the code that checks if you're over your quota. You know, just for fun. Actual disk space is probably way lower than the total of the users' quota.

User avatar
phlip
Restorer of Worlds
Posts: 7572
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia
Contact:

Re: Coding: Fleeting Thoughts

Postby phlip » Wed Feb 17, 2010 10:45 pm UTC

There's a Religious Wars thread about code pronounciation...

I don't think anyone in a non-rhotic accent would pronounce "char" as the first syllable of "character"... since non-rhotic accents pronounce it as "cha-rac-te(r)" instead of "char-ac-ter". Also, here at least, that first "a" isn't the vowel from "care"... it's pronounced differently. So if I were to say the first syllable of "character", it'd basically be "cat", but without the t... which doesn't resemble "char" at all.

I had one lecturer who insisted it should be pronounced "car", since "character" isn't pronounced with a "ch"... but still keeping the vowel from "char" rather than "character". It didn't catch on.

Code: Select all

enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};
void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}
[he/him/his]

User avatar
Xeio
Friends, Faidites, Countrymen
Posts: 5101
Joined: Wed Jul 25, 2007 11:12 am UTC
Location: C:\Users\Xeio\
Contact:

Re: Coding: Fleeting Thoughts

Postby Xeio » Wed Feb 17, 2010 11:13 pm UTC

phlip wrote:I had one lecturer who insisted it should be pronounced "car", since "character" isn't pronounced with a "ch"... but still keeping the vowel from "char" rather than "character". It didn't catch on.
Yup, same here, and I'd alway have a few second delay before I realized what she meant.

Also... sequel... yea, that probably screwed up that internship interview real good when I went all "wha?".

User avatar
You, sir, name?
Posts: 6983
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: Coding: Fleeting Thoughts

Postby You, sir, name? » Wed Feb 17, 2010 11:39 pm UTC

If the 28 hour day makes you crazy, imagine what spending it doing Haskell would do. My life has become a haze of logic and higher order functions. It's like I live in the second panel of Lisp.
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

qbg
Posts: 586
Joined: Tue Dec 18, 2007 3:37 pm UTC

Re: Coding: Fleeting Thoughts

Postby qbg » Fri Feb 19, 2010 7:47 pm UTC

This must be the worse problem I have ever coded.

My program's upper bound for runtime is O((2n+1)^n^2).

User avatar
You, sir, name?
Posts: 6983
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: Coding: Fleeting Thoughts

Postby You, sir, name? » Fri Feb 19, 2010 7:54 pm UTC

qbg wrote:This must be the worse problem I have ever coded.

My program's upper bound for runtime is O((2n+1)^n^2).


That is ambiguous. Is it O(((2n+1)^n)^2) = O(e^(2n log(n))) or O((2n+1)^(n^2)) = O(e^(n^2 log(n)))

Though either complexity is pretty damn appalling.
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

User avatar
Cosmologicon
Posts: 1806
Joined: Sat Nov 25, 2006 9:47 am UTC
Location: Cambridge MA USA
Contact:

Re: Coding: Fleeting Thoughts

Postby Cosmologicon » Fri Feb 19, 2010 8:12 pm UTC

You, sir, name? wrote:That is ambiguous. Is it O(((2n+1)^n)^2) = O(e^(2n log(n))) or O((2n+1)^(n^2)) = O(e^(n^2 log(n)))

It's the latter. Exponentiation is right-associative. Always has been. The fact that BASIC and Excel do it the other way simply proves the point, I think. :)

User avatar
You, sir, name?
Posts: 6983
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: Coding: Fleeting Thoughts

Postby You, sir, name? » Fri Feb 19, 2010 8:19 pm UTC

Cosmologicon wrote:
You, sir, name? wrote:That is ambiguous. Is it O(((2n+1)^n)^2) = O(e^(2n log(n))) or O((2n+1)^(n^2)) = O(e^(n^2 log(n)))

It's the latter. Exponentiation is right-associative. Always has been. The fact that BASIC and Excel do it the other way simply proves the point, I think. :)


I come from languages that don't have exponentiation operators.
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

User avatar
'; DROP DATABASE;--
Posts: 3284
Joined: Thu Nov 22, 2007 9:38 am UTC
Location: Midwest Alberta, where it's STILL snowy
Contact:

Re: Coding: Fleeting Thoughts

Postby '; DROP DATABASE;-- » Sat Feb 20, 2010 9:10 am UTC

Random thought:

PHP is the English of programming languages. It works well, is so widely-used that it's a de-facto standard, doesn't have too many different tokens, not too hard to read or write. However, it's a mess, grammatically, as it just takes things from other languages and libraries and slaps them in without adopting them to conform to its own rules, which are extremely loose. So you have a lot of inconsistencies, as function names may be named_like_this or NamedLikeThis or LikeThisNamed or nlt; whatever the original library's convention was. It was put together without a whole lot of thought at the time, and has sort of grown organically, but is now so popular that fixing it would break a LOT of existing code.
Also, it's not statically typed, so you can freely cast numbers, strings, etc. Just as in English, you can just about use any type of word as a noun, verb, etc in the right context. (As some might say, you can verb anything you want!)

Lua might be considered analogous to a good artificial language. Very simple, well thought out, but not especially popular (though it is steadily increasing in popularity), a bit lacking in some areas (you'll want the hex patch if you're dealing with raw binary or memory/file addresses, and no way to guarantee order of table keys will annoy you at times), and lacks some shortcuts (such as x++ or x += 1) in the interest of simplicity.

Perl might be compared to Chinese. I don't know either, but both are stereotyped (somewhat correctly) as extremely difficult to read and write and having a lot of symbols. If you actually know them, as I'm told, they're not bad, but they can take a long time to learn.

I don't know Korean either, but from what I've heard of it, it could be compared to C: pretty simple, designed with a fair bit of thought and some nice features, efficient, with strongly-defined rules, but difficult grammar and general simplicity make learning it, and actually using it for complex tasks, a little harder than other languages. (My understanding is that the writing system was designed by some pretty intelligent people, but the words and grammar grew on their own as with most languages. I could be wrong.)

I guess you might compare Assembly to, rather than telling someone your idea, taking them somewhere to show them? :P Or just explaining every little detail, but that's not a language...
poxic wrote:You suck. And simultaneously rock. I think you've invented a new state of being.

User avatar
You, sir, name?
Posts: 6983
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: Coding: Fleeting Thoughts

Postby You, sir, name? » Sat Feb 20, 2010 11:35 am UTC

I guess that Lisp would be Latin then. Old language, popular with yesteryear's scholars, and chiefly used by religious people (well, in Lisp's case, EMACS users) and opinionated people with long beards.
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

User avatar
TheChewanater
Posts: 1279
Joined: Sat Aug 08, 2009 5:24 am UTC
Location: lol why am I still wearing a Santa suit?

Re: Coding: Fleeting Thoughts

Postby TheChewanater » Sat Feb 20, 2010 7:15 pm UTC

On the topic of language/programming language analogies, APL is whatever language cats use when they meow.
ImageImage
http://internetometer.com/give/4279
No one can agree how to count how many types of people there are. You could ask two people and get 10 different answers.

User avatar
Briareos
Posts: 1940
Joined: Thu Jul 12, 2007 12:40 pm UTC
Location: Town of the Big House

Re: Coding: Fleeting Thoughts

Postby Briareos » Sat Feb 20, 2010 7:52 pm UTC

'; DROP DATABASE;-- wrote:I guess you might compare Assembly to, rather than telling someone your idea, taking them somewhere to show them? :P Or just explaining every little detail, but that's not a language...
T-Rex wrote:Oh Utahraptor, if only we had descriptive language; if only I could describe something using my words. But you know as well as I that I can only point and say, lookit.
Sandry wrote:Bless you, Briareos.

Blriaraisghaasghoasufdpt.
Oregonaut wrote:Briareos is my new bestest friend.

fazzone
Posts: 186
Joined: Wed Dec 10, 2008 9:38 pm UTC
Location: A boat

Re: Coding: Fleeting Thoughts

Postby fazzone » Sat Feb 20, 2010 8:09 pm UTC

Then Java is legalese.
*/

User avatar
headprogrammingczar
Posts: 3072
Joined: Mon Oct 22, 2007 5:28 pm UTC
Location: Beaming you up

Re: Coding: Fleeting Thoughts

Postby headprogrammingczar » Sat Feb 20, 2010 11:28 pm UTC

Monads are breaking my brain. I'll get to the point where I can half-understand them, then I just get confused again. So far they just seem like a way of storing and returning multiple values. I can't see how they relate to state, except as a container for extra arguments to pass to functions.
<quintopia> You're not crazy. you're the goddamn headprogrammingspock!
<Weeks> You're the goddamn headprogrammingspock!
<Cheese> I love you

User avatar
Berengal
Superabacus Mystic of the First Rank
Posts: 2707
Joined: Thu May 24, 2007 5:51 am UTC
Location: Bergen, Norway
Contact:

Re: Coding: Fleeting Thoughts

Postby Berengal » Sun Feb 21, 2010 12:53 am UTC

Really, monads in themselves aren't actually that interesting. The interesting things are all the things that are monads. Turns out, if you abstract enough, things like lists, functions, webservers and parsers all share a common interface (return and bind, with laws), and thus share certain properties which it makes sense to put in a common library. Like, for example, for all monads 'm', all traversible functors 't' and all types 'a' if you have a value of type `t (m a)' you can commute the 't' and 'm' to get a value of type `m (t a)'. This again means that if you've got a tree of parsers of some type 'a' you can turn that into a parser of trees of some type 'a'.

Anyway, asking what monads are is useless. Better to ask why monads, to which the answer is that there's lots of them, so they seem like a nice part to factor out for themselves. Alone they're pretty much useless.
It is practically impossible to teach good programming to students who are motivated by money: As potential programmers they are mentally mutilated beyond hope of regeneration.

User avatar
TheChewanater
Posts: 1279
Joined: Sat Aug 08, 2009 5:24 am UTC
Location: lol why am I still wearing a Santa suit?

Re: Coding: Fleeting Thoughts

Postby TheChewanater » Sun Feb 21, 2010 2:09 am UTC

My mom just looked at my laptop and asked who I was spying on. After asking why she thought I was spying, she said, "Look, render spy!" and pointed to a file called "render.py" on the screen.
ImageImage
http://internetometer.com/give/4279
No one can agree how to count how many types of people there are. You could ask two people and get 10 different answers.

User avatar
lulzfish
Posts: 1214
Joined: Tue Dec 16, 2008 8:17 am UTC

Re: Coding: Fleeting Thoughts

Postby lulzfish » Sun Feb 21, 2010 7:39 am UTC

TheChewanater wrote:My mom just looked at my laptop and asked who I was spying on. After asking why she thought I was spying, she said, "Look, render spy!" and pointed to a file called "render.py" on the screen.

Ha. There should be a Python dialect called Spython. I don't what it would do.

User avatar
You, sir, name?
Posts: 6983
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: Coding: Fleeting Thoughts

Postby You, sir, name? » Sun Feb 21, 2010 9:46 am UTC

headprogrammingczar wrote:Monads are breaking my brain. I'll get to the point where I can half-understand them, then I just get confused again. So far they just seem like a way of storing and returning multiple values. I can't see how they relate to state, except as a container for extra arguments to pass to functions.


This. Very much this.

Berengal wrote:Anyway, asking what monads are is useless. Better to ask why monads, to which the answer is that there's lots of them, so they seem like a nice part to factor out for themselves. Alone they're pretty much useless.


So... the monads that can be understood are not the true monads?
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

User avatar
headprogrammingczar
Posts: 3072
Joined: Mon Oct 22, 2007 5:28 pm UTC
Location: Beaming you up

Re: Coding: Fleeting Thoughts

Postby headprogrammingczar » Tue Feb 23, 2010 12:05 am UTC

Moving away from monads for a second: is it possible to catch exceptions thrown by read? I've looked into how Haskell normally handles exceptions and all the types are incompatible.
<quintopia> You're not crazy. you're the goddamn headprogrammingspock!
<Weeks> You're the goddamn headprogrammingspock!
<Cheese> I love you

User avatar
Berengal
Superabacus Mystic of the First Rank
Posts: 2707
Joined: Thu May 24, 2007 5:51 am UTC
Location: Bergen, Norway
Contact:

Re: Coding: Fleeting Thoughts

Postby Berengal » Tue Feb 23, 2010 12:21 am UTC

headprogrammingczar wrote:Moving away from monads for a second: is it possible to catch exceptions thrown by read? I've looked into how Haskell normally handles exceptions and all the types are incompatible.

Yes, but you don't really want to. See, evaluation order is not specified in Haskell (no, it's not lazy. It has lazy semantics, but you can get the same semantics out of strict evaluation and a halting oracle, or a more practical alternative, eager evaluation). This means that you can't really set up an exception handler around pure expressions since any exception it might cause may not be thrown while it's inside the handled code, but hidden inside a value that will later cause the exception in an entirely different part of the code. Exactly when this happens depends not only on the implementation you use, but your entire program taken as a whole.

It's for this reason you should treat any exceptions thrown by partial functions the same as infinite loops, and use `reads' instead of `read' if you want to recover from parse errors.
It is practically impossible to teach good programming to students who are motivated by money: As potential programmers they are mentally mutilated beyond hope of regeneration.

User avatar
headprogrammingczar
Posts: 3072
Joined: Mon Oct 22, 2007 5:28 pm UTC
Location: Beaming you up

Re: Coding: Fleeting Thoughts

Postby headprogrammingczar » Tue Feb 23, 2010 1:11 am UTC

Cool. reads was exactly what I was looking for.
<quintopia> You're not crazy. you're the goddamn headprogrammingspock!
<Weeks> You're the goddamn headprogrammingspock!
<Cheese> I love you

User avatar
TheChewanater
Posts: 1279
Joined: Sat Aug 08, 2009 5:24 am UTC
Location: lol why am I still wearing a Santa suit?

Re: Coding: Fleeting Thoughts

Postby TheChewanater » Thu Feb 25, 2010 1:16 am UTC

Stupid apt.

Code: Select all

Version of g++-4.4 on system is 4.4.3-2ubuntu1.
ImageImage
http://internetometer.com/give/4279
No one can agree how to count how many types of people there are. You could ask two people and get 10 different answers.

User avatar
wizardy42
Posts: 83
Joined: Fri Dec 26, 2008 8:53 am UTC

Re: Coding: Fleeting Thoughts

Postby wizardy42 » Thu Feb 25, 2010 9:35 am UTC

.
Last edited by wizardy42 on Wed Mar 18, 2015 3:18 am UTC, edited 1 time in total.

User avatar
hotaru
Posts: 1045
Joined: Fri Apr 13, 2007 6:54 pm UTC

Re: Coding: Fleeting Thoughts

Postby hotaru » Thu Feb 25, 2010 9:52 am UTC

wizardy42 wrote:I started a computer science class today. Woot! But I know nothing. Woot!

Why does -17%10 give you 3? Whhhyyyyyyyy?

because (-17/10) * 10 + (-17%10) has to be -17 (where "/" is integer division) and whatever language you're using rounds down for integer division instead of truncating (rounding toward 0).

Code: Select all

factorial product enumFromTo 1
isPrime n 
factorial (1) `mod== 1

User avatar
wizardy42
Posts: 83
Joined: Fri Dec 26, 2008 8:53 am UTC

Re: Coding: Fleeting Thoughts

Postby wizardy42 » Thu Feb 25, 2010 10:03 am UTC

.
Last edited by wizardy42 on Wed Mar 18, 2015 3:17 am UTC, edited 1 time in total.

User avatar
phlip
Restorer of Worlds
Posts: 7572
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia
Contact:

Re: Coding: Fleeting Thoughts

Postby phlip » Thu Feb 25, 2010 10:03 am UTC

Personally I prefer it that way around... it makes modular arithmetic easier if, say, (horrible formula) % 10 is always between 0 and 9... without having to worry about whether that horrible formula is negative.

Happily, Haskell has both... though I can never remember which is which without trying it. div rounds towards negative infinity and mod has the same sign as the second operand... quot rounds towards zero and rem has the same sign as the first operand. (/) is floating division.

Code: Select all

enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};
void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}
[he/him/his]

User avatar
lulzfish
Posts: 1214
Joined: Tue Dec 16, 2008 8:17 am UTC

Re: Coding: Fleeting Thoughts

Postby lulzfish » Thu Feb 25, 2010 2:39 pm UTC

wizardy42 wrote:...I say again; WHHHHHYYYYYY?

Because the largest multiple of 10 that is less than -17 would be -20, and -17 - -20 = -17 + 20 = 3.
It's very simple.
Er, SIMPLEEEEEEEEEEEEEEE

User avatar
Xanthir
My HERO!!!
Posts: 5412
Joined: Tue Feb 20, 2007 12:49 am UTC
Location: The Googleplex
Contact:

Re: Coding: Fleeting Thoughts

Postby Xanthir » Thu Feb 25, 2010 4:47 pm UTC

wizardy42 wrote:I started a computer science class today. Woot! But I know nothing. Woot!

Why does -17%10 give you 3? Whhhyyyyyyyy?

Because it's the correct value. Don't let the 7 throw you. You're dealing with negative numbers, which are backwards.

If you add an integer multiple of the modulus to a number, it won't change the result of modding it by that modulus. That's a basic invariant of modular arithmetic. 3%10 = 3, 13%10 = 3, 23%10 = 3, and so on. Going in the other direction, 3 - 10 = -7, so -7%10 = 3, -17%10 = 3, -27%10 = 3, etc.

Some languages annoyingly have special behavior for negative numbers. They'll return the lowest negative number with that modulus when modding a negative number, so -17%10 would return -7. This is basically never useful, and is the reason so many language-agnostic things using mod in some algorithm express it as (((num mod n) + n) mod n), so that if you happen to be given a negative number it'll get corrected into a positive one, while positive numbers won't be affected. (This pattern appears all throughout CSS specs, for example.)
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))

User avatar
phlip
Restorer of Worlds
Posts: 7572
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia
Contact:

Re: Coding: Fleeting Thoughts

Postby phlip » Thu Feb 25, 2010 10:41 pm UTC

Xanthir wrote:This is basically never useful

It's not a particularly useful behavior for mod, but it does mean a more useful behavior for division... it means that (-a) / b == -(a / b), which is otherwise not necessarily the case (for the round-towards-negative-infinity option, (-a) / b == -(a / b) - (a%b == 0 ? 0 : 1)).

In short, it's not possible to have all of:
  1. (a/b) * b + (a%b) == a
  2. (-a) / b == -(a / b)
  3. 0 <= a%b < b, for positive b
Some languages break rule 2, some break rule 3. A language that breaks rule 1 is arguably less useful than both.

Code: Select all

enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};
void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}
[he/him/his]

Osha
Posts: 727
Joined: Wed Dec 03, 2008 3:24 am UTC
Location: Boise, Idaho, USA

Re: Coding: Fleeting Thoughts

Postby Osha » Fri Feb 26, 2010 10:58 pm UTC

Today in things that took me way too long to figure out:

Code: Select all

#include <stdio.h>
#include <stdint.h>

int main(int argc,char** argv) {
   uint16_t a;
   uint8_t& b = *((uint8_t*)(&a));
    uint8_t& c = *((uint8_t*)(&a)+1);
   a = 767;
   printf("a: %d, b: %d, c: %d\n",a,b,c);
   return 0;
}


Making registers for an emulator project! Different registers may refer to the same thingy but say the left or right half! :o

User avatar
headprogrammingczar
Posts: 3072
Joined: Mon Oct 22, 2007 5:28 pm UTC
Location: Beaming you up

Re: Coding: Fleeting Thoughts

Postby headprogrammingczar » Sat Feb 27, 2010 12:26 am UTC

Multi-size registers lead to some awesome hacks, especially if what you need is (x mod 2**y).
<quintopia> You're not crazy. you're the goddamn headprogrammingspock!
<Weeks> You're the goddamn headprogrammingspock!
<Cheese> I love you


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 7 guests