Query about mod operator

Mick Collins mickclns at mac.com
Tue Apr 22 10:11:48 EDT 2008


Thanks Jim and Colin, especially the cautions about integer functions  
used for non-integer  operations.  Turns out I snipped out (after  
missing in my scan-over) the examples that would have proved your  
points, namely:
0.25,   0,   0
0.5,   0,   0
0.75,   0.5,   0
These are the only exact divisors of 8 for which the mods come out  
"correctly", that is,  as 0.
.25 decimal = .01 binary, exactly
.5 decimal = .1 binary, exactly
.75 decimal = .11 binary, exactly

That said, 4 things:
1) Other languages have an fmod for floating point, could Rev? (or  
does it have one that is hidden?).
2) Because I don't know of an fmod, when I need to use such  
functionality, I write my own function that bypasses the non- 
integer / binary problems.
3) That usually involves converting to integer, using the built in  
function, and converting  back.  I have never been unable to produce  
a function this way that gives the correct values (except extreme  
fringe-of-precision numbers).
4) A related joke that hard-core programmers probably know already:
Why do programmers have trouble distinguishing between Christmas and  
Halloween?

Because 25 dec = 31 oct.

(for those not understanding, octal is base 8, and is a direct  
conversion from  base 2, so used some in nitty gritty low level and  
hardware programming (still used? I'm not sure, I don't). In octal,   
"31" represents three 8s and one 1, 24+1= 25 in decimal. )


nerdy, nerdy, nerdy,
    -  Mick


On Apr 22, 2008, at 6:48 AM, use-revolution-request at lists.runrev.com  
wrote:
>
> Message: 3
> Date: Mon, 21 Apr 2008 11:43:20 -0700
> From: Jim Ault <JimAultWins at yahoo.com>
>
> Mick,
>
>> It seems that    d   divides   8     if and only if  the mod is NOT
>> correct (until we get to 1, of course.  I got similar results using 7
>> instead of 8..  Seems like it should work, I think it's a bug.
>
> You need to use caution when working with fractions in computer  
> operations.
> Binary means that math involving fractions in base 10 is not  
> precise as you
> would normally think about it.  This has been covered on the list  
> in the
> last couple months.
>
> I think that 'mod' would exhibit the same apparent irregularities  
> that all
> other math functions do when dealing with fractions.
>
> If you want to be more accurate in this case:
>
> put 100 into mult
> put ((8 * mult) mod (div * mult))/ mult into theDiff
>
> but again, you need to be aware of the binary nature of computers  
> expressing
> fractions when calculating theDiff.  All computer languages have  
> the same
> limitation.  This is why floating point libraries are added to  
> scientific
> processing to achieve accuracy for using decimal values.
>
> Hope this helps shed light on the subject.
>
> Jim Ault
> Las Vegas
>
> On 4/21/08 11:18 AM, "Mick Collins" <mickclns at mac.com> wrote:
>
>> Interesting, I don't think it has to do with dividing by zero
>>
>> I ran this script:
>>
>> ON tst
>>     REPEAT WITH i = 1 to 100
>>        put i * .01 into iDiv
>>        put 8 mod iDiv into modiDiv
>>        put 8 div iDiv into intQuotient
>>        put 8 - (intQuotient * iDiv + modiDiv) into theDiff
>>        put iDiv & ",   " &  modiDiv & ",   " & theDiff into line i of
>> field 1
>>     END repeat
>> END tst
>>
>>
>> The (partial) results are:
>> 0.01,   0.01,   -0.01
>> 0.02,   0.02,   -0.02
<snip>
>> 0.98,   0.16,   0
>> 0.99,   0.08,   0
>> 1,   0,   0
>>
>>
>> It seems that    d   divides   8     if and only if  the mod is NOT
>> correct (until we get to 1, of course.  I got similar results using 7
>> instead of 8..  Seems like it should work, I think it's a bug.
>>
>>     -  Mick
>>>
>>> Message: 15
>>> Date: Mon, 21 Apr 2008 10:39:33 -0400
>>> From: Colin Holgate <coiin at rcn.com>
>>> On Apr 21, 2008, at 10:11 AM, Paul Williams wrote:
>>>
>>>> why does 8 mod 0.05  return 0.05 ?
>>>
>>> Mod is normally an integer operation, and as you wouldn't want to  
>>> risk
>>> a divide by zero issue, perhaps it divides by at least 1? That would
>>> give a remainder that was equal to the fraction.
>>
>> _______________________________________________
>
> Message: 4
> Date: Mon, 21 Apr 2008 13:30:47 -0700
> From: "Mark Wieder" <mwieder at ahsoftware.net>
>
> ...and from the docs...
> Note: Using non-integer number and divisor usually produces sensible
> results. However, mathematically, modulus is generally defined as a  
> function
> over the integers, and the results using non-integers may not  
> consistently
> be what you expect.
>
>
> -- 
>  Mark Wieder
>  mwieder at ahsoftware.net
>
>
>




More information about the Use-livecode mailing list