Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

[Solved] i % 1 does not give 0 in script, but manually in command line it does?

Asked by
rabbi99 714 Moderation Voter
3 years ago
Edited 3 years ago

I have a piece of code that counts in a for loop but while counting I want it so whenever it is divisible by 1 to run a function. However, it keeps failing the 'checking if divisible by 1' part (it gives a different number). I just can't figure it out, unless it's some bug.

Here's a slightly edited copy of my script:

for i = 0,Time, 0.1 do
    Charge = i / Time
    if i % 1 == 0 then
        print(i, true,  i % 1)
        Function()
    else
        print(i, false, i % 1)
    end
    if HoldingZ == false then
        break
    end
    wait(.1)
end

And here's the output:

>   0 true 0
>   0.1 false 0.1
>   0.2 false 0.2
>   0.3 false 0.3
>   0.4 false 0.4
>   0.5 false 0.5
>   0.6 false 0.6
>   0.7 false 0.7
>   0.8 false 0.8
>   0.9 false 0.9
>   1 false 1 -- Why does it not output 0?
>   1.1 false 0.1
>   1.2 false 0.2
>   1.3 false 0.3
>   1.4 false 0.4
>   1.5 false 0.5
>   1.6 false 0.6
>   1.7 false 0.7
>   1.8 false 0.8
>   1.9 false 0.9
>   2 false 4.4408920985006e-16 -- Why does it not output 0?
>   2.1 false 0.1
>   2.2 false 0.2
>   2.3 false 0.3
>   2.4 false 0.4
>   2.5 false 0.5
>   2.6 false 0.6
>   2.7 false 0.7
>   2.8 false 0.8
>   2.9 false 0.9
>   3 false 1.3322676295502e-15 -- Why does it not output 0?
>   3.1 false 0.1
>   3.2 false 0.2
>   3.3 false 0.3
>   3.4 false 0.4
>   3.5 false 0.5
>   3.6 false 0.6
>   3.7 false 0.7
>   3.8 false 0.8
>   3.9 false 0.9
>   4 false 1.7763568394003e-15 -- Why does it not output 0?
>   4.1 false 0.1
>   4.2 false 0.2
>   4.3 false 0.3
>   4.4 false 0.4
>   4.5 false 0.5
>   4.6 false 0.6
>   4.7 false 0.7
>   4.8 false 0.8
>   4.9 false 0.9
>   5 false 1 -- Why does it not output 0?

Now here's what happens if I do this in the command line at the bottom of Roblox Studio:

print(5%1) -- Output: 0

What am I doing wrong? I am absolutely confused!

Thanks for helping!

Solution on DevForum: https://devforum.roblox.com/t/modulus-not-working-properly-in-loops/874087/2

1
Glad to hear that this was solved! :D Nckripted 580 — 3y
1
Glad to hear that this was solved! :D Nckripted 580 — 3y

2 answers

Log in to vote
1
Answered by
Nckripted 580 Moderation Voter
3 years ago
Edited 3 years ago

I have no idea what this is happening. Fortunately, I was browsing around, and I found a replacement for the modulus (you aren't the only one having an issue with this operator):

a % b = a - math.floor(a / b) * b

It's not ideal, but hey! At least it returns the remainder. So, you could make a variable with this and write your code something like this:

local remainder = i - math.floor(i / 1) * 1

Hope this helped!

0
Tell me if I made a mistake with formula. I am currently using a device where most Roblox resources are restricted, and I can later look into the workings of this operator. Nckripted 580 — 3y
0
change b to 1 by the way OhManXDXD 445 — 3y
0
I did that already. The change might not have updated, but I did notice that mistake. Thanks for pointing it out though! Nckripted 580 — 3y
0
Unfortunately, this brings out the same error. I figured this only happens with for loops. I don't understand why... rabbi99 714 — 3y
View all comments (3 more)
0
What the heck? I will investigate the issue more once I can utilize a different device. Nckripted 580 — 3y
0
Hey, I asked on the DevForum. I'll provide a link soon. They had a pretty crazy answer. rabbi99 714 — 3y
0
But I really appreciate your help and effort. I really do! rabbi99 714 — 3y
Ad
Log in to vote
1
Answered by
OhManXDXD 445 Moderation Voter
3 years ago

Use math.fmod, it does exactly what you want.


for i = 0,Time, 0.1 do Charge = i / Time if math.fmod(i,1) == 0 then print(i, true, math.fmod(i, 1)) Function() else print(i, false, math.fmod(i, 1)) end if HoldingZ == false then break end wait(.1) end
0
You one hundred percent certain Luau supports math.fmod? Nckripted 580 — 3y
0
Unfortunately, this brings out the same error. I figured this only happens with for loops. I don't understand why... rabbi99 714 — 3y

Answer this question