1275: "int(pi)"

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

Moderators: Moderators General, Prelates, Magistrates

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

Re: 1275: "int(pi)"

Postby time burglar » Thu Oct 10, 2013 12:04 pm UTC

orthogon wrote: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".


OK...I am intrigued too :) Just found a javascript speccy emulator, so I can see if the call does ever finish (maybe "saves" all 64k, taking >5 minutes?) Yes, the border colour is unusual - blue and magenta, rather than the usual blue and yellow, I think?

From the ROM code, I think the blue is a fixed value, and the other colour is from whatever's in register C. Must normally be initialised to 6 (yellow) somewhere I guess?

To find out what C is when we call USR 1331, I suppose we can look at the USR function in that ROM disassembly; it's at 34B3. So, for it to call the address you've supplied, it puts it into BC, pushes that onto the stack and does a RET. That means that the value of C will be the lower byte of 1331, which is 33 hex - so the relevant lowest 3 colour bits will indeed be magenta!

[after lunch edit...]

Yes it does terminate after several minutes. And I think I'm right about the second colour effectively coming from the address you call. You can replace 1331 with 1333 or 1334 and get cyan or yellow. Marvellous :D

Emulator at http://jsspeccy.zxdemo.org/
Can you remember the keyboard layout?
Spoiler:
T: "RANDOMIZE"
ctrl+shift for extended mode, then L: "USR"

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 1:44 pm UTC

Excellent work. It's brilliant that you can change the colour.

Strangely, I also started this emulator doing it, before you did, and it hasn't finished yet (i.e. hours later).

The keyboard layout was an issue. I had to refer to this:
Spoiler:
Image

Edit: also to get a bit back on topic, or at least off-topic in the same direction as everyone else, the "raise to a power" operator was a proper upward arrow, but its code was 0x5E like the caret. So should we count Sinclair BASIC in the "use ^ for exponentiation" or not? Adjudication needed.
xtifr wrote:... and orthogon merely sounds undecided.

rmsgrey
Posts: 3653
Joined: Wed Nov 16, 2011 6:35 pm UTC

Re: 1275: "int(pi)"

Postby rmsgrey » Thu Oct 10, 2013 4:03 pm UTC

Something Physicists and Engineers will be familiar with is that Pi^2 (9.86...) is also very close to the acceleration due to gravity at or sufficiently close to the Earth's surface in SI units (9.81...)

User avatar
Monika
Welcoming Aarvark
Posts: 3673
Joined: Mon Aug 18, 2008 8:03 am UTC
Location: Germany, near Heidelberg
Contact:

Re: 1275: "int(pi)"

Postby Monika » Thu Oct 10, 2013 7:57 pm UTC

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

Maybe I should look at Lua after all.

phlip wrote:
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.)

One more reason to hate Haskell.

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


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.

Most real languages would return 1? I don't think so. C-like languages (C++, Java, C#) do it. I think Python used to do it and had // for real division and then they switched it around or something like that? PHP does it, PHP does a lot of strange things. Is this evil behavior so widespread?

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.

There was never such a time. The tagline of xkcd has been "Warning: this comic occasionally contains strong language (which may be unsuitable for children), unusual humor (which may be unsuitable for adults), and advanced mathematics (which may be unsuitable for liberal-arts majors)." since almost forever.

chapel wrote: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

This is neat :D
#xkcd-q on irc.foonetic.net - the LGBTIQQA support channel
Please donate to help these people

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

Re: 1275: "int(pi)"

Postby Eternal Density » Fri Oct 11, 2013 12:57 am UTC

chapel wrote: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.
I'm tempted to add all these to Sandcastle Builder but I think my users (including me) will hate me for it.
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
phlip
Restorer of Worlds
Posts: 7573
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia
Contact:

Re: 1275: "int(pi)"

Postby phlip » Fri Oct 11, 2013 1:09 am UTC

Monika wrote:
phlip wrote: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.)

One more reason to hate Haskell.

