Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^2: modulo 1 (%1) and fractional part of a number

by JBCookin (Initiate)
on Sep 14, 2018 at 16:17 UTC ( [id://1222383]=note: print w/replies, xml ) Need Help??


in reply to Re: modulo 1 (%1) and fractional part of a number
in thread modulo 1 (%1) and fractional part of a number

Yea I know what it does. My point is wouldn't it be more useful if m%n worked the way it does now for all n where n≠1, but when n=1 it returns the fractional part, since right now it only returns 0.

I understand why it only returns 0, I just think that it would be more useful if for the specific case m%1 the return value is the fractional part of m.

Replies are listed 'Best First'.
Re^3: modulo 1 (%1) and fractional part of a number
by LanX (Saint) on Sep 14, 2018 at 16:21 UTC
    This kind of DWIM magic edge cases is actually causing much trouble in Perl.

    Better define your own custom function.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

      I agree that making an edge case for 1, specifically, would be DWIMmery. But I think that the current implementation of truncate floating points to integers before applying an integer modulo, unless the divisor happens to be bigger than UV_MAX+1; oh, and by the way, we're going to require you to study perlguts to know that UV_MAX is the same number of bits as IV_MAX, but unsigned, and that UV_MAX+1 can thus be calculated from $UV_MAX_PLUS_ONE = 256**$Config{ivsize}. Personally, I think it should only use the integer moduluo if both operands are integers that fit within ivsize; otherwise, if either operand is recognizably floating point (ie, has a fractional component, or is too big for IV), then use floating-point modulo. Or, be like C and never make exceptions: have separate operators for integer modulo and floating modulo. The Perl implementation has too many DWIM DWTPM-isms for my tastes.

      But yes, since it's so easy to define the custom function as hippo did, that's the best choice for Perl at this stage in the game.

        I don't know much about the other flaws*, but we agree that we shouldn't add more.

        > otherwise, if either operand is recognizably floating point (ie, has a fractional component, or is too big for IV), then use floating-point modulo.

        Perl is unlike Python or JS an operator language.

        If you want an operator where modulo returns a fraction, write a patch to introduce something like %% (hopefully this doesn't clash with a special variable)°

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

        *) I suppose they are related to integers being represented as floats if they become to big to be stored as integers.

        °) hnngnggnn ...

        $FORMAT_PAGE_NUMBER $% The current page number of the currently selected output channel. Mnemonic: % is page number in nroff.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1222383]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2024-03-28 14:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found