### 1275: "int(pi)"

Posted:

**Wed Oct 09, 2013 4:03 am UTC**Page **1** of **3**

Posted: **Wed Oct 09, 2013 4:03 am UTC**

Posted: **Wed Oct 09, 2013 4:14 am UTC**

Why do I get the feeling that this whole thing was to set up the alt-text pie joke?

Lordy! Am I becoming cynical?

Lordy! Am I becoming cynical?

Posted: **Wed Oct 09, 2013 4:17 am UTC**

Pi is exactly 3?

Posted: **Wed Oct 09, 2013 4:18 am UTC**

I knew "3" was the bad apple in "13". "1" is infallible and infinitely perfectly self-divisible.

Posted: **Wed Oct 09, 2013 4:24 am UTC**

Quicksilver wrote:Pi is exactly 3?

No, int(Pi) is exactly 3. Same for floor(Pi).

This comic gave me a "meh" moment.

Posted: **Wed Oct 09, 2013 4:30 am UTC**

The fraction looks relatively clean, in mathematical notation:

Posted: **Wed Oct 09, 2013 4:33 am UTC**

Quicksilver wrote:Pi is exactly 3?

While there are some variations between programming languages, what Randall's doing here is referring to three specific functions:

floor(x) gives the highest integer that is less than or equal to x. So floor(3) = floor(3.01) = floor(3.8) = floor(pi) = 3.

ceiling(x) gives the lowest integer that is greater than or equal to x. So ceiling(4) = ceiling(3.01) = ceiling(3.8) = ceiling(pi) = 4.

int(x) gives the nearest integer to x, i.e. it gives x rounded to the nearest 1. So int(3) = int(3.01) = int(2.8) = int(pi) = 3.

In other words, he's providing a way to calculate the volume of a sphere ostensibly in terms of pi and the radius of the sphere.

Posted: **Wed Oct 09, 2013 4:43 am UTC**

ConMan wrote:int(x) gives the nearest integer to x, i.e. it gives x rounded to the nearest 1. So int(3) = int(3.01) = int(2.8) = int(pi) = 3.

In most programming languages I've come across, int() rounds towards zero (ie the same as floor() for positive numbers and ceil() for negative numbers... simply truncating the number at the decimal point). Sometimes it rounds down (ie same as floor()) but that's less common. There aren't that many that it's a round-to-nearest operation... usually there's a separate round() function for that, or failing that, floor(x + 0.5).

Posted: **Wed Oct 09, 2013 4:52 am UTC**

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.

Posted: **Wed Oct 09, 2013 4:59 am UTC**

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.

Posted: **Wed Oct 09, 2013 5:22 am UTC**

Floor pi is best pi. (now I'm hungry)

Half-Life 2: Episode Pi

Half-Life 2: Episode Pi

Posted: **Wed Oct 09, 2013 5:23 am UTC**

Just don't use PHP: 4 ^ 3 returns 7 (it's bitwise XOR).

I think int(x) is usually truncate or round toward zero; that is 3.14 => 3, 5.6 => 5, -2.2 => -2. You get the picture.

Round is usually separate.

(edited to be clearer)

I think int(x) is usually truncate or round toward zero; that is 3.14 => 3, 5.6 => 5, -2.2 => -2. You get the picture.

Round is usually separate.

(edited to be clearer)

Posted: **Wed Oct 09, 2013 5:38 am UTC**

eh, having a 3 in your code is fine. a 4 though, you know what they say : four means death.

Posted: **Wed Oct 09, 2013 5:49 am UTC**

Hah, I love it. I actually got one of these programming jokes on my own. Ok, it's actually math, right? But close enough.

Posted: **Wed Oct 09, 2013 5:54 am UTC**

Stop the irrationality! Vote no on ballot measure π!

Posted: **Wed Oct 09, 2013 6:22 am UTC**

I rewrote all of Sandcastle Builder's code to conform to this.

So now I have lines like

or

and I'm not risking it with numbers close to int(pi):

or fractions of it:

or strings:

or combinations of the above:

or numbers users might see:

This delayed the release of version 1.81. I hope GLR is happy.

So now I have lines like

Code: Select all

`Molpy.Boosts['Double or Nothing'].department=1*(Math.floor(Math.random()*Math.floor(Math.PI))==0);`

or

Code: Select all

`var nBuy = Math.pow(Math.ceil(Math.PI),Molpy.options.castlemultibuy);`

and I'm not risking it with numbers close to int(pi):

Code: Select all

