Page 1 of 3

1275: "int(pi)"

Posted: Wed Oct 09, 2013 4:03 am UTC
by asdfzxc
Image

Alt-text: "If replacing all the '3's doesn't fix your code, remove the 4s, too, with 'ceiling(pi) / floor(pi) * pi * r^floor(pi)'. Mmm, floor pie."

I'll have to remember that one.

Re: 1275: "int(pi)"

Posted: Wed Oct 09, 2013 4:14 am UTC
by n079614
Why do I get the feeling that this whole thing was to set up the alt-text pie joke?

Lordy! Am I becoming cynical?

Re: 1275: "int(pi)"

Posted: Wed Oct 09, 2013 4:17 am UTC
by Quicksilver
Pi is exactly 3?

Re: 1275: "int(pi)"

Posted: Wed Oct 09, 2013 4:18 am UTC
by rhomboidal
I knew "3" was the bad apple in "13". "1" is infallible and infinitely perfectly self-divisible.

Re: 1275: "int(pi)"

Posted: Wed Oct 09, 2013 4:24 am UTC
by Demki
Quicksilver wrote:Pi is exactly 3?

No, int(Pi) is exactly 3. Same for floor(Pi).
This comic gave me a "meh" moment.

Re: 1275: "int(pi)"

Posted: Wed Oct 09, 2013 4:30 am UTC
by phlip
The fraction looks relatively clean, in mathematical notation:

Image

Re: 1275: "int(pi)"

Posted: Wed Oct 09, 2013 4:33 am UTC
by ConMan
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.

Re: 1275: "int(pi)"

Posted: Wed Oct 09, 2013 4:43 am UTC
by phlip
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).

Re: 1275: "int(pi)"

Posted: Wed Oct 09, 2013 4:52 am UTC
by trwrt
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.

Re: 1275: "int(pi)"

Posted: Wed Oct 09, 2013 4:59 am UTC
by phlip
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.

Re: 1275: "int(pi)"

Posted: Wed Oct 09, 2013 5:22 am UTC
by cjmcjmcjmcjm
Floor pi is best pi. (now I'm hungry)

Half-Life 2: Episode Pi

Re: 1275: "int(pi)"

Posted: Wed Oct 09, 2013 5:23 am UTC
by SomeoneSomewhere
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)

Re: 1275: "int(pi)"

Posted: Wed Oct 09, 2013 5:38 am UTC
by zukenft
eh, having a 3 in your code is fine. a 4 though, you know what they say : four means death.

Re: 1275: "int(pi)"

Posted: Wed Oct 09, 2013 5:49 am UTC
by azule
Hah, I love it. I actually got one of these programming jokes on my own. Ok, it's actually math, right? But close enough. :)

Re: 1275: "int(pi)"

Posted: Wed Oct 09, 2013 5:54 am UTC
by keithl
Stop the irrationality! Vote no on ballot measure π!

Re: 1275: "int(pi)"

Posted: Wed Oct 09, 2013 6:22 am UTC
by Eternal Density
I rewrote all of Sandcastle Builder's code to conform to this.
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.

Re: 1275: "int(pi)"

Posted: Wed Oct 09, 2013 7:32 am UTC
by Pi is exactly three
Well, I wish I had known of this curse BEFORE I chose my forum username...

Re: 1275: "int(pi)"

Posted: Wed Oct 09, 2013 8:13 am UTC
by Arancaytar
Remember, you can generate EVERY natural number by a finite expression of the form ceil(pi)+...+ceil(pi)-floor(pi)-...-floor(pi)! :D

Re: 1275: "int(pi)"

Posted: Wed Oct 09, 2013 8:25 am UTC
by orthogon
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.

Re: 1275: "int(pi)"

Posted: Wed Oct 09, 2013 9:44 am UTC
by Monika
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 :D

Re: 1275: "int(pi)"

Posted: Wed Oct 09, 2013 10:05 am UTC
by Choonster
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.

Re: 1275: "int(pi)"

Posted: Wed Oct 09, 2013 10:29 am UTC
by Eoink
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.)

Re: 1275: "int(pi)"

Posted: Wed Oct 09, 2013 10:41 am UTC
by phlip
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.)

Re: 1275: "int(pi)"

Posted: Wed Oct 09, 2013 10:52 am UTC
by Randomizer
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?

Re: 1275: "int(pi)"

Posted: Wed Oct 09, 2013 10:57 am UTC
by PM 2Ring
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.

Re: 1275: "int(pi)"

Posted: Wed Oct 09, 2013 11:13 am UTC
by Gargravarr
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.

Re: 1275: "int(pi)"

Posted: Wed Oct 09, 2013 12:38 pm UTC
by orthogon
MATLAB and Octave use ^ for exponentiation.

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

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:
To start with you are looking for ways to "use up" unneeded 5's (+5-5), but in other regions you have to make each 5 work really hard for you. 5! proved quite useful; I particularly remember using 5!/5 a lot. Only some time later did I get the idea of using the "floor" and "ceiling" operators; applying these to sqrt(5) gives you 2 and 3 for the price of one 5. This seemed too much like cheating and I couldn't decide whether "floor" and "ceiling" are well enough known operators to be allowed.


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.

Re: 1275: "int(pi)"

Posted: Wed Oct 09, 2013 12:54 pm UTC
by brenok
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?

Re: 1275: "int(pi)"

Posted: Wed Oct 09, 2013 1:01 pm UTC
by Zassounotsukushi
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.

Re: 1275: "int(pi)"

Posted: Wed Oct 09, 2013 1:01 pm UTC
by orthogon
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.

Re: 1275: "int(pi)"

Posted: Wed Oct 09, 2013 1:14 pm UTC
by drachefly
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.

Re: 1275: "int(pi)"

Posted: Wed Oct 09, 2013 1:33 pm UTC
by Mike Rore
The number three is lava!

Re: 1275: "int(pi)"

Posted: Wed Oct 09, 2013 1:39 pm UTC
by PM 2Ring
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.

Re: 1275: "int(pi)"

Posted: Wed Oct 09, 2013 1:42 pm UTC
by ctdonath
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. :D

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...

Re: 1275: "int(pi)"

Posted: Wed Oct 09, 2013 2:11 pm UTC
by pdg
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.

Re: 1275: "int(pi)"

Posted: Wed Oct 09, 2013 2:19 pm UTC
by orthogon
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.
Spoiler:
The gamma function would have been useful. We could have had 5!/5 using only one 5.

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.

Re: 1275: "int(pi)"

Posted: Wed Oct 09, 2013 2:25 pm UTC
by cheeseheadtotherescue
!+[]+!+[]+!+[] 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!))

Re: 1275: "int(pi)"

Posted: Wed Oct 09, 2013 2:31 pm UTC
by cellocgw
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.

Re: 1275: "int(pi)"

Posted: Wed Oct 09, 2013 3:21 pm UTC
by San Fran Sam
Would any of those pi be of the pumpkin variety?

Re: 1275: "int(pi)"

Posted: Wed Oct 09, 2013 3:22 pm UTC
by smileyy
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?