Query about mod operator

Mick Collins mickclns at mac.com
Tue Apr 22 18:31:05 EDT 2008


code below, comments interspersed,
    -  Mick


>> Date: Tue, 22 Apr 2008 07:31:42 -0700
> From: Richard Gaskin <ambassador at fourthworld.com>
> Subject: Re: Query about mod operator

> There's a long-standing debate between engineers and people about
> whether computers should be made to think like humans or the other way
> around.
>
> For microchip instruction sets I'll leave that debate to the pros in
> that field, but with high-level languages like Transcript I tend to
> favor people.


Mick:  I agree completely


>  it seems we could do something like this for mod:
>
> function logicalMod n
>    set the itemdel to "."
>    get item 2 of n
>    if it is empty then return 0
>    else return it
> end logicalMod
>
> Where would this fail?
>

mod as a function needs (at least) two arguments (unless I  
misunderstand what you are doing with this ... only giving integer  
results, only mod'g by 1?)

In the past I have done something like this:


FUNCTION rMod n, nMod, dMod
    -- rational mod,  returns  n mod (nMod/dMod)
    -- assumes nMod and dMod are integers
    put n*dMod into nMults -- the number of multiples that n is, of 1/ 
dMod
    return (round(nMults mod nMod)    /    dMod)
    --    OR
    --   return (  round(nMults mod nMod) && "/" && dMod  )
    --                                 -- if the mod is desired as a  
fraction
END rMod



and to test it:


ON tst2
    put displayMod(8, 35, 100) into line 1 of  field 1
    put displayMod(8, 25, 100) into line 2 of  field 1
    put displayMod(8, 4, 3) into line 3 of  field 1
    put displayMod(8, 5, 3) into line 4 of  field 1
    put displayMod(8.1, 1, 3) into line 5 of  field 1
    put displayMod(45, 7, 1) into line 6 of  field 1
END tst2


FUNCTION displayMod n,nMod,dMod
    return n & ",   " & nMod & ",   " & dMod & ",   " & rMod 
(n,nMod,dMod)
END displayMod


the results:
8,   35,   100,   0.3
8,   25,   100,   0
8,   4,   3,   0
8,   5,   3,   1.333333
8.1,   1,   3,   0
45,   7,   1,   3

These are the expected / intended results.  There are other  
variations depending on your needs, see the end of the rMod function  
for an example.  Pretty clunky to use, though, unless you are  
actually working with fractions or arbitrarily limiting your decimal  
precision.


> And should we expect a high-level language to return results like this
> from the engine directly, or is there some benefit to having illogical
> byproducts of microchip design as our result that I'm not  
> understanding?
>
Mick:  it would be nice, but not absolutely necessary, IMHO
>
> Message: 18
> Date: Tue, 22 Apr 2008 07:52:26 -0700
> From: Richard Gaskin <ambassador at fourthworld.com>
>
> Colin Holgate wrote:
>
>> At 7:31 AM -0700 4/22/08, Richard Gaskin wrote:
>>>   if n is not a number then return "NAN"
>>>   get offset(".", n)
>>
>> Do you know that in many countries a number might be written like  
>> this?:
>>
>> 10.000,1234
>>
>> Try setting your International number settings to Belgium, you'll  
>> see.
>
> True.  Let's say we account for the delimiter, since we're  
> proposing an
> engine change and it's a one-liner to obtain that from the OS.
>
> Can you think of any reason why a high-level language wouldn't return
> results like what's proposed?
>
> -- 
>   Richard Gaskin
>   Managing Editor, revJournal
>   _______________________________________________________
>   Rev tips, tutorials and more: http://www.revJournal.com
>
>
>
> ------------------------------
>
> Message: 30
> Date: Tue, 22 Apr 2008 12:25:48 -0400
> From: Colin Holgate <coiin at rcn.com>
>
> At 7:52 AM -0700 4/22/08, Richard Gaskin wrote:
>> True.  Let's say we account for the delimiter, since we're proposing
>> an engine change and it's a one-liner to obtain that from the OS.
>
>
> It seems wrong to try and fix a math issue with a string hack.
>
>
>> Can you think of any reason why a high-level language wouldn't
>> return results like what's proposed?
>
> No. Either it should be integer and only return integers, or it
> should be floating pointing, and return decimal values too. Flash
> does floating point mod, but with the limits of binary arithmetic
> (4.3 % 2.1 = 0.09999999999999964), Director uses integers (4.3 mod
> 2.1 = 0), and Rev and HyperCard use floating point, but with repaired
> results (4.3 mod 2.1 = 0.1).

Mick:  agreed, using my function, I get
4.3,   21,   10,   0.1
(I assume, by "repaired results" you mean something like what I do  
with round)

>



More information about the use-livecode mailing list