Why is that a reason to hate Haskell? (^) is exact (as long as the underlying multiplication is exact), and lets you do large exponentiation of integers (eg for number theory work). (**) on the other hand works with floats, which can have rounding errors, but this lets you use fractional powers. They're different operations with different use cases... eg you could reasonably write a program to tell you that (2^127 - 1) is prime... on the other hand, (2**127 - 1) is a rounding error, and indeed "(2**127 - 1) == 2**127" evaluates to True.

Haskell tends to give you a lot of options in this regard... if there's more than one way to do a thing, it'll often do both and let you choose. This is why it has (/) for floating point division, and both `quot` and `div` for integer division (quot rounds towards zero, div rounds towards -inf).

Code: Select all

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

Arcorann
Posts: 16
Joined: Sun Mar 31, 2013 12:06 pm UTC

Re: 1275: "int(pi)"

Postby Arcorann » Fri Oct 11, 2013 2:14 am 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.
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.


I once read a puzzle in a book somewhere where you had to make the integers 1 to 20 using only pi and a few operations (don't remember exactly which ones apart from +-*/, but floor was included while ceiling wasn't). I remember that there were a few solutions posted in the book, the second of which used -floor(-pi) to replicate ceiling(pi) which the first one hadn't done. Sadly I can't remember anything else about it.

Also, does the spam filter really have to block links in posts I'm quoting?

ps.02
Posts: 378
Joined: Fri Apr 05, 2013 8:02 pm UTC

Re: 1275: "int(pi)"

Postby ps.02 » Fri Oct 11, 2013 3:44 am UTC

chapel wrote:ceiling(pi*pi) = 10
floor(pi*pi) = 9
...

Yes but what is mol(pi)?

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

Re: 1275: "int(pi)"

Postby Pfhorrest » Fri Oct 11, 2013 4:02 am UTC

Arcorann wrote:Also, does the spam filter really have to block links in posts I'm quoting?

Yes, because quoted material is just ordinary text wrapped in quote tags, so if it didn't block that you could just write any fake quote and put a link in it
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
eran_rathan
Mostly Wrong
Posts: 1846
Joined: Fri Apr 09, 2010 2:36 pm UTC
Location: in your ceiling, judging you

Re: 1275: "int(pi)"

Postby eran_rathan » Fri Oct 11, 2013 11:45 am UTC

ps.02 wrote:
chapel wrote:ceiling(pi*pi) = 10
floor(pi*pi) = 9
...

Yes but what is mol(pi)?


(6.02214129(27)×10^23)*(3.1428...) = 1.891909*10^24 (approx).

that's big. "You just won't believe how vastly, hugely, mind- bogglingly big it is. I mean, you may think it's a long way down the road to the chemist's, but that's just peanuts to space." -Douglas Adams
"Does this smell like chloroform to you?"
"Google tells me you are not unique. You are, however, wrong."
nɒʜƚɒɿ_nɒɿɘ

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 » Fri Oct 11, 2013 1:17 pm UTC

time burglar wrote:OK...I am intrigued too :) Just found a javascript speccy emulator, so I can see if the call does ever finish (maybe "saves" all 64k, taking >5 minutes?) Yes, the border colour is unusual - blue and magenta, rather than the usual blue and yellow, I think?

From the ROM code, I think the blue is a fixed value, and the other colour is from whatever's in register C. Must normally be initialised to 6 (yellow) somewhere I guess?

To find out what C is when we call USR 1331, I suppose we can look at the USR function in that ROM disassembly; it's at 34B3. So, for it to call the address you've supplied, it puts it into BC, pushes that onto the stack and does a RET. That means that the value of C will be the lower byte of 1331, which is 33 hex - so the relevant lowest 3 colour bits will indeed be magenta!

[after lunch edit...]

Yes it does terminate after several minutes. And I think I'm right about the second colour effectively coming from the address you call. You can replace 1331 with 1333 or 1334 and get cyan or yellow. Marvellous :D