`bonus+=Molpy.SandToolsById[i].amount*Math.pow(Math.floor(Math.PI)+.5,i+1);`

or fractions of it:

Code: Select all

`var amount = Math.pow(1+Math.ceil(Math.PI)/10,Math.max(0,(Molpy.SandTools['Bag'].amount-Molpy.npbDoubleThreshhold)/2))-1;`

or strings:

Code: Select all

`,"The Lord of the Bags "+Math.floor(Math.PI)+": The Return of La Petite"`

,"Half-Bag "+Math.floor(Math.PI)+" Confirmed!"

or combinations of the above:

Code: Select all

` new Molpy.Boost({name:'Embaggening',desc:'Each Cuegan after the 14th gives a 2% boost to the sand dig rate of Bags',`

sand:Math.floor(Math.PI)+'.5M',castles:'23K',icon:'embaggening'});

or numbers users might see:

Code: Select all

` if(number==Math.floor(Math.PI))return 'Math.floor(Math.PI)';`

if(number==Math.ceil(Math.PI))return 'Math.ceil(Math.PI)';

This delayed the release of version 1.81. I hope GLR is happy.

Posted: **Wed Oct 09, 2013 7:32 am UTC**

Well, I wish I had known of this curse BEFORE I chose my forum username...

Posted: **Wed Oct 09, 2013 8:13 am UTC**

Remember, you can generate EVERY natural number by a finite expression of the form ceil(pi)+...+ceil(pi)-floor(pi)-...-floor(pi)!

Posted: **Wed Oct 09, 2013 8:25 am UTC**

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.

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.

Posted: **Wed Oct 09, 2013 9:44 am UTC**

SomeoneSomewhere wrote:Just don't use PHP: 4 ^ 3 returns 7 (it's bitwise XOR).

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

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.

Wow

Posted: **Wed Oct 09, 2013 10:05 am UTC**

Monika wrote:SomeoneSomewhere wrote:Just don't use PHP: 4 ^ 3 returns 7 (it's bitwise XOR).

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

Lua [lua.org] (and MoonScript [moonscript.org], which compiles into Lua) uses ^ for exponent instead of XOR.

Posted: **Wed Oct 09, 2013 10:29 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.

Thanks for the memory, I'd completely forgotten that. Ahh, the good old days, if you tell that to kids today they won't believe you. (And in defence of the Speccie, it did come with 16k of RAM as basic, it was the ZX81 and the earlier ZX80 which had the 1k RAM.)

Posted: **Wed Oct 09, 2013 10:41 am UTC**

Choonster 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"

Lua [lua.org] (and MoonScript [moonscript.org], which compiles into Lua) uses ^ for exponent instead of XOR.

Haskell and VB both also have ^ as a power-of function as well. Haskell also has ** as a different power-of function... because Haskell is like that. (x^n is integer exponentiation, defined for anything that supports multiplication, to the power of a natural number... x**y is floating point exponentiation, only defined for floats.)

Posted: **Wed Oct 09, 2013 10:52 am UTC**

Ok, I could kinda see if this was a "the program isn't working and for some dumb reason doing X seemingly irrelevant thing fixes it" joke, but... I'm just not seeing what the "the number 3 is cursed" thing is in reference to. Things like pi/pi instead of 1 being used to save memory, I guess it could be related to this, but still that doesn't quite seem to fit.

Anyone have any idea what that's about?

Anyone have any idea what that's about?

Posted: **Wed Oct 09, 2013 10:57 am UTC**

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

Also bc, a programmable arbitrary-precision calculator found in many Linux distros. However, the power must be an integer; if you want arbitrary powers, you need to use logs.

Posted: **Wed Oct 09, 2013 11:13 am UTC**

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.

Posted: **Wed Oct 09, 2013 12:38 pm UTC**

MATLAB and Octave use ^ for exponentiation.

I was also reminded of a problem set by Chris Maslanka in The Guardian. You have to make all the integers from 1 to 100 using five 5's, e.g.:

1=(5+5)/5-5/5

2=(5+5)/5+5-5

...

100=5x5x5-5x5

I worked on it for a while with a colleague on a business trip. We did pretty well but ran out of time/ideas before we got them all. Some thoughts spoilered in case you want to have a go:**Spoiler:**

ETA:

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.

Arancaytar wrote:Remember, you can generate EVERY natural number by a finite expression of the form ceil(pi)+...+ceil(pi)-floor(pi)-...-floor(pi)!

I was also reminded of a problem set by Chris Maslanka in The Guardian. You have to make all the integers from 1 to 100 using five 5's, e.g.:

