1275: "int(pi)"

This forum is for the individual discussion thread that goes with each new comic.

Moderators: Moderators General, Prelates, Magistrates

User avatar
orthogon
Posts: 3099
Joined: Thu May 17, 2012 7:52 am UTC
Location: The Airy 1830 ellipsoid

Re: 1275: "int(pi)"

Postby orthogon » Wed Oct 09, 2013 3:55 pm UTC

A little-known fact is that std::vector has no element 13 owing to a powerful superstitious lobby in the standardisation committee*. This is the main reason why iterators were invented: they safely skip a missing element, whereas using the [] operator can lead to subtle bugs.

*In response, representatives of various administrations insisted on their own superstitions being catered for, so elements 4, 7, 666 etc. might be absent instead, depending on the locale.
xtifr wrote:... and orthogon merely sounds undecided.

User avatar
Red Hal
Magically Delicious
Posts: 1445
Joined: Wed Nov 28, 2007 2:42 pm UTC

Re: 1275: "int(pi)"

Postby Red Hal » Wed Oct 09, 2013 4:01 pm UTC

Eternal Density wrote:I rewrote all of Sandcastle Builder's code to conform to this.

This delayed the release of version 1.81. I hope GLR is happy.
I ... I think I'm a little bit in love with you.
Lost Greatest Silent Baby X Y Z. "There is no one who loves pain itself, who seeks after it and wants to have it, simply because it is pain..."

ctdonath
Posts: 198
Joined: Wed Feb 08, 2012 2:40 pm UTC

Re: 1275: "int(pi)"

Postby ctdonath » Wed Oct 09, 2013 4:14 pm UTC