Strangely, mine is still going, but I can't be sure whether it carries on running when I'm looking at another tab (it's a bit like trying to find out if the fridge light really goes off when you close the door). Looking at the code, the data to be saved is pointed to by IX, and the number of bytes is in DE; so it should "save" at most 64KBytes. I can't see any way for it not to exit. Maybe I just need to wait for it.
[Edit]: I wrote a program to count up from 0 in a loop: it stops when the tab is not selected in the browser and carries on when you switch back to it. So my 1331 probably only had a few tens of seconds of runtime.

Also, it's a shame that 1337 doesn't do anything interesting. That would have proved that the speccy was really ahead of its time.
xtifr wrote:... and orthogon merely sounds undecided.

User avatar
sam_i_am
Posts: 624
Joined: Mon Jun 18, 2012 3:38 pm UTC
Location: Urbana, Illinois, USA

Re: 1275: "int(pi)"

Postby sam_i_am » Fri Oct 11, 2013 8:46 pm UTC

doesn't 4/3 get truncated to 1?

User avatar
Monika
Welcoming Aarvark
Posts: 3673
Joined: Mon Aug 18, 2008 8:03 am UTC
Location: Germany, near Heidelberg
Contact:

Re: 1275: "int(pi)"

Postby Monika » Fri Oct 11, 2013 10:00 pm UTC

phlip wrote:
Monika wrote:
phlip wrote: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.)

One more reason to hate Haskell.

Why is that a reason to hate Haskell? (^) is exact (as long as the underlying multiplication is exact), and lets you do large exponentiation of integers (eg for number theory work). (**) on the other hand works with floats, which can have rounding errors, but this lets you use fractional powers. They're different operations with different use cases... eg you could reasonably write a program to tell you that (2^127 - 1) is prime... on the other hand, (2**127 - 1) is a rounding error, and indeed "(2**127 - 1) == 2**127" evaluates to True.

Haskell tends to give you a lot of options in this regard... if there's more than one way to do a thing, it'll often do both and let you choose. This is why it has (/) for floating point division, and both `quot` and `div` for integer division (quot rounds towards zero, div rounds towards -inf).

Okay, I guess that sounds pretty neat after all.
#xkcd-q on irc.foonetic.net - the LGBTIQQA support channel
Please donate to help these people

DR6
Posts: 171
Joined: Thu Nov 01, 2012 1:44 pm UTC

Re: 1275: "int(pi)"

Postby DR6 » Sun Oct 13, 2013 12:03 pm UTC

Monika wrote:Okay, I guess that sounds pretty neat after all.


Actually, haskell has three exponential operators.
"There are three two-argument exponentiation operations: (^) raises any number to a nonnegative integer power, (^^) raises a fractional number to any integer power, and (**) takes two floating-point arguments. The value of x^0 or x^^0 is 1 for any x, including zero; 0**y is undefined. "

The only problem is that it can't actually enforce that the power of ^ is nonnegative, so it just raises an exception, and exceptions in Haskell are tricky because you don't use them for actual error handling.

User avatar
Monika
Welcoming Aarvark
Posts: 3673
Joined: Mon Aug 18, 2008 8:03 am UTC
Location: Germany, near Heidelberg
Contact:

Re: 1275: "int(pi)"

Postby Monika » Sun Oct 13, 2013 9:25 pm UTC

What do you use them for?
#xkcd-q on irc.foonetic.net - the LGBTIQQA support channel
Please donate to help these people

User avatar
bmonk
Posts: 662
Joined: Thu Feb 18, 2010 10:14 pm UTC
Location: Schitzoed in the OTT between the 2100s and the late 900s. Hoping for singularity.

Re: 1275: "int(pi)"

Postby bmonk » Thu Oct 24, 2013 10:46 pm UTC

The fact that (pi)^2 is close to 10 was also used on many slide rules--the CF/DF/CIF scales were "folded" at pi because it was a useful factor, and close enough to the center of the scale that the "error" didn't really matter.
Having become a Wizard on n.p. 2183, the Yellow Piggy retroactively appointed his honorable self a Temporal Wizardly Piggy on n.p.1488, not to be effective until n.p. 2183, thereby avoiding a partial temporal paradox. Since he couldn't afford two philosophical PhDs to rule on the title.


Return to “Individual XKCD Comic Threads”

Who is online

Users browsing this forum: No registered users and 120 guests