Looking for a hard function
#1

I am not sure whether you will understand me, but I'll try to explain what I want.
I need a function, which gets one float value, then finds the most closest value to the first one, which can be divided by a spefic value without modulo. It sounds like a real mess, right? So I will try to explain in examples:

pawn Код:
//let's call our function FindClosestVal
stock FindClosestVal(Float:inputval,Float:divisor);//where inputval is a float value we input, and divisor is a value by which the final value can be divided

//Let's look at this example script:

new Float:a;
a=FindClosestVal(10.0, 3.0);
//If the function is right, we will get a=9, as 9 is the most closest value to 10, which can be divided by 3

new Float:b;
b=FindClosestVal(4.45, 1.5)
//in result we will get b=3, because 3 is the closest to 4.45 value which can be divided by 1.5
I hope you understood what thia function must do. WHo has any solution of this?
Reply
#2

pawn Код:
stock Float:FindClosestVal(Float:inputval,Float:divisor) {
    return floatround(inputval / divisor, floatround_floor) * divisor;
}
?
Reply
#3

Quote:
Originally Posted by Misiur
Посмотреть сообщение
pawn Код:
stock Float:FindClosestVal(Float:inputval,Float:divisor) {
    return floatround(inputval / divisor, floatround_floor) * divisor;
}
?
Hmm, byt what if the closest value will be float? This won't wqork then. for example:

FindClosestVal(5,1.5) - the value will be 4.5
EDIT: I've found out that it works! I don't know how, But i am very glad! Thank you very much for help!
Reply
#4

Please, make sure that you actually run the code someone gave you before you tell that it doesn't work as you wanted it to. I wasted 10 minutes of my time writing version which makes sure that the number doesn't have fraction part:

pawn Код:
stock FindClosestVal(Float:inputval,Float:divisor) {
    new i = floatround(inputval / divisor, floatround_floor), rnd;
    for(; i != 0; --i) {
        rnd = floatround(i * divisor, floatround_floor);
        if(i * divisor == float(rnd)) return rnd;
    }
    return 0;
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)