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

How to find when a variable is a decimal?

Asked by 4 years ago

So I am making a script that tells me what number is a prime number.

local dividend= 1
local divisor= 1


function divide()
    local result= dividend/divisor
    --if result is not a decimal then
        --if result is not 1 or "dividend" then
    print(dividend.."is not a prime number")
    else
        print(dividend.."is a prime number")
        dividend= dividend + 1
        divisor= 1
    end
end
divisor= divisor + 1
end

while true do
    divide()
wait()
end

In line 7, I don't know how to find if "result" has a decimal, as per the question says. In line 8, as soon as I type "not", it gets a red line under it.

Please note that I put the end and else just for convenience so please don't tell me that's the one incorrect.

1 answer

Log in to vote
2
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

You can do this two different ways. Using the function modf() from math, we can devise two results from the number given: It's integral, and fractional component. If we retrieve a fractional component greater than zero, our number contains decimals. To use this directly in a conditional, we can use select() in conjunction with it to compare the second result.

local Pi = 3.14

if (select(2,math.modf(Pi)) > 0) then
    print("Is a float!")
end

We can also use string patterns if you wish to match a dot character and a digit (which is a result of a decimal:

local Pi = 3.14

if (tostring(Pi):match("%p%d+") then
    print("Is a float!")
end
0
Yes, thank you. Now I shall proceed to find the next largest prime number there is to improve the security of banks. And I might get a prize for it too, but who knows :) Cyroxite 46 — 4y
0
Huh, well good luck, glad to help you on your way! Ziffixture 6913 — 4y
Ad

Answer this question