Query about mod operator
Jim Ault
JimAultWins at yahoo.com
Mon Apr 21 14:43:20 EDT 2008
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
> 0.03, 0.02, 0
> 0.04, 0.04, -0.04
> 0.05, 0.05, -0.05
> 0.06, 0.02, 0
> 0.07, 0.02, 0
> 0.08, 0.08, -0.08
> 0.09, 0.08, 0
> 0.1, 0.1, -0.1
> 0.11, 0.08, 0
> 0.12, 0.08, 0
> <>
> 0.3, 0.2, 0
> 0.31, 0.25, 0
> 0.32, 0.32, -0.32
> 0.33, 0.08, 0
> 0.34, 0.18, 0
> 0.35, 0.3, 0
> 0.36, 0.08, 0
> 0.37, 0.23, 0
> 0.38, 0.02, 0
> 0.39, 0.2, 0
> <>
> 0.7, 0.3, 0
> 0.71, 0.19, 0
> 0.72, 0.08, 0
> 0.73, 0.7, 0
> <>
> 0.79, 0.1, 0
> 0.8, 0.8, -0.8
> 0.81, 0.71, 0
> 0.82, 0.62, 0
> <>
> 0.88, 0.08, 0
> 0.89, 0.88, 0
> 0.9, 0.8, 0
> 0.91, 0.72, 0
> 0.92, 0.64, 0
> <>
> 0.95, 0.4, 0
> 0.96, 0.32, 0
> 0.97, 0.24, 0
> 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
>
>
> On Apr 21, 2008, at 1:00 PM, use-revolution-request at lists.runrev.com
> wrote:
>>
>> Message: 15
>> Date: Mon, 21 Apr 2008 10:39:33 -0400
>> From: Colin Holgate <coiin at rcn.com>
>> Subject: Re: Query about mod operator
>> To: How to use Revolution <use-revolution at lists.runrev.com>
>> Message-ID: <C9F53C6A-67C6-4B88-B732-608AB6DE5153 at rcn.com>
>> Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes
>>
>>
>> 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.
>
> _______________________________________________
> use-revolution mailing list
> use-revolution at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your subscription
> preferences:
> http://lists.runrev.com/mailman/listinfo/use-revolution
More information about the use-livecode
mailing list