1=(5+5)/5-5/5

2=(5+5)/5+5-5

...

100=5x5x5-5x5

I worked on it for a while with a colleague on a business trip. We did pretty well but ran out of time/ideas before we got them all. Some thoughts spoilered in case you want to have a go:

ETA:

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.

Posted: **Wed Oct 09, 2013 12: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.

1 year -> 2 centuries? Or two decades?

Posted: **Wed Oct 09, 2013 1:01 pm UTC**

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.

That was my thought. While there are programs that will do (4/3) "correctly", as in the floating point math, I thought that integer division was fairly common. I would think that most real languages would return 1, as you say. If you're using something that's mathematics-oriented, probably not, but the use of ceiling() and floor() makes me think that this is intended to be relatively low-level, and thus not work right.

Posted: **Wed Oct 09, 2013 1:01 pm UTC**

brenok wrote: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.

1 year -> 2 centuries? Or two decades?

I guess 1 year -> The project will be cancelled next month.

Posted: **Wed Oct 09, 2013 1:14 pm UTC**

orthogon wrote:This reminded me of the techniques used on the ZX81 and ZX Spectrum...

I can't quite decide whether that makes this better or worse.

Posted: **Wed Oct 09, 2013 1:33 pm UTC**

The number three is lava!

Posted: **Wed Oct 09, 2013 1:39 pm UTC**

orthogon wrote:I was also reminded of a problem set by Chris Maslanka in The Guardian. You have to make all the integers from 1 to 100 using five 5's

The earlier form of this problem (going back to the late 19th century) uses four fours.

The first printed occurrence of this activity is in "Mathematical Recreations and Essays" by W. W. Rouse Ball published in 1892. In this book it is described as a "traditional recreation".

In his discussion of the problem Ball calls it "An arithmetical amusement, said to have been first propounded in 1881, ...." . This date aligns with the appearance of the problem in Knowledge, An Illustrated Magazine of Science, (Dec 30, 1881) edited by Richard A. Proctor, the English astronomer who is remembered for one of the earliest maps of Mars.

I'm pretty sure that I first heard of the four fours puzzle in a Martin Gardner column.

Posted: **Wed Oct 09, 2013 1:42 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.

Oh, NOW someone tells me, some 30 years after I needed it.

Trying to cram programs AND operating system AND video memory into 1KB meant I NEEDED those extra bytes.

I might just have to dig that l'il beast out and try. Plugging a ZX-80 into an HDTV alone might be quite satisfying...

Posted: **Wed Oct 09, 2013 2:11 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.

I thought of that when I saw this comic, too.

On the Spectrum with 16k or more typically 48k of RAM it wasn't necessary most of the time (I certainly never came close to filling 48k), but it cropped up in the loaders of very large programs like games where the machine code was crammed into RAM so tightly that it left very little space in the BASIC area for the loader to fit.

I could see it on the unexpanded ZX81 though, where every single byte could make a difference. Now there was a system that knew how to use minimal memory. The way the screen contents were stored was particularly ingenious.

Posted: **Wed Oct 09, 2013 2:19 pm UTC**

PM 2Ring wrote:orthogon wrote:I was also reminded of a problem set by Chris Maslanka in The Guardian. You have to make all the integers from 1 to 100 using five 5's

The earlier form of this problem (going back to the late 19th century) uses four fours.

Ah, so it was a twist on an old puzzle.

Glad to see there are a few Sinclair BASIC old-timers on here. I had a 48K Spectrum and I agree that the PI-baking wasn't really necessary for that - I think I saw it in printed listings in old Sinclair User mags and it made a lasting impression on me (and, I fear, my coding style) ever after.

Posted: **Wed Oct 09, 2013 2:25 pm UTC**

!+[]+!+[]+!+[] is 3 in javascript. Also, Astronomers often take pi to be 3 (or even sometimes 1) and pi^2 to be 10 when dealing with large numbers (the kind where you want the order of magnitude to be within an order of magnitude (wrap you're head around that!))

Posted: **Wed Oct 09, 2013 2:31 pm UTC**

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.

There's this thing called "DWIM" (do what I mean) which, when well implemented, makes programming far faster and easier. The more you have to use non-intuitive methods and characters, the slower your work will be. And further, the harder it'll be for anyone not well-versed in your choice of programming language to understand your code.

Posted: **Wed Oct 09, 2013 3:21 pm UTC**

Would any of those pi be of the pumpkin variety?

Posted: **Wed Oct 09, 2013 3:22 pm UTC**

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?