Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re: Perl oddities

by Anonymous Monk
on Mar 01, 2005 at 13:23 UTC ( #435404=note: print w/replies, xml ) Need Help??

in reply to Perl oddities

Perl has sin and cos, but no tan. I know you can make your own tan function, but that's not the point. It's not convenient (hence, unPerlish) for the programmer to make his/her own (or load a module to do so). Besides, calling sub in Perl is expensive. Even many calculators have a button for the tan function.

Replies are listed 'Best First'.
Re^2: Perl oddities
by merlyn (Sage) on Mar 01, 2005 at 18:05 UTC
    sin, cos, and atan2 are "primitive" functions. From those, all other forward and reverse functions can be trivially derived.

    The overhead of calling a subroutine is tiny compared to the overhead of computing the sin or cos, I would imagine.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

        sin, cos, and atan2 are "primitive" functions.

      Well, once you have a routine for sin, then cos is the same thing, but pi/2 sooner. And I'm fairly sure that's the way it's implemented in the libraries.

      Using atan2 to do an inverse sin or cos is a bit of a walk in the park, but essentially I guess you have to find the 'Y' value once you have the 'X', then submit both to atan2, and that will give you the angle you're looking for.

      That's a nice brain exercise right after lunch.

      Alex / talexb / Toronto

      "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

        I suspect on most CPUs, it just uses the sin and cos opcodes of the native CPU. Implementing sin in terms of cos (or vice-versa) is just asking for problems, like the well-known results being a little off, since pi/2 cannot be exactly represented as a float (or as anything else in finite RAM).

      I know. But you can do away with either sin or cos as well - they are the same primitive, just shifted a quarter of a phase. That of course doesn't mean tan couldn't be present. After all, Perl gives us push, pop, shift and unshift, when they can all be trivially implemented using splice. Perl is a rich language, and any minimal approach is, to quote brian, what I consider exceptions to normal Perl thinking. Normal Perl thinking is "programmer convenience". A minimalistic approach isn't programmer convenience.

        My sense is that push/pop/shift/unshift are much more frequently used than the trig functions. On that basis, it makes Huffman sense to provide the convenience of the push, etc. functions while declining to provide a fuller set of trig functions.

Re^2: Perl oddities
by fizbin (Chaplain) on Mar 03, 2005 at 23:38 UTC
    Note that I've seen the pattern of providing sin, cos and atan (but not other trig functions) in other libraries and programming languages. And the reason for this (that so far as I can tell no one has yet said) is simple: those functions are defined for all values of their argument. tan isn't. arcsin and arccos aren't.

    Not that it's necessarily a very good reason, mind you.

    -- @/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/; map{y/X_/\n /;print}map{pop@$_}@/for@/
      And the reason for this (that so far as I can tell no one has yet said) is simple: those functions are defined for all values of their argument. tan isn't. arcsin and arccos aren't. Well, division isn't defined for all values of its arguments either, and neither are log and sqrt. So, I agree it's not much of a reason at all.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://435404]
[1nickt]: ... sign I have seen ...
[1nickt]: Now that one made me hopeful! First time not angry in a long time when I saw that kid.
[Discipulus]: i propend for removing: why? because we are so few that we must find i minimal common divisor, this is certainly Perl not our (anyway private) thougths. And i say this still wondering because i love a lot freedom of expression. And i say this not for roho
[1nickt]: Discipulus that was the point of my story of taking the sticker off my truck. I know there are lots of people in the world who if I knw their private beliefs I might want to argue with them. And they with me. But life cannot all be arguments!
[1nickt]: This is less than perfect ... but demanding perfection (from people or from life) is a sure way to unhappiness.
[Discipulus]: and anyway we have CB where every (democratic) opinion can be expressed
erix eat the rich!
[1nickt]: I do think it is sad that roho has received 3 downvotes for his polite request, as did I when I objected to the profanity in stonecolddevin's sig. I upvoted both him and Karl for the discussion. Way too much downvoting for inappropriate reasons here!
Discipulus learn that 'argue' has a little negative sense, he thought was a neutral sense, 'vox media'
[1nickt]: argue == discuss && argue == be contentious

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2017-06-22 12:18 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (519 votes). Check out past polls.