[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Number type limitations
- From: Coda Highland <chighland@...>
- Date: Tue, 5 Apr 2016 07:38:49 -0700
On Tue, Apr 5, 2016 at 3:26 AM, Vyacheslav Napadovsky
<napadovskiy@gmail.com> wrote:
> Hello.
>
> Recently I faced a bug with some arithmetic. I suppose it is not Lua
> related but PC architecture related. Nevertheless, I have a question
> about how to avoid this behavior correctly.
>
> I assume to check multiplicity with math.fmod() == 0.
>
>> =math.fmod(235.7, 0.05)
> 0.049999999999976
>> = 235.7 / 0.05
> 4714
>> = 235.7 % 0.05
> 0.049999999999983
>> = 235.7 - 235.7 / 0.05 * 0.05
> 2.8421709430404e-14
>> =_VERSION
> Lua 5.2
>
>
> --
> Vyacheslav Napadovsky
Try working with a rational number implementation like
https://github.com/torch/rational. Your particular case could probably
be done like:
rat = require("rational")
x = rat(2357, 10)
y = rat(5, 100)
=(rat.gcd(x, y) == rat.min(x, y))
That is, if the greatest common denominator of the two numbers is
equal to the smaller of the numbers, then the smaller number evenly
divides the larger.
/s/ Adam