cheeseheadtotherescue wrote:want the order of magnitude to be within an order of magnitude (wrap you're head around that!))

Heh. Yeah, lately I've been using (and looking for opportunities to use) the term "orders of magnitude of orders of magnitude".

User avatar
Link
Posts: 1419
Joined: Sat Mar 07, 2009 11:33 am UTC
Location: ᘝᓄᘈᖉᐣ
Contact:

Re: 1275: "int(pi)"

Postby Link » Wed Oct 09, 2013 5:20 pm UTC

Here's a vastly overcomplicated fully compile-time C++11 version that uses no predefined symbolic constants (though it does define some of its own) and uses only 0 and 1 as numeric constants:

Code: Select all

constexpr long double one = static_cast<long double> (1);
constexpr long double four = static_cast<long double> ((1 << 1) << 1);

constexpr long double pi_series (std::size_t k = 1 << (1 << (1 << (1 << 1))) >> 1)
{
   return (k ? (four / (static_cast<long double> (k << 1) + one) * (((k & 1) == 0) ? one : -one) + pi_series (k - 1)) : four);
}

constexpr long double npow (long double base, std::size_t exponent)
{
   return (exponent ? (base * npow (base, exponent - 1)) : one);
}

constexpr long double pi = pi_series ();

constexpr long double volume (long double r)
{
   return four/static_cast<int> (pi)*pi*npow (r, static_cast<std::size_t> (pi));
}

Gargravarr
Posts: 74
Joined: Mon Dec 21, 2009 8:34 am UTC

Re: 1275: "int(pi)"

Postby Gargravarr » Wed Oct 09, 2013 5:54 pm UTC

orthogon wrote:
Gargravarr wrote:The PI rule of project estimation: Analyze the project and calculate a reasonable estimate of how much time it should take. Then multiply with PI.

That's way too optimistic. The one I heard was "double it and move to the next larger unit of time", e.g. 3 days -> 6 weeks, 2 months->4 years, etc.

The problem is how to make a customer sign a deal like that.
"How can it take 6 days to add an FB Like button? Didn't you say a couple of hours?"
"I assume that before we're done, you'll discover Twitter, Disqus, and half a dozen other social media, and complain that that isn't included in 'FaceBook in general'"
"I see. Don't call us, we'll call you".

User avatar
Quizatzhaderac
Posts: 1821
Joined: Sun Oct 19, 2008 5:28 pm UTC
Location: Space Florida

Re: 1275: "int(pi)"

Postby Quizatzhaderac » Wed Oct 09, 2013 6:11 pm UTC

All hard coded numbers are cursed, except zero and one. Here's the ridiculous way I'd do it.

Code: Select all

/**
 * Determines the volume of a sphere, in meters sphered
 */
double Sphere::volume() {
   //arbitrary powers are for problems with an arbitary number of dimensions
   return this->crossSection.area() * this->radius;
}

/**
 * Differs only from Square's method in units, as it's impossible to square a circle
 */
double Circle::area() {
   return this->radius * this->radius;
}
The thing about recursion problems is that they tend to contain other recursion problems.

time burglar
Posts: 20
Joined: Thu Apr 25, 2013 9:26 pm UTC

Re: 1275: "int(pi)"

Postby time burglar » Wed Oct 09, 2013 6:22 pm UTC

orthogon wrote:This reminded me of the techniques used on the ZX81 and ZX Spectrum, where "1" was often replaced by PI/PI and "0" by PI-PI.

The BASIC programs were tokenized on entry and stored in tokenized form. Numeric literals were stored as an escape byte followed by four bytes of floating point. Keywords were a single byte in the high ASCII range; these didn't even need to be scanned since they were entered with a single keypress. It was beautiful. The upshot was that a "1" took up 5 bytes, whereas PI/PI only took three. Bear in mind that you only had 1KB of memory to fit your program and data.


Lordy yes, I still reminisce about this stuff. The time I typed in a ZX81 program from a magazine, which used this trickery, was probably my first experience of feeling bewilderment/revulsion at someone's coding style (I was 9, I think!) Wasn't it SGN PI (i.e. sign of pi) for +1, in just 2 bytes?

Also didn't they store the literal ASCII digits in addition to the floating point format? I think they must've, because otherwise if you entered 0.1 (which can't be represented exactly as floating point), then when you LISTed the program it would have to display something like 0.0999994. I'm sure I remember realising that on the spectrum I could obfuscate my code by poking the stored BASIC, so e.g. a RANDOMIZE USR 40000 could actually be calling machine code at a different address. :D

JustMe
Posts: 28
Joined: Wed Sep 10, 2008 2:21 pm UTC
Location: Washington (the real one - not D.C)

Re: 1275: "int(pi)"

Postby JustMe » Wed Oct 09, 2013 7:03 pm UTC

Back to "why is 3 cursed?" I think it's because 3 and E are close on the keyboard so it's easy to type e when you mean 3. But, when you try to find it, your brain sees E as 3 because that's what you MEANT. So avoiding 3 avoids that problem.
Or maybe it's just me.
I have traveled from 1963 to be a member of the unofficial board Council of Elders. Phear M3

Kit.
Posts: 1117
Joined: Thu Jun 16, 2011 5:14 pm UTC

Re: 1275: "int(pi)"

Postby Kit. » Wed Oct 09, 2013 7:23 pm UTC

Gargravarr wrote:The problem is how to make a customer sign a deal like that.
"How can it take 6 days to add an FB Like button? Didn't you say a couple of hours?"

It's not for your customer. Your customer should never hear the "a couple of hours".

Unless your immediate management is your customer. But then it's what they are stuck with anyway.

modus_ponens
Posts: 2
Joined: Mon Mar 25, 2013 8:55 pm UTC

Re: 1275: "int(pi)"

Postby modus_ponens » Wed Oct 09, 2013 7:27 pm UTC

My first thoughts upon reading this comic were of a programmer who is trying to avoid being nitpicked at a code review of having "magic constants" or "bare numbers" in their code.

A common practice in big software projects is to have peers review code before it is allowed into the production code base. In some teams, the process is taken seriously and the reviewers look critically at the code for various flaws. In other teams, the peers might not be very bright and it is a haphazard attempt to improve the quality of the code by picking at trivialities.

Whenever bare numbers appear in code, a code reviewer immediately gets suspicious and is likely to reject the code. By using these obfuscation techniques, the careless code reviewer might be fooled and let it pass, and the clever programmer gets the code approved.

User avatar
orthogon
Posts: 3099
Joined: Thu May 17, 2012 7:52 am UTC
Location: The Airy 1830 ellipsoid

Re: 1275: "int(pi)"

Postby orthogon » Wed Oct 09, 2013 7:28 pm UTC

time burglar wrote:
orthogon wrote:This reminded me of the techniques used on the ZX81 and ZX Spectrum, where "1" was often replaced by PI/PI and "0" by PI-PI.

The BASIC programs were tokenized on entry and stored in tokenized form. Numeric literals were stored as an escape byte followed by four bytes of floating point. Keywords were a single byte in the high ASCII range; these didn't even need to be scanned since they were entered with a single keypress. It was beautiful. The upshot was that a "1" took up 5 bytes, whereas PI/PI only took three. Bear in mind that you only had 1KB of memory to fit your program and data.


Lordy yes, I still reminisce about this stuff. The time I typed in a ZX81 program from a magazine, which used this trickery, was probably my first experience of feeling bewilderment/revulsion at someone's coding style (I was 9, I think!) Wasn't it SGN PI (i.e. sign of pi) for +1, in just 2 bytes?

Also didn't they store the literal ASCII digits in addition to the floating point format? I think they must've, because otherwise if you entered 0.1 (which can't be represented exactly as floating point), then when you LISTed the program it would have to display something like 0.0999994. I'm sure I remember realising that on the spectrum I could obfuscate my code by poking the stored BASIC, so e.g. a RANDOMIZE USR 40000 could actually be calling machine code at a different address. :D

I bow down before a true master. SGN PI for the win! I do believe you're right on all counts. I was trying to figure out how it dealt with the 0.1 issue.
RANDOMIZE USR was another of those idioms, if that isn't too kind a word. USR x was a function that called the machine code at address x and returned the contents of the accumulator. You couldn't just ignore the return value; you had to do something with it. Setting the random number seed was the cheapest thing you could do, costing just one byte. Ok, there might have been bizarre side effects if you used random numbers as well, but hey.
xtifr wrote:... and orthogon merely sounds undecided.

speising
Posts: 2363
Joined: Mon Sep 03, 2012 4:54 pm UTC
Location: wien

Re: 1275: "int(pi)"

Postby speising » Wed Oct 09, 2013 7:31 pm UTC

modus_ponens wrote:In other teams, the peers might not be very bright...

then they are no peers of mine!

User avatar
azule
Saved
Posts: 2132
Joined: Mon Jul 26, 2010 9:45 pm UTC
Location: The land of the Golden Puppies and Rainbows

Re: 1275: "int(pi)"

Postby azule » Wed Oct 09, 2013 7:36 pm UTC

smileyy wrote:This expression is really slow on my computer, because it wants to calculate the last digit of pi before rounding. Does anyone know a way to optimize this?
Use a faster computer. *badoom-tish*


Seriously...we gotta know what language, silly.
Image

If you read this sig, post about one arbitrary thing you did today.

I celebrate up to six arbitrary things before breakfast.
Time does drag on and on and contain spoilers. Be aware of memes.

User avatar
xkcdfan
Posts: 140
Joined: Wed Jul 30, 2008 5:10 am UTC

Re: 1275: "int(pi)"

Postby xkcdfan » Wed Oct 09, 2013 7:51 pm UTC

Does anybody know what floor(0.99999....) is?

User avatar
gmalivuk
GNU Terry Pratchett
Posts: 26818
Joined: Wed Feb 28, 2007 6:02 pm UTC
Location: Here and There
Contact:

Re: 1275: "int(pi)"

Postby gmalivuk » Wed Oct 09, 2013 8:00 pm UTC

cheeseheadtotherescue wrote:Astronomers often take pi to be 3 (or even sometimes 1) and pi^2 to be 10 when dealing with large numbers
Using pi^2=10 actually gets you extraordinarily close, order-of-magnitude wise. Since log10pi = 0.497, treating pi as half an order of magnitude introduces less error than, say, rounding 10.3 down to 10 in a calculation.

(4/3 is very close to 10^(1/8), as well, so can be treated as approximately the 4th root of pi or 1/8 of an order of magnitude, as needed, if you're working with spheres and want to get a bit closer than basic orders of magnitude.)
Unless stated otherwise, I do not care whether a statement, by itself, constitutes a persuasive political argument. I care whether it's true.
---
If this post has math that doesn't work for you, use TeX the World for Firefox or Chrome

(he/him/his)

Kinitawowi
Posts: 1
Joined: Wed Oct 09, 2013 8:02 pm UTC

Re: 1275: "int(pi)"

Postby Kinitawowi » Wed Oct 09, 2013 8:12 pm UTC

orthogon wrote:I bow down before a true master. SGN PI for the win! I do believe you're right on all counts. I was trying to figure out how it dealt with the 0.1 issue.
RANDOMIZE USR was another of those idioms, if that isn't too kind a word. USR x was a function that called the machine code at address x and returned the contents of the accumulator. You couldn't just ignore the return value; you had to do something with it. Setting the random number seed was the cheapest thing you could do, costing just one byte. Ok, there might have been bizarre side effects if you used random numbers as well, but hey.

It was also NOT PI for 0, rather than PI-PI. And it wasn't the accumulator's value that USR returned, it was the BC register.

But hey, what are we to expect from someone who read Sinclair User? Your Sinclair FTW. (Yeah, I'm going there. :p)

Using RANDOMIZE was the most common approach. Lots of games with BASIC loaders did it as PRINT USR (to get around the random number seed issue; it didn't matter that it would eventually affect the screen because you'd never leave the machine code routine), and I definitely remember seeing "LET E=USR x" in a couple of type-ins back in the day.

The 0.1 issue was always funky, particularly on FOR-NEXT loops; if you did "10 FOR X=0 TO 100 STEP 0.1" (as I had to in a maths class once - probably something to do with numerical approximations for integration), it'll eventually start losing track of the LSBs and end up overcounting by a step to say 100.9999999994. (Workaround: "10 FOR Y=0 TO 1000" "20 LET X=Y/10")

User avatar
Quizatzhaderac
Posts: 1821
Joined: Sun Oct 19, 2008 5:28 pm UTC
Location: Space Florida

Re: 1275: "int(pi)"

Postby Quizatzhaderac » Wed Oct 09, 2013 9:38 pm UTC

xkcdfan wrote:Does anybody know what floor(0.99999....) is?
Syntax error. Recurring decimals aren't a valid concept in any language I know.
The thing about recursion problems is that they tend to contain other recursion problems.

User avatar
Pfhorrest
Posts: 5473
Joined: Fri Oct 30, 2009 6:11 am UTC
Contact:

Re: 1275: "int(pi)"

Postby Pfhorrest » Wed Oct 09, 2013 9:41 pm UTC

xkcdfan wrote:Does anybody know what floor(0.99999....) is?

The closest integer less than or equal to (0.999...) is (0.999...).

(Incidentally, are there any languages out there which allow you to set something explicitly to the value of an infinitely repeating decimal, i.e. not just set it to an equivalent ratio? E.g to assign x = 0.137137... without first having to figure out what ratio expands to 0.137137... and set x = that).

EDIT: Ninja'd answer to my question. Thanks Quizatzhaderac.
Forrest Cameranesi, Geek of All Trades
"I am Sam. Sam I am. I do not like trolls, flames, or spam."
The Codex Quaerendae (my philosophy) - The Chronicles of Quelouva (my fiction)

User avatar
gmalivuk
GNU Terry Pratchett
Posts: 26818
Joined: Wed Feb 28, 2007 6:02 pm UTC
Location: Here and There
Contact:

Re: 1275: "int(pi)"

Postby gmalivuk » Wed Oct 09, 2013 9:45 pm UTC

Repeating decimals are equal to the fraction of what gets repeated over that many nines. You don't need to figure anything *extra* out, just do math on 137/999.
Unless stated otherwise, I do not care whether a statement, by itself, constitutes a persuasive political argument. I care whether it's true.
---
If this post has math that doesn't work for you, use TeX the World for Firefox or Chrome

(he/him/his)

User avatar
koreiryuu
Posts: 13
Joined: Mon Feb 07, 2011 7:56 am UTC
Location: Southwest Louisiana
Contact:

Re: 1275: "int(pi)"

Postby koreiryuu » Wed Oct 09, 2013 10:21 pm UTC

I miss the intelligent, clever humor that didn't involve an applied mathematics focus or programming specialty to understand :(

I mean, I'm not making any suggestions. Just throwing my feelings out there. It's your comics, there are plenty of programmers and engineers who get your jokes, I'm not trying to deprive anyone of a good laugh.
Wilson: "How'd you get here?"
House: "Osmosis."

User avatar
Eternal Density
Posts: 5588
Joined: Thu Oct 02, 2008 12:37 am UTC
Contact:

Yes that just replaces a 3 with 2 more 3s :P

Postby Eternal Density » Wed Oct 09, 2013 11:26 pm UTC

smileyy wrote:This expression is really slow on my computer, because it wants to calculate the last digit of pi before rounding. Does anyone know a way to optimize this?
Try floor(1!/2!+2!/3!+3!/2!)


Hmm, so if n = floor(1!/2!+2!/n!+n!/2!), n could be either 2 or 3 :P
Play the game of Time! castle.chirpingmustard.com Hotdog Vending Supplier But what is this?
In the Marvel vs. DC film-making war, we're all winners.

User avatar
gmalivuk
GNU Terry Pratchett
Posts: 26818
Joined: Wed Feb 28, 2007 6:02 pm UTC
Location: Here and There
Contact:

Re: 1275: "int(pi)"

Postby gmalivuk » Wed Oct 09, 2013 11:39 pm UTC

koreiryuu wrote:I miss the intelligent, clever humor that didn't involve an applied mathematics focus or programming specialty to understand :(

I mean, I'm not making any suggestions. Just throwing my feelings out there. It's your comics, there are plenty of programmers and engineers who get your jokes, I'm not trying to deprive anyone of a good laugh.
If you remember middle school geometry and you can guess that int( ) has something to do with nearby integers you can get the joke. Much of this thread has been at a much higher level of computer science discussion, but that doesn't really count for appreciating the comic.

(And there's really nothing to miss, either. I don't think early comics were any less likely to contain the occasional bit of actual math than recent ones have been.)
Unless stated otherwise, I do not care whether a statement, by itself, constitutes a persuasive political argument. I care whether it's true.
---
If this post has math that doesn't work for you, use TeX the World for Firefox or Chrome

(he/him/his)

keldor
Posts: 81
Joined: Thu Jan 26, 2012 9:18 am UTC

Re: 1275: "int(pi)"

Postby keldor » Thu Oct 10, 2013 12:57 am UTC

JustMe wrote:Back to "why is 3 cursed?" I think it's because 3 and E are close on the keyboard so it's easy to type e when you mean 3. But, when you try to find it, your brain sees E as 3 because that's what you MEANT. So avoiding 3 avoids that problem.
Or maybe it's just me.


But that's easy to fix! E~=2.718281828459, so you just need to do ceil(E) = ceil(3) = 3, thereby fixing the problem if you accidently substitute an E for the 3.

gormster
Posts: 233
Joined: Mon Jul 23, 2007 6:43 am UTC
Location: Sydney

Re: 1275: "int(pi)"

Postby gormster » Thu Oct 10, 2013 1:10 am UTC

SomeoneSomewhere wrote:Just don't use PHP


Good advice in general.



cellocgw wrote:
Monika wrote:Is there even any language where ^ (if it exists at all) does not mean XOR and does mean "to the power of"?


Seriously? All {for sufficiently architected values of "all"} higher-level languages use '^' for exponentiation. R, MatLab, and IDL are floor(pi) such examples.


Let's see...

C, C++, C#, Objective-C, Java, Python, Ruby, JavaScript, PHP, Perl... that's github's top ten languages, and they ALL use ^ to mean "bitwise XOR".

ALL OF THEM.
Eddie Izzard wrote:And poetry! Poetry is a lot like music, only less notes and more words.

Harry Voyager
Posts: 52
Joined: Thu Nov 04, 2010 7:55 am UTC

Re: 1275: "int(pi)"

Postby Harry Voyager » Thu Oct 10, 2013 1:39 am UTC

phlip wrote:
trwrt wrote:This code doesn't work correctly. Because of the parens around (4/int(pi)) the expression will be evaluated as an integer, yielding 1 instead of 1.3333... Only after the division does the result get promoted to float for the rest of the operations. If it read 4/int(pi)*pi*r^int(pi) then it would work because everything would get promoted to float beforehand.

Well, that depends on the language in question. Some languages will treat the division of two ints as an int division, others as a float division (and have some other syntax for performing an int division). If you're going to assume we're using a specific language, you may as well complain that "^" is bitwise XOR instead of the power function, or that it should be "(int)x" instead of "int(x)" also the constant is called "M_PI" and "func(x) = expr" isn't the valid C function declaration syntax.


Matlab, for example, will convert everything into doubles, unless someone has declared Pi as an object before you've entered this method, in which case, God help you.

User avatar
PM 2Ring
Posts: 3715
Joined: Mon Jan 26, 2009 3:19 pm UTC
Location: Sydney, Australia

Re: 1275: "int(pi)"

Postby PM 2Ring » Thu Oct 10, 2013 2:21 am UTC

gmalivuk wrote:
cheeseheadtotherescue wrote:Astronomers often take pi to be 3 (or even sometimes 1) and pi^2 to be 10 when dealing with large numbers
Using pi^2=10 actually gets you extraordinarily close, order-of-magnitude wise. Since log10pi = 0.497, treating pi as half an order of magnitude introduces less error than, say, rounding 10.3 down to 10 in a calculation.

(4/3 is very close to 10^(1/8), as well, so can be treated as approximately the 4th root of pi or 1/8 of an order of magnitude, as needed, if you're working with spheres and want to get a bit closer than basic orders of magnitude.)


pi ~= sqrt(10) has a venerable tradition, and some ancient Indian mathematicians believed that pi was exactly equal to sqrt(10).

...

gormster wrote:
SomeoneSomewhere wrote:Just don't use PHP


Good advice in general.

Indeed.

gormster wrote:
cellocgw wrote:
Monika wrote:Is there even any language where ^ (if it exists at all) does not mean XOR and does mean "to the power of"?


Seriously? All {for sufficiently architected values of "all"} higher-level languages use '^' for exponentiation. R, MatLab, and IDL are floor(pi) such examples.


Let's see...

C, C++, C#, Objective-C, Java, Python, Ruby, JavaScript, PHP, Perl... that's github's top ten languages, and they ALL use ^ to mean "bitwise XOR".

ALL OF THEM.

But is it fair to include languages that don't even provide an exponentiation operator?

keldor
Posts: 81
Joined: Thu Jan 26, 2012 9:18 am UTC

Re: 1275: "int(pi)"

Postby keldor » Thu Oct 10, 2013 2:58 am UTC

Well, in PASCAL, exponentiation is **, i.e. x**c.

User avatar
gmalivuk
GNU Terry Pratchett
Posts: 26818
Joined: Wed Feb 28, 2007 6:02 pm UTC
Location: Here and There
Contact:

Re: 1275: "int(pi)"

Postby gmalivuk » Thu Oct 10, 2013 2:59 am UTC

PM 2Ring wrote:
gmalivuk wrote:
cheeseheadtotherescue wrote:Astronomers often take pi to be 3 (or even sometimes 1) and pi^2 to be 10 when dealing with large numbers
Using pi^2=10 actually gets you extraordinarily close, order-of-magnitude wise. Since log10pi = 0.497, treating pi as half an order of magnitude introduces less error than, say, rounding 10.3 down to 10 in a calculation.

(4/3 is very close to 10^(1/8), as well, so can be treated as approximately the 4th root of pi or 1/8 of an order of magnitude, as needed, if you're working with spheres and want to get a bit closer than basic orders of magnitude.)


pi ~= sqrt(10) has a venerable tradition, and some ancient Indian mathematicians believed that pi was exactly equal to sqrt(10).
Another convenient fact is that there are very nearly 10^7.5 seconds in a year (or 10 million pi, depending on whichever is more convenient).
Unless stated otherwise, I do not care whether a statement, by itself, constitutes a persuasive political argument. I care whether it's true.
---
If this post has math that doesn't work for you, use TeX the World for Firefox or Chrome

(he/him/his)

User avatar
Eternal Density
Posts: 5588
Joined: Thu Oct 02, 2008 12:37 am UTC
Contact:

Re: 1275: "int(pi)"

Postby Eternal Density » Thu Oct 10, 2013 3:23 am UTC

gmalivuk wrote:
PM 2Ring wrote:
gmalivuk wrote:
cheeseheadtotherescue wrote:Astronomers often take pi to be 3 (or even sometimes 1) and pi^2 to be 10 when dealing with large numbers
Using pi^2=10 actually gets you extraordinarily close, order-of-magnitude wise. Since log10pi = 0.497, treating pi as half an order of magnitude introduces less error than, say, rounding 10.3 down to 10 in a calculation.

(4/3 is very close to 10^(1/8), as well, so can be treated as approximately the 4th root of pi or 1/8 of an order of magnitude, as needed, if you're working with spheres and want to get a bit closer than basic orders of magnitude.)


pi ~= sqrt(10) has a venerable tradition, and some ancient Indian mathematicians believed that pi was exactly equal to sqrt(10).
Another convenient fact is that there are very nearly 10^7.5 seconds in a year (or 10 million pi, depending on whichever is more convenient).
But 10^7.5 is 13.5.
Play the game of Time! castle.chirpingmustard.com Hotdog Vending Supplier But what is this?
In the Marvel vs. DC film-making war, we're all winners.

User avatar
gmalivuk
GNU Terry Pratchett
Posts: 26818
Joined: Wed Feb 28, 2007 6:02 pm UTC
Location: Here and There
Contact:

Re: 1275: "int(pi)"

Postby gmalivuk » Thu Oct 10, 2013 3:30 am UTC

See? It's a pretty damn good approximation, isn't it?
Unless stated otherwise, I do not care whether a statement, by itself, constitutes a persuasive political argument. I care whether it's true.
---
If this post has math that doesn't work for you, use TeX the World for Firefox or Chrome

(he/him/his)

chapel
Posts: 121
Joined: Mon Dec 01, 2008 8:52 am UTC

Re: 1275: "int(pi)"

Postby chapel » Thu Oct 10, 2013 4:01 am UTC

ceiling(pi*pi) = 10
floor(pi*pi) = 9
ceiling(e*e) = 8
floor(e*e) = 7
floor(pi^phi) = 6
ceiling(phi^pi) = 5
ceiling(pi) = 4
floor(pi) = 3
ceiling(phi) = 2
floor(phi) = 1

Hopefully this will help people debug their programs.

User avatar
Platypodes
Posts: 218
Joined: Mon Nov 19, 2007 8:42 am UTC
Location: On a knot on a log in a hole in the bottom of the sea

Re: 1275: "int(pi)"

Postby Platypodes » Thu Oct 10, 2013 5:52 am UTC

Gargravarr wrote:The PI rule of project estimation: Analyze the project and calculate a reasonable estimate of how much time it should take. Then multiply with PI.

I used the pi rule of commute-time driving in the SF bay area before Google introduced "in current traffic" estimates. Taking the Google Maps estimated driving times and multiplying by pi was often pretty close.
videogamesizzle wrote:so, uh, seen any good arbitrary, high numbers lately?

User avatar
PM 2Ring
Posts: 3715
Joined: Mon Jan 26, 2009 3:19 pm UTC
Location: Sydney, Australia

Re: 1275: "int(pi)"

Postby PM 2Ring » Thu Oct 10, 2013 5:54 am UTC

gmalivuk wrote:Another convenient fact is that there are very nearly 10^7.5 seconds in a year (or 10 million pi, depending on whichever is more convenient).

And if we combine that with the fact that the mean diameter of Earth's orbit is roughly 1000 light-seconds, we can see that Earth's orbital speed is roughly 10-4 c.

User avatar
da Doctah
Posts: 995
Joined: Fri Feb 03, 2012 6:27 am UTC

Re: 1275: "int(pi)"

Postby da Doctah » Thu Oct 10, 2013 6:00 am UTC

modus_ponens wrote:Whenever bare numbers appear in code, a code reviewer immediately gets suspicious and is likely to reject the code. By using these obfuscation techniques, the careless code reviewer might be fooled and let it pass, and the clever programmer gets the code approved.
They tend to let a few constants go: zero, 1, -1, usually 2, but get beyond single-digit integers and they start getting hinky.

Which is why, back in the days of Applesoft BASIC, we used to define PI = 4*ATN(1).

User avatar
PM 2Ring
Posts: 3715
Joined: Mon Jan 26, 2009 3:19 pm UTC
Location: Sydney, Australia

Re: 1275: "int(pi)"

Postby PM 2Ring » Thu Oct 10, 2013 6:34 am UTC

da Doctah wrote:
modus_ponens wrote:Whenever bare numbers appear in code, a code reviewer immediately gets suspicious and is likely to reject the code. By using these obfuscation techniques, the careless code reviewer might be fooled and let it pass, and the clever programmer gets the code approved.
They tend to let a few constants go: zero, 1, -1, usually 2, but get beyond single-digit integers and they start getting hinky.

Understandably. I have to admit that I do sometimes put magic numbers into my own code, but I generally clean them up if I intend to make the code available to others. OTOH, which of these do you think is easier to read:
hours = 24 * days
or
hours = HOURS_PER_DAY * days
?

da Doctah wrote:Which is why, back in the days of Applesoft BASIC, we used to define PI = 4*ATN(1).

I still do that in bc since it doesn't provide pi as a built-in constant. (Annoyingly, the bc library uses very terse names for most functions, so arctan is a(), but I guess that's tolerable, since bc is a calculator, not a full programming language).

I thought it would be more efficient to use
pi = 4 * (a(1/2) + a(1/3)), since the Taylor series for a(1) converges so slowly, but it turns out that a(1/2) + a(1/3) actually takes longer to calculate; I guess a(1) must be optimised (or bc uses a better algorithm than Taylor series summation).

User avatar
orthogon
Posts: 3099
Joined: Thu May 17, 2012 7:52 am UTC
Location: The Airy 1830 ellipsoid

Re: 1275: "int(pi)"

Postby orthogon » Thu Oct 10, 2013 8:02 am UTC

Kinitawowi wrote:
orthogon wrote:I bow down before a true master. SGN PI for the win! I do believe you're right on all counts. I was trying to figure out how it dealt with the 0.1 issue.
RANDOMIZE USR was another of those idioms, if that isn't too kind a word. USR x was a function that called the machine code at address x and returned the contents of the accumulator. You couldn't just ignore the return value; you had to do something with it. Setting the random number seed was the cheapest thing you could do, costing just one byte. Ok, there might have been bizarre side effects if you used random numbers as well, but hey.

It was also NOT PI for 0, rather than PI-PI. And it wasn't the accumulator's value that USR returned, it was the BC register.

But hey, what are we to expect from someone who read Sinclair User? Your Sinclair FTW. (Yeah, I'm going there. :p)

Using RANDOMIZE was the most common approach. Lots of games with BASIC loaders did it as PRINT USR (to get around the random number seed issue; it didn't matter that it would eventually affect the screen because you'd never leave the machine code routine), and I definitely remember seeing "LET E=USR x" in a couple of type-ins back in the day.

I'm glad you spotted my deliberate mistake. Returning the accumulator would have only allowed an 8-bit result, so returning BC was more useful. Perhaps you or time burglar can explain RANDOMIZE USR 1331? It would flash the border making it go stripy, a bit like when a file was loading (but in different colours). Was that an early Easter egg? Or was it a freak behaviour caused by jumping into the middle of some code? That it would return cleanly when you pressed space suggests the former.
xtifr wrote:... and orthogon merely sounds undecided.

User avatar
Xenomortis
Not actually a special flower.
Posts: 1455
Joined: Thu Oct 11, 2012 8:47 am UTC

Re: 1275: "int(pi)"

Postby Xenomortis » Thu Oct 10, 2013 8:39 am UTC

cellocgw wrote:Seriously? All {for sufficiently architected values of "all"} higher-level languages use '^' for exponentiation. R, MatLab, and IDL are floor(pi) such examples.

BASIC, J, Lua, Haskell (sometimes), IDL, TeX, and some calculator languages?

PM 2Ring wrote:But is it fair to include languages that don't even provide an exponentiation operator?

Ruby, Perl, Python, Fortran, COBOL, OCaml, F#, and Ada, according to Wikipedia, use ** for exponentiation.
Which is still a lot that don't use ^ and three of them are in the previous list.
So basically, it's languages that are derived from C and PHP, which is regarded as batshit crazy anyway, that you would exclude. Oh, and Perl.

Probably not all of those have ^ as XOR though.
Image

jcdubacq
Posts: 1
Joined: Thu Oct 10, 2013 8:19 am UTC

Re: 1275: "int(pi)"

Postby jcdubacq » Thu Oct 10, 2013 8:42 am UTC

orthogon wrote:This reminded me of the techniques used on the ZX81 and ZX Spectrum, where "1" was often replaced by PI/PI and "0" by PI-PI.

The BASIC programs were tokenized on entry and stored in tokenized form. Numeric literals were stored as an escape byte followed by four bytes of floating point. Keywords were a single byte in the high ASCII range; these didn't even need to be scanned since they were entered with a single keypress. It was beautiful. The upshot was that a "1" took up 5 bytes, whereas PI/PI only took three. Bear in mind that you only had 1KB of memory to fit your program and data.


You did beat me to the post, but I was also going to say: INT(PI) was indeed shorter than 3 (in memory), SGN(PI) shorter than 1, NOT(PI) shorter than 0. GOD (good old days)!

time burglar
Posts: 20
Joined: Thu Apr 25, 2013 9:26 pm UTC

Re: 1275: "int(pi)"

Postby time burglar » Thu Oct 10, 2013 8:48 am UTC

orthogon wrote:I'm glad you spotted my deliberate mistake. Returning the accumulator would have only allowed an 8-bit result, so returning BC was more useful. Perhaps you or time burglar can explain RANDOMIZE USR 1331? It would flash the border making it go stripy, a bit like when a file was loading (but in different colours). Was that an early Easter egg? Or was it a freak behaviour caused by jumping into the middle of some code? That it would return cleanly when you pressed space suggests the former.


Ahh yes, I recall entering that command for amusement when I couldn't think of anything else to do. That is the cassette-handling part of the ROM you're jumping into there. In fact the ROM disassembly is available online - ftp://ftp.worldofspectrum.org/pub/sincl ... blyThe.txt

The address 1331 is 0533 in hex - you can see that's in the middle of the main SAVE routine, skipping the distinctive-sounding header part. My guess would be that the state of the registers when you jump in there is telling it to "SAVE" starting from address 0 (i.e. the ROM itself)?

User avatar
orthogon
Posts: 3099
Joined: Thu May 17, 2012 7:52 am UTC
Location: The Airy 1830 ellipsoid

Re: 1275: "int(pi)"

Postby orthogon » Thu Oct 10, 2013 9:11 am UTC

time burglar wrote:
orthogon wrote:I'm glad you spotted my deliberate mistake. Returning the accumulator would have only allowed an 8-bit result, so returning BC was more useful. Perhaps you or time burglar can explain RANDOMIZE USR 1331? It would flash the border making it go stripy, a bit like when a file was loading (but in different colours). Was that an early Easter egg? Or was it a freak behaviour caused by jumping into the middle of some code? That it would return cleanly when you pressed space suggests the former.


Ahh yes, I recall entering that command for amusement when I couldn't think of anything else to do. That is the cassette-handling part of the ROM you're jumping into there. In fact the ROM disassembly is available online - ftp://ftp.worldofspectrum.org/pub/sincl ... blyThe.txt

The address 1331 is 0533 in hex - you can see that's in the middle of the main SAVE routine, skipping the distinctive-sounding header part. My guess would be that the state of the registers when you jump in there is telling it to "SAVE" starting from address 0 (i.e. the ROM itself)?

Thanks - it would take some patience to work out why the colours were different to a normal SAVE, and also why it doesn't seem to return of its own accord (even if it were saving all of ROM and RAM, you'd think it would finish eventually). I can't remember whether USR made any commitment about the contents of the registers on entry, but perhaps they were always the same.

I still love the idea that you could hope to understand every single thing about the ZX Spectrum right down to the hardware. Maybe Pfhorrest should get hold of one - it might help with the discussion over at "Functional".
xtifr wrote:... and orthogon merely sounds undecided.


Return to “Individual XKCD Comic Threads”

Who is online

Users browsing this forum: Google Feedfetcher, ijuin and 43